[PATCH 2 of 2 RFC] import: populate extras from the patch headers (issue4544)

Matt Harbison mharbison72 at gmail.com
Sun Dec 20 21:52:27 CST 2015


# HG changeset patch
# User Matt Harbison <matt_harbison at yahoo.com>
# Date 1450663284 18000
#      Sun Dec 20 21:01:24 2015 -0500
# Node ID 7a92182315d3ade8b736687eb0aafd3511c27709
# Parent  eff20351cc9b29232493885887e82576f772d1b6
import: populate extras from the patch headers (issue4544)

Since the individual keys aren't registered, the lines with the extras prefix
needs to be handled specially, like a couple of existing headers.

Do we maybe need a switch to enable this (and/or export)?  I'm guessing most
devs amend before submitting to this project, and picking up those amend
attributes doesn't seem useful.  (Though it isn't necessarily harmful either.)

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -827,9 +827,14 @@
     if runfn:
         return runfn()
 
+def _importextras(repo, patchdata, extra, opts):
+    for k, v in patchdata.iteritems():
+        if k.startswith('HgExtra.'):
+            extra[k[8:]] = v
+
 ## facility to let extension process additional data into an import patch
 # list of identifier to be executed in order
-extrapreimport = []  # run before commit
+extrapreimport = ['hgextra']  # run before commit
 extrapostimport = [] # run after commit
 # mapping from identifier to actual import function
 #
@@ -842,7 +847,9 @@
 # XXX ideally, we would just pass an ctx ready to be computed, that would allow
 # mutation of in memory commit and more. Feel free to rework the code to get
 # there.
-extrapreimportmap = {}
+extrapreimportmap = {
+    'hgextra': _importextras
+}
 # 'postimport' are run after the commit is made and are provided the following
 # argument:
 # - ctx: the changectx created by import.
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -239,6 +239,12 @@
                             ui.debug('From: %s\n' % data['user'])
                         elif line.startswith("# Parent "):
                             parents.append(line[9:].lstrip())
+                        elif line.startswith('# HgExtra.'):
+                            kv = line[2:].split(' ', 1)
+                            if len(kv) != 2:
+                                raise error.Abort(_('invalid HgExtra: %s')
+                                                  % kv[0])
+                            data[kv[0]] = kv[1].strip()
                         elif line.startswith("# "):
                             for header, key in patchheadermap:
                                 prefix = '# %s ' % header
diff --git a/tests/test-graft.t b/tests/test-graft.t
--- a/tests/test-graft.t
+++ b/tests/test-graft.t
@@ -335,8 +335,40 @@
   2
   
   
+  $ hg export -r 13 -o export.patch
+  $ cat export.patch
+  # HG changeset patch
+  # User foo
+  # Date 0 0
+  #      Thu Jan 01 00:00:00 1970 +0000
+  # Node ID 7a4785234d87ec1aa420ed6b11afe40fa73e12a9
+  # Parent  b592ea63bb0c19a6c5c44685ee29a2284f9f1b8f
+  # HgExtra.intermediate-source ef0ef43d49e79e81ddafdc7997401ba0041efc82
+  # HgExtra.source 5c095ad7e90f871700f02dd1fa5012cb4498a2d4
+  2
+  
+  diff -r b592ea63bb0c -r 7a4785234d87 a
+  --- a/a	Thu Jan 01 00:00:00 1970 +0000
+  +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
+  @@ -1,1 +0,0 @@
+  -a
+  diff -r b592ea63bb0c -r 7a4785234d87 b
+  --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/b	Thu Jan 01 00:00:00 1970 +0000
+  @@ -0,0 +1,1 @@
+  +a
+  $ hg up -qr '13^'
+  $ hg import -u xyz export.patch
+  applying export.patch
+  $ hg log -r . -T "{extras % '{extra}\n'}"
+  branch=default
+  intermediate-source=ef0ef43d49e79e81ddafdc7997401ba0041efc82
+  source=5c095ad7e90f871700f02dd1fa5012cb4498a2d4
+  $ hg rollback -q
+  $ rm export.patch
+
 Disallow grafting an already grafted cset onto its original branch
-  $ hg up -q 6
+  $ hg up -Cq 6
   $ hg graft 7
   skipping already grafted revision 7:ef0ef43d49e7 (was grafted from 2:5c095ad7e90f)
   [255]
diff --git a/tests/test-transplant.t b/tests/test-transplant.t
--- a/tests/test-transplant.t
+++ b/tests/test-transplant.t
@@ -170,6 +170,38 @@
   date:        Thu Jan 01 00:00:02 1970 +0000
   summary:     b3
   
+  $ hg log -r 7 -T "{extras % '{extra}\n'}"
+  branch=default
+  transplant_source=\xa52Q\xcd\xf7\x17g\x9d\x19\x07\xb2\x89\xf9\x91SK\xe0\\\x99z
+  $ hg export -r 7 -o export.patch
+  $ cat export.patch
+  # HG changeset patch
+  # User test
+  # Date 2 0
+  #      Thu Jan 01 00:00:02 1970 +0000
+  # Node ID ffd6818a39750e29092b630db947d103bcaf11c8
+  # Parent  539f377d78dfe142e335dff2696d70fd5cbdffb8
+  # HgExtra.transplant_source \xa52Q\xcd\xf7\x17g\x9d\x19\x07\xb2\x89\xf9\x91SK\xe0\\\x99z (esc)
+  b3
+  
+  diff -r 539f377d78df -r ffd6818a3975 b3
+  --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/b3	Thu Jan 01 00:00:02 1970 +0000
+  @@ -0,0 +1,1 @@
+  +b3
+  $ hg up -q '7^'
+  $ hg import -u xyz export.patch
+  applying export.patch
+  $ hg log -r . -T "{extras % '{extra}\n'}"
+  branch=default
+  transplant_source=\xa52Q\xcd\xf7\x17g\x9d\x19\x07\xb2\x89\xf9\x91SK\xe0\\\x99z
+  $ hg rollback -q
+  $ hg import --bypass -u xyz export.patch
+  applying export.patch
+  $ hg log -r tip -T "{extras % '{extra}\n'}"
+  branch=default
+  $ hg rollback -q
+  $ rm export.patch
 
 back to the original dir
   $ cd ../rebase


More information about the Mercurial-devel mailing list