[PATCH] mq: Add mechanism for setting patch header contents

Peter Williams pwil3058 at bigpond.net.au
Sat Aug 22 01:46:17 CDT 2009


# HG changeset patch
# User Peter Williams <pwil3058 at bigpond.net.au>
# Date 1250923379 -36000
# Node ID c4c1c8bd697f0e62c546740b8dc84b65990f2a6a
# Parent  37042e8b3b342b2e380d8be3e3f7692584c92d33
mq: Add mechanism for setting patch header contents

Modify 'qheader' by adding options rather than creating a new command.
Make sure that users will not be surprised by changes to 'qheader'.
Modification may be made to both applied and unapplied patches.
If the patch is applied, the change is not propagated into the change
set representing the patch but the user is warned that a refresh is
required for the propagation to occur.

Signed-off-by: Peter Williams <pwil3058 at bigpond.net.au>

diff -r 37042e8b3b34 -r c4c1c8bd697f hgext/mq.py
--- a/hgext/mq.py	Mon Jun 29 01:33:26 2009 +0900
+++ b/hgext/mq.py	Sat Aug 22 16:42:59 2009 +1000
@@ -2084,8 +2084,20 @@ def guard(ui, repo, *args, **opts):
     else:
         status(q.series.index(q.lookup(patch)))
 
-def header(ui, repo, patch=None):
-    """print the header of the topmost or specified patch"""
+def header(ui, repo, patch=None, **opts):
+    """print or modify the header of the specified patch (or topmost
+    patch if no patch specified).
+
+    When run without options, the header of the patch is printed.
+
+    -e/--edit, -m/--message or -l/--logfile set the patch header. This
+    will become the commit message the next time the patch is refreshed
+    or pushed.
+
+    -u/--user and -d/--date can be used to set the (specified) user and
+    date, respectively. -U/--currentuser and -D/--currentdate set user
+    to current user and date to current date.
+    """
     q = repo.mq
 
     if patch:
@@ -2095,6 +2107,64 @@ def header(ui, repo, patch=None):
             ui.write('no patches applied\n')
             return 1
         patch = q.lookup('qtip')
+
+    if [val for val in opts.values() if val]: # any options supplied?
+        setupheaderopts(ui, opts)
+        newuser = opts.get('user')
+        newdate = opts.get('date')
+        if newdate:
+            newdate = '%d %d' % util.parsedate(newdate)
+        if opts.get('edit'):
+            if opts.get('message') or opts.get('logfile'):
+                raise util.Abort(_('option "-e" incompatible with "-m" or "-l"'))
+            msg = '\n'.join(patchheader(q.join(patch)).message)
+            msg = ui.edit(msg, ui.username())
+        else:
+            msg = cmdutil.logmessage(opts)
+        wlock = repo.wlock()
+        try:
+
+            patchf = q.opener(patch, 'r')
+
+            lines = patchf.readlines()
+            bd = 0
+            while bd < len(lines):
+                if lines[bd].startswith('diff --git'):
+                    break
+                elif lines[bd].startswith('--- '):
+                    if bd + 1 < len(lines) and lines[bd + 1].startswith('+++ '):
+                        break
+                bd += 1
+
+            ph = patchheader(q.join(patch))
+            if msg:
+                ph.setmessage(msg)
+            if newuser:
+                ph.setuser(newuser)
+            if newdate:
+                ph.setdate(newdate)
+
+            patchf = q.opener(patch, 'w', atomictemp=True)
+
+            patchf.seek(0)
+            patchf.truncate()
+
+            comments = str(ph)
+            if comments:
+                patchf.write(comments)
+            if bd < len(lines):
+                for line in lines[bd:]:
+                    patchf.write(line)
+            patchf.rename()
+            patchf.close()
+            if q.isapplied(patch):
+                ui.warn(_('%s requires refresh to update commit message\n') % patch)
+        except:
+            raise util.Abort(_('write "%s" header failed') % patch)
+        finally:
+            wlock.release()
+        return
+
     ph = patchheader(repo.mq.join(patch))
 
     ui.write('\n'.join(ph.message) + '\n')
@@ -2520,6 +2590,11 @@ def uisetup(ui):
     extensions.wrapcommand(commands.table, 'import', mqimport)
 
 seriesopts = [('s', 'summary', None, _('print first line of patch header'))]
+userdateopts = \
+    [('U', 'currentuser', None, _('add "From: <current user>" to patch')),
+     ('u', 'user', '', _('add "From: <given user>" to patch')),
+     ('D', 'currentdate', None, _('add "Date: <current date>" to patch')),
+     ('d', 'date', '', _('add "Date: <given date>" to patch'))]
 
 cmdtable = {
     "qapplied": (applied, [] + seriesopts, _('hg qapplied [-s] [PATCH]')),
@@ -2560,7 +2635,11 @@ cmdtable = {
          [('l', 'list', None, _('list all patches and guards')),
           ('n', 'none', None, _('drop all guards'))],
          _('hg qguard [-l] [-n] -- [PATCH] [+GUARD]... [-GUARD]...')),
-    'qheader': (header, [], _('hg qheader [PATCH]')),
+    'qheader':
+        (header,
+         [('e', 'edit', None, _('edit commit message')),
+         ] + userdateopts + commands.commitopts,
+         _('hg qheader [[-e] | [-m TEXT] | [-l FILE]] [PATCH]')),
     "^qimport":
         (qimport,
          [('e', 'existing', None, _('import file in patch directory')),
@@ -2579,11 +2658,7 @@ cmdtable = {
          [('e', 'edit', None, _('edit commit message')),
           ('f', 'force', None, _('import uncommitted changes into patch')),
           ('g', 'git', None, _('use git extended diff format')),
-          ('U', 'currentuser', None, _('add "From: <current user>" to patch')),
-          ('u', 'user', '', _('add "From: <given user>" to patch')),
-          ('D', 'currentdate', None, _('add "Date: <current date>" to patch')),
-          ('d', 'date', '', _('add "Date: <given date>" to patch'))
-          ] + commands.walkopts + commands.commitopts,
+          ] + userdateopts + commands.walkopts + commands.commitopts,
          _('hg qnew [-e] [-m TEXT] [-l FILE] [-f] PATCH [FILE]...')),
     "qnext": (next, [] + seriesopts, _('hg qnext [-s]')),
     "qprev": (prev, [] + seriesopts, _('hg qprev [-s]')),


More information about the Mercurial-devel mailing list