compat: adapt `amend --patch` to the new `patch.extract` API
authorPierre-Yves David <pierre-yves.david@octobus.net>
Tue, 17 Apr 2018 15:45:58 +0200
changeset 3670 1043e9c54355
parent 3669 0407965ae79e
child 3671 c82f0d5740b4
compat: adapt `amend --patch` to the new `patch.extract` API
hgext3rd/evolve/cmdrewrite.py
--- a/hgext3rd/evolve/cmdrewrite.py	Tue Apr 17 15:04:15 2018 +0200
+++ b/hgext3rd/evolve/cmdrewrite.py	Tue Apr 17 15:45:58 2018 +0200
@@ -11,6 +11,7 @@
 
 from __future__ import absolute_import
 
+import contextlib
 import random
 
 from mercurial import (
@@ -226,34 +227,42 @@
     """utility function to use filestore and patchrepo to apply a patch to the
     repository with metadata being extracted from the patch"""
     metadata = patch.extract(ui, fp)
+    if util.safehasattr(metadata, 'get'): # < hg-4.6
+        @contextlib.contextmanager
+        def patchcontext():
+            yield metadata
+        patchcontext = patchcontext()
+    else:
+        patchcontext = metadata
     pold = old.p1()
 
-    # store the metadata from the patch to variables
-    parents = (metadata.get('p1'), metadata.get('p2'))
-    date = metadata.get('date') or old.date()
-    branch = metadata.get('branch') or old.branch()
-    user = metadata.get('user') or old.user()
-    # XXX: we must extract extras from the patchfile too
-    extra = old.extra()
-    message = metadata.get('message') or old.description()
-    store = patch.filestore()
-    fp.seek(0)
-    try:
-        files = set()
-        # beware: next line may raise a PatchError to be handled by the caller
-        # of this function
-        patch.patchrepo(ui, repo, pold, store, fp, 1, '',
-                        files=files, eolmode=None)
+    with patchcontext as metadata:
+        # store the metadata from the patch to variables
+        parents = (metadata.get('p1'), metadata.get('p2'))
+        date = metadata.get('date') or old.date()
+        branch = metadata.get('branch') or old.branch()
+        user = metadata.get('user') or old.user()
+        # XXX: we must extract extras from the patchfile too
+        extra = old.extra()
+        message = metadata.get('message') or old.description()
+        store = patch.filestore()
+        fp.seek(0)
+        try:
+            files = set()
+            # beware: next line may raise a PatchError to be handled by the caller
+            # of this function
+            patch.patchrepo(ui, repo, pold, store, fp, 1, '',
+                            files=files, eolmode=None)
 
-        memctx = context.memctx(repo, parents, message, files=files,
-                                filectxfn=store,
-                                user=user,
-                                date=date,
-                                branch=branch,
-                                extra=extra)
-        newcm = memctx.commit()
-    finally:
-        store.close()
+            memctx = context.memctx(repo, parents, message, files=files,
+                                    filectxfn=store,
+                                    user=user,
+                                    date=date,
+                                    branch=branch,
+                                    extra=extra)
+            newcm = memctx.commit()
+        finally:
+            store.close()
     return newcm
 
 def _writectxmetadata(repo, ctx, fp):