[PATCH][CORRECTION] mq: modify qheader to allow setting header contents

Peter Williams pwil3058 at bigpond.net.au
Tue Aug 18 07:22:24 CDT 2009


# HG changeset patch
# User Peter Williams <pwil3058 at bigpond.net.au>
# Date 1250597669 -36000
# Node ID a0a0323e97e43ab7f1dbb8504d25818423e7fa91
# Parent  37042e8b3b342b2e380d8be3e3f7692584c92d33
mq: Modify 'qheader' to support setting of the patch header contents

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

diff -r 37042e8b3b34 -r a0a0323e97e4 hgext/mq.py
--- a/hgext/mq.py	Mon Jun 29 01:33:26 2009 +0900
+++ b/hgext/mq.py	Tue Aug 18 22:14:29 2009 +1000
@@ -2084,8 +2084,19 @@ 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 topmost or specified patch
+
+    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 (given) 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 +2106,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')
@@ -2560,7 +2629,15 @@ 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')),
+          ('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.commitopts,
+         _('hg qheader [[-e] | [-m TEXT] | [-l FILE]] [PATCH]')),
      "^qimport":
          (qimport,
           [('e', 'existing', None, _('import file in patch directory')),


More information about the Mercurial-devel mailing list