[PATCH 2 of 3 remove-makememctx V2] context: add convenience method for returning a memfilectx from a patch

Sean Farley sean at farley.io
Fri Jun 9 17:20:25 EDT 2017

# HG changeset patch
# User Sean Farley <sean at farley.io>
# Date 1497040753 25200
#      Fri Jun 09 13:39:13 2017 -0700
# Branch remove-makememctx
# Node ID 06fb38543b73062f9f6b5190e98ca000e2a34636
# Parent  dfe7dc826d3b1c6b0c933cc74c9ad14042f39877
context: add convenience method for returning a memfilectx from a patch

This is mostly a copy of what makememctx does but refactored to make it
behave more like our other convenience methods.

diff --git a/mercurial/context.py b/mercurial/context.py
index 5960d02..7d37c23 100644
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -2058,10 +2058,26 @@ def memfilefromctx(ctx):
                           islink=fctx.islink(), isexec=fctx.isexec(),
                           copied=copied, memctx=memctx)
     return getfilectx
+def memfilefrompatch(patchstore):
+    """Given a patch (e.g. patchstore object) return a memfilectx
+    This is a convenience method for building a memctx based on a patchstore.
+    """
+    def getfilectx(repo, memctx, path):
+        data, mode, copied = patchstore.getfile(path)
+        if data is None:
+            return None
+        islink, isexec = mode
+        return memfilectx(repo, path, data, islink=islink,
+                          isexec=isexec, copied=copied,
+                          memctx=memctx)
+    return getfilectx
 class memctx(committablectx):
     """Use memctx to perform in-memory commits via localrepo.commitctx().
     Revision information is supplied at initialization time while
     related files data and is made available through a callback

More information about the Mercurial-devel mailing list