[PATCH] Show patch content in description edit buffer
authorPaolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Fri, 2 Sep 2005 09:25:20 +0000 (11:25 +0200)
committerCatalin Marinas <catalin.marinas@gmail.com>
Fri, 2 Sep 2005 16:15:30 +0000 (17:15 +0100)
Add an option (-s/--showpatch) to show the patch content when editing the
patch description, by appending it to the edit buffer, for more accurate
patch comment editing and to check its correctness before committing it.

It's how one works with quilt, and it's very important when the patch is
non-trivial to re-read it.

It's done for import, new and refresh, I left fold out.

Additionally, set the buffer filetype to "diff" for correct highlighting in
Vi.

Signed-off-by: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
stgit/commands/imprt.py
stgit/commands/new.py
stgit/commands/refresh.py
stgit/stack.py

index 9dccaf9..c6eaa04 100644 (file)
@@ -45,6 +45,9 @@ options = [make_option('-m', '--mail',
            make_option('-e', '--edit',
                        help = 'invoke an editor for the patch description',
                        action = 'store_true'),
+           make_option('-s', '--showpatch',
+                       help = 'show the patch content in the editor buffer',
+                       action = 'store_true'),
            make_option('-a', '--author', metavar = '"NAME <EMAIL>"',
                        help = 'use "NAME <EMAIL>" as the author details'),
            make_option('--authname',
@@ -199,7 +202,8 @@ def func(parser, options, args):
     sys.stdout.flush()
 
     git.apply_patch(filename)
-    crt_series.refresh_patch(edit = options.edit)
+    crt_series.refresh_patch(edit = options.edit,
+                             show_patch = options.showpatch)
 
     print 'done'
     print_crt_patch()
index 9efecc1..c968eaf 100644 (file)
@@ -39,6 +39,9 @@ options = [make_option('-m', '--message',
            make_option('--force',
                        help = 'proceed even if there are local changes',
                        action = 'store_true'),
+           make_option('-s', '--showpatch',
+                       help = 'show the patch content in the editor buffer',
+                       action = 'store_true'),
            make_option('-a', '--author', metavar = '"NAME <EMAIL>"',
                        help = 'use "NAME <EMAIL>" as the author details'),
            make_option('--authname',
@@ -63,11 +66,14 @@ def func(parser, options, args):
         check_local_changes()
         check_conflicts()
         check_head_top_equal()
+        # No local changes -> no patch to show
+        options.showpatch = False
 
     if options.author:
         options.authname, options.authemail = name_email(options.author)
 
     crt_series.new_patch(args[0], message = options.message,
+                         show_patch = options.showpatch,
                          author_name = options.authname,
                          author_email = options.authemail,
                          author_date = options.authdate,
index f5cb2e9..48b406a 100644 (file)
@@ -44,6 +44,9 @@ options = [make_option('-f', '--force',
                        help = 'invoke an editor for the patch '\
                        'description',
                        action = 'store_true'),
+           make_option('-s', '--showpatch',
+                       help = 'show the patch content in the editor buffer',
+                       action = 'store_true'),
            make_option('-m', '--message',
                        help = 'use MESSAGE as the patch ' \
                        'description'),
@@ -96,6 +99,7 @@ def func(parser, options, args):
             resolved_all()
         crt_series.refresh_patch(message = options.message,
                                  edit = options.edit,
+                                 show_patch = options.showpatch,
                                  author_name = options.authname,
                                  author_email = options.authemail,
                                  author_date = options.authdate,
index 1e68b99..1e45e72 100644 (file)
@@ -29,10 +29,21 @@ from stgit.config import config
 class StackException(Exception):
     pass
 
+class FilterUntil:
+    def __init__(self):
+        self.should_print = True
+    def __call__(self, x, until_test, prefix):
+        if until_test(x):
+            self.should_print = False
+        if self.should_print:
+            return x[0:len(prefix)] != prefix
+        return False
+
 #
 # Functions
 #
 __comment_prefix = 'STG:'
+__patch_prefix = 'STG_PATCH:'
 
 def __clean_comments(f):
     """Removes lines marked for status in a commit file
@@ -40,8 +51,12 @@ def __clean_comments(f):
     f.seek(0)
 
     # remove status-prefixed lines
-    lines = filter(lambda x: x[0:len(__comment_prefix)] != __comment_prefix,
-                   f.readlines())
+    lines = f.readlines()
+
+    patch_filter = FilterUntil()
+    until_test = lambda t: t == (__patch_prefix + '\n')
+    lines = [l for l in lines if patch_filter(l, until_test, __comment_prefix)]
+
     # remove empty lines at the end
     while len(lines) != 0 and lines[-1] == '\n':
         del lines[-1]
@@ -49,7 +64,7 @@ def __clean_comments(f):
     f.seek(0); f.truncate()
     f.writelines(lines)
 
-def edit_file(string, comment):
+def edit_file(series, string, comment, show_patch = True):
     fname = '.stgit.msg'
     tmpl = os.path.join(git.base_dir, 'patchdescr.tmpl')
 
@@ -66,6 +81,14 @@ def edit_file(string, comment):
           % __comment_prefix
     print >> f, __comment_prefix, \
           'Trailing empty lines will be automatically removed.'
+
+    if show_patch:
+       print >> f, __patch_prefix
+       # series.get_patch(series.get_current()).get_top()
+       git.diff([], series.get_patch(series.get_current()).get_bottom(), None, f)
+
+    #Vim modeline must be near the end.
+    print >> f, __comment_prefix, 'vi: set textwidth=75 filetype=diff:'
     f.close()
 
     # the editor
@@ -326,7 +349,8 @@ class Series:
         create_empty_file(self.__unapplied_file)
         self.__begin_stack_check()
 
-    def refresh_patch(self, message = None, edit = False, cache_update = True,
+    def refresh_patch(self, message = None, edit = False, show_patch = False,
+                      cache_update = True,
                       author_name = None, author_email = None,
                       author_date = None,
                       committer_name = None, committer_email = None,
@@ -347,9 +371,9 @@ class Series:
             descr = message
 
         if not message and edit:
-            descr = edit_file(descr.rstrip(), \
+            descr = edit_file(self, descr.rstrip(), \
                               'Please edit the description for patch "%s" ' \
-                              'above.' % name)
+                              'above.' % name, show_patch)
 
         if not author_name:
             author_name = patch.get_authname()
@@ -382,7 +406,7 @@ class Series:
 
         return commit_id
 
-    def new_patch(self, name, message = None, edit = False,
+    def new_patch(self, name, message = None, edit = False, show_patch = False,
                   author_name = None, author_email = None, author_date = None,
                   committer_name = None, committer_email = None):
         """Creates a new patch
@@ -391,9 +415,9 @@ class Series:
             raise StackException, 'Patch "%s" already exists' % name
 
         if not message:
-            descr = edit_file(None, \
+            descr = edit_file(self, None, \
                               'Please enter the description for patch "%s" ' \
-                              'above.' % name)
+                              'above.' % name, show_patch)
         else:
             descr = message