changeset 37620:fd1dd79cff20

cmdutil: pass in parsed patch to tryimportone() (API) Previously, we parsed the patch in tryimportone(). This assumes the input is in a patch format that needs to be parsed. We want to support feeding in data from other formats. So let's let the caller handle the parsing. One wonky thing about patch parsing is that patch.extract() creates a temp file to hold the diffs and it is up to tryimportone() to unlink that temp file. I'll improve this in a subsequent commit. Differential Revision: https://phab.mercurial-scm.org/D3305
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 12 Apr 2018 23:06:27 -0700
parents 68132a95df31
children 5537d8f5e989
files mercurial/cmdutil.py mercurial/commands.py
diffstat 2 files changed, 18 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/cmdutil.py	Thu Apr 12 20:42:42 2018 -0700
+++ b/mercurial/cmdutil.py	Thu Apr 12 23:06:27 2018 -0700
@@ -1343,7 +1343,7 @@
 # - ctx: the changectx created by import.
 extrapostimportmap = {}
 
-def tryimportone(ui, repo, hunk, parents, opts, msgs, updatefunc):
+def tryimportone(ui, repo, patchdata, parents, opts, msgs, updatefunc):
     """Utility function used by commands.import to import a single patch
 
     This function is explicitly defined here to help the evolve extension to
@@ -1352,7 +1352,8 @@
     The API is currently a bit ugly because it a simple code translation from
     the import command. Feel free to make it better.
 
-    :hunk: a patch (as a binary string)
+    :patchdata: a dictionary containing parsed patch data (such as from
+                ``patch.extract()``)
     :parents: nodes that will be parent of the created commit
     :opts: the full dict of option passed to the import command
     :msgs: list to save commit message to.
@@ -1362,15 +1363,15 @@
     """
     # avoid cycle context -> subrepo -> cmdutil
     from . import context
-    extractdata = patch.extract(ui, hunk)
-    tmpname = extractdata.get('filename')
-    message = extractdata.get('message')
-    user = opts.get('user') or extractdata.get('user')
-    date = opts.get('date') or extractdata.get('date')
-    branch = extractdata.get('branch')
-    nodeid = extractdata.get('nodeid')
-    p1 = extractdata.get('p1')
-    p2 = extractdata.get('p2')
+
+    tmpname = patchdata.get('filename')
+    message = patchdata.get('message')
+    user = opts.get('user') or patchdata.get('user')
+    date = opts.get('date') or patchdata.get('date')
+    branch = patchdata.get('branch')
+    nodeid = patchdata.get('nodeid')
+    p1 = patchdata.get('p1')
+    p2 = patchdata.get('p2')
 
     nocommit = opts.get('no_commit')
     importbranch = opts.get('import_branch')
@@ -1462,7 +1463,7 @@
                                              **pycompat.strkwargs(opts))
                 extra = {}
                 for idfunc in extrapreimport:
-                    extrapreimportmap[idfunc](repo, extractdata, extra, opts)
+                    extrapreimportmap[idfunc](repo, patchdata, extra, opts)
                 overrides = {}
                 if partial:
                     overrides[('ui', 'allowemptycommit')] = True
--- a/mercurial/commands.py	Thu Apr 12 20:42:42 2018 -0700
+++ b/mercurial/commands.py	Thu Apr 12 23:06:27 2018 -0700
@@ -3089,9 +3089,11 @@
 
             haspatch = False
             for hunk in patch.split(patchfile):
-                (msg, node, rej) = cmdutil.tryimportone(ui, repo, hunk,
-                                                        parents, opts,
-                                                        msgs, hg.clean)
+                patchdata = patch.extract(ui, hunk)
+
+                msg, node, rej = cmdutil.tryimportone(ui, repo, patchdata,
+                                                      parents, opts,
+                                                      msgs, hg.clean)
                 if msg:
                     haspatch = True
                     ui.note(msg + '\n')