[StGit PATCH 4/5] Make generic --message/--file/--save-template flags

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



And let "stg edit" use them.

Signed-off-by: Karl Hasselström <kha@xxxxxxxxxxx>

---

 stgit/commands/edit.py |   77 +++++++++++++++++++-----------------------------
 stgit/utils.py         |   45 ++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 47 deletions(-)


diff --git a/stgit/commands/edit.py b/stgit/commands/edit.py
index 65b54d9..b9699d5 100644
--- a/stgit/commands/edit.py
+++ b/stgit/commands/edit.py
@@ -61,8 +61,6 @@ directory = DirectoryGotoToplevel()
 options = [make_option('-d', '--diff',
                        help = 'edit the patch diff',
                        action = 'store_true'),
-           make_option('-f', '--file',
-                       help = 'use FILE instead of invoking the editor'),
            make_option('-O', '--diff-opts',
                        help = 'options to pass to git-diff'),
            make_option('--undo',
@@ -70,10 +68,6 @@ options = [make_option('-d', '--diff',
                        action = 'store_true'),
            make_option('-a', '--annotate', metavar = 'NOTE',
                        help = 'annotate the patch log entry'),
-           make_option('-m', '--message',
-                       help = 'replace the patch description with MESSAGE'),
-           make_option('--save-template', metavar = 'FILE',
-                       help = 'save the patch to FILE in the format used by -f'),
            make_option('--author', metavar = '"NAME <EMAIL>"',
                        help = 'replae the author details with "NAME <EMAIL>"'),
            make_option('--authname',
@@ -86,47 +80,48 @@ options = [make_option('-d', '--diff',
                        help = 'replace the committer name with COMMNAME'),
            make_option('--commemail',
                        help = 'replace the committer e-mail with COMMEMAIL')
-           ] + make_sign_options()
+           ] + make_sign_options() + make_message_options()
 
-def __update_patch(pname, fname, options):
-    """Update the current patch from the given file.
+def __update_patch(pname, text, options):
+    """Update the current patch from the given text.
     """
     patch = crt_series.get_patch(pname)
 
     bottom = patch.get_bottom()
     top = patch.get_top()
 
-    if fname == '-':
-        f = sys.stdin
-    else:
-        f = open(fname)
-    (message, author_name, author_email, author_date, diff
-     ) = parse_patch(f.read())
-    f.close()
+    message, author_name, author_email, author_date, diff = parse_patch(text)
 
     out.start('Updating patch "%s"' % pname)
 
     if options.diff:
         git.switch(bottom)
         try:
-            git.apply_patch(fname)
+            git.apply_patch(diff = diff)
         except:
             # avoid inconsistent repository state
             git.switch(top)
             raise
 
+    def c(a, b):
+        if a != None:
+            return a
+        return b
     crt_series.refresh_patch(message = message,
-                             author_name = author_name,
-                             author_email = author_email,
-                             author_date = author_date,
-                             backup = True, log = 'edit')
+                             author_name = c(options.authname, author_name),
+                             author_email = c(options.authemail, author_email),
+                             author_date = c(options.authdate, author_date),
+                             committer_name = options.commname,
+                             committer_email = options.commemail,
+                             backup = True, sign_str = options.sign_str,
+                             log = 'edit', notes = options.annotate)
 
     if crt_series.empty_patch(pname):
         out.done('empty patch')
     else:
         out.done()
 
-def __generate_file(pname, fname, options):
+def __generate_file(pname, write_fn, options):
     """Generate a file containing the description to edit
     """
     patch = crt_series.get_patch(pname)
@@ -175,12 +170,7 @@ def __generate_file(pname, fname, options):
     text = tmpl % tmpl_dict
 
     # write the file to be edited
-    if fname == '-':
-        sys.stdout.write(text)
-    else:
-        f = open(fname, 'w+')
-        f.write(text)
-        f.close()
+    write_fn(text)
 
 def __edit_update_patch(pname, options):
     """Edit the given patch interactively.
@@ -189,13 +179,17 @@ def __edit_update_patch(pname, options):
         fname = '.stgit-edit.diff'
     else:
         fname = '.stgit-edit.txt'
+    def write_fn(text):
+        f = file(fname, 'w')
+        f.write(text)
+        f.close()
 
-    __generate_file(pname, fname, options)
+    __generate_file(pname, write_fn, options)
 
     # invoke the editor
     call_editor(fname)
 
-    __update_patch(pname, fname, options)
+    __update_patch(pname, file(fname).read(), options)
 
 def func(parser, options, args):
     """Edit the given patch or the current one.
@@ -232,25 +226,14 @@ def func(parser, options, args):
         out.start('Undoing the editing of "%s"' % pname)
         crt_series.undo_refresh()
         out.done()
-    elif options.message or options.authname or options.authemail \
-             or options.authdate or options.commname or options.commemail \
-             or options.sign_str:
-        # just refresh the patch with the given information
-        out.start('Updating patch "%s"' % pname)
-        crt_series.refresh_patch(message = options.message,
-                                 author_name = options.authname,
-                                 author_email = options.authemail,
-                                 author_date = options.authdate,
-                                 committer_name = options.commname,
-                                 committer_email = options.commemail,
-                                 backup = True, sign_str = options.sign_str,
-                                 log = 'edit',
-                                 notes = options.annotate)
-        out.done()
     elif options.save_template:
         __generate_file(pname, options.save_template, options)
-    elif options.file:
-        __update_patch(pname, options.file, options)
+    elif any([options.message, options.authname, options.authemail,
+              options.authdate, options.commname, options.commemail,
+              options.sign_str]):
+        out.start('Updating patch "%s"' % pname)
+        __update_patch(pname, options.message, options)
+        out.done()
     else:
         __edit_update_patch(pname, options)
 
diff --git a/stgit/utils.py b/stgit/utils.py
index 3a480c0..837c6c2 100644
--- a/stgit/utils.py
+++ b/stgit/utils.py
@@ -256,3 +256,48 @@ def add_sign_line(desc, sign_str, name, email):
     if not any(s in desc for s in ['\nSigned-off-by:', '\nAcked-by:']):
         desc = desc + '\n'
     return '%s\n%s\n' % (desc, sign_str)
+
+def make_message_options():
+    def no_dup(parser):
+        if parser.values.message != None:
+            raise optparse.OptionValueError(
+                'Cannot give more than one --message or --file')
+    def no_combine(parser):
+        if (parser.values.message != None
+            and parser.values.save_template != None):
+            raise optparse.OptionValueError(
+                'Cannot give both --message/--file and --save-template')
+    def msg_callback(option, opt_str, value, parser):
+        no_dup(parser)
+        parser.values.message = value
+        no_combine(parser)
+    def file_callback(option, opt_str, value, parser):
+        no_dup(parser)
+        if value == '-':
+            parser.values.message = sys.stdin.read()
+        else:
+            f = file(value)
+            parser.values.message = f.read()
+            f.close()
+        no_combine(parser)
+    def templ_callback(option, opt_str, value, parser):
+        if value == '-':
+            def w(s):
+                sys.stdout.write(s)
+        else:
+            def w(s):
+                f = file(value, 'w+')
+                f.write(s)
+                f.close()
+        parser.values.save_template = w
+        no_combine(parser)
+    m = optparse.make_option
+    return [m('-m', '--message', action = 'callback', callback = msg_callback,
+              dest = 'message', type = 'string',
+              help = 'use MESSAGE instead of invoking the editor'),
+            m('-f', '--file', action = 'callback', callback = file_callback,
+              dest = 'message', type = 'string', metavar = 'FILE',
+              help = 'use FILE instead of invoking the editor'),
+            m('--save-template', action = 'callback', callback = templ_callback,
+              metavar = 'FILE', dest = 'save_template', type = 'string',
+              help = 'save the message template to FILE and exit')]

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux