This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Teach mklog to reference PRs.


Hi.

I use quite often mklog and then I manually write references
to PRs. I would like to teach the script to do it for me.
Example:

$ ./contrib/mklog /tmp/patch -b lto/91307,c/12345 > /tmp/changelog
$ cat /tmp/changelog
(PR lto/91307,c/12345)

contrib/ChangeLog:

2019-08-01  Martin Liska  <mliska@suse.cz>

        PR lto/91307
        PR c/12345
	* mklog:

gcc/testsuite/ChangeLog:

2019-08-01  Martin Liska  <mliska@suse.cz>

        PR lto/91307
        PR c/12345
	* g++.dg/lto/pr89330_0.C:

As seen it puts the references to each ChangeLog file and to
the first line which will become a commit message.

Thoughts?
Thanks,
Martin

contrib/ChangeLog:

2019-08-01  Martin Liska  <mliska@suse.cz>

	* mklog: Use argparse as it's much easier to use.
	Add new option -b that can refence PRs.
---
 contrib/mklog | 80 +++++++++++++++++++++------------------------------
 1 file changed, 32 insertions(+), 48 deletions(-)


diff --git a/contrib/mklog b/contrib/mklog
index 15558cfbfe3..7b7b47f3afd 100755
--- a/contrib/mklog
+++ b/contrib/mklog
@@ -28,11 +28,11 @@
 #
 # Author: Yury Gribov <tetra2005@gmail.com>
 
+import argparse
 import sys
 import re
 import os.path
 import os
-import getopt
 import tempfile
 import time
 import shutil
@@ -66,21 +66,6 @@ class RegexCache(object):
 
 cache = RegexCache()
 
-def print_help_and_exit():
-    print("""\
-Usage: %s [-i | --inline] [PATCH]
-Generate ChangeLog template for PATCH.
-PATCH must be generated using diff(1)'s -up or -cp options
-(or their equivalent in Subversion/git).
-
-When PATCH is - or missing, read standard input.
-
-When -i is used, prepends ChangeLog to PATCH.
-If PATCH is not stdin, modifies PATCH in-place, otherwise writes
-to stdout.
-""" % me)
-    sys.exit(1)
-
 def run(cmd, die_on_error):
   """Simple wrapper for Popen."""
   proc = Popen(cmd.split(' '), stderr = PIPE, stdout = PIPE)
@@ -356,38 +341,30 @@ def parse_patch(contents):
 def main():
   name, email = read_user_info()
 
-  try:
-    opts, args = getopt.getopt(sys.argv[1:], 'hiv', ['help', 'verbose', 'inline'])
-  except getopt.GetoptError as err:
-    error(str(err))
-
-  inline = False
-  verbose = 0
-
-  for o, a in opts:
-    if o in ('-h', '--help'):
-      print_help_and_exit()
-    elif o in ('-i', '--inline'):
-      inline = True
-    elif o in ('-v', '--verbose'):
-      verbose += 1
-    else:
-      assert False, "unhandled option"
+  help_message =  """\
+Generate ChangeLog template for PATCH.
+PATCH must be generated using diff(1)'s -up or -cp options
+(or their equivalent in Subversion/git).
+"""
 
-  if len(args) == 0:
-    args = ['-']
+  inline_message = """\
+Prepends ChangeLog to PATCH.
+If PATCH is not stdin, modifies PATCH in-place,
+otherwise writes to stdout.
+"""
 
-  if len(args) == 1 and args[0] == '-':
-    input = sys.stdin
-  elif len(args) == 1:
-    input = open(args[0])
-  else:
-    error("too many arguments; for more details run with -h")
+  parser = argparse.ArgumentParser(description = help_message)
+  parser.add_argument('-v', '--verbose', action = 'store_true', help = 'Verbose messages')
+  parser.add_argument('-i', '--inline', action = 'store_true', help = inline_message)
+  parser.add_argument('-b', '--bugs', default = '', help = 'References to bugs separated by comma (e.g. lto/91234,ipa/12345)')
+  parser.add_argument('input', nargs = '?', help = 'Patch file (or missing, read standard input)')
+  args = parser.parse_args()
 
+  input = open(args.input) if args.input else sys.stdin
   contents = input.read()
   diffs = parse_patch(contents)
 
-  if verbose:
+  if args.verbose:
     print("Parse results:")
     for d in diffs:
       d.dump()
@@ -431,7 +408,7 @@ def main():
 
     logs[log_name] += change_msg if change_msg else ":\n"
 
-  if inline and args[0] != '-':
+  if args.inline and args.input:
     # Get a temp filename, rather than an open filehandle, because we use
     # the open to truncate.
     fd, tmp = tempfile.mkstemp("tmp.XXXXXXXX")
@@ -439,7 +416,7 @@ def main():
 
     # Copy permissions to temp file
     # (old Pythons do not support shutil.copymode)
-    shutil.copymode(args[0], tmp)
+    shutil.copymode(args.input, tmp)
 
     # Open the temp file, clearing contents.
     out = open(tmp, 'w')
@@ -449,22 +426,29 @@ def main():
 
   # Print log
   date = time.strftime('%Y-%m-%d')
+  bugmsg = ''
+  bugs = [b for b in args.bugs.split(',') if b]
+  if len(bugs):
+    bugmsg = '\n'.join(['%sPR %s' % (' ' * 8, pr) for pr in bugs]) + '\n'
+    # put bug references to the commit message (first line)
+    out.write('(PR ' + ','.join(bugs) + ')\n\n')
+
   for log_name, msg in sorted(logs.items()):
     out.write("""\
 %s:
 
 %s  %s  <%s>
 
-%s\n""" % (log_name, date, name, email, msg))
+%s%s\n""" % (log_name, date, name, email, bugmsg, msg))
 
-  if inline:
+  if args.inline:
     # Append patch body
     out.write(contents)
 
-    if args[0] != '-':
+    if args.input:
       # Write new contents atomically
       out.close()
-      shutil.move(tmp, args[0])
+      shutil.move(tmp, args.input)
 
 if __name__ == '__main__':
     main()


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]