D3305: cmdutil: pass in parsed patch to tryimportone() (API)

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Fri Apr 13 06:27:19 UTC 2018


indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3305

AFFECTED FILES
  mercurial/cmdutil.py
  mercurial/commands.py

CHANGE DETAILS

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -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')
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1343,16 +1343,17 @@
 # - 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
     wrap this part of the import logic.
 
     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



To: indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list