diff mercurial/simplemerge.py @ 33849:8b91a4ff23ad

simplemerge: use contexts to read file data from, if passed Differential Revision: https://phab.mercurial-scm.org/D374
author Phil Cohen <phillco@fb.com>
date Sun, 13 Aug 2017 20:06:52 -0700
parents b3571dc0e6b8
children b86fc43e4b73
line wrap: on
line diff
--- a/mercurial/simplemerge.py	Sun Aug 13 20:06:52 2017 -0700
+++ b/mercurial/simplemerge.py	Sun Aug 13 20:06:52 2017 -0700
@@ -410,12 +410,30 @@
 
 def simplemerge(ui, localfile, basefile, otherfile,
                 localctx=None, basectx=None, otherctx=None, repo=None, **opts):
+    """Performs the simplemerge algorithm.
+
+    {local|base|other}ctx are optional. If passed, they (local/base/other) will
+    be read from. You should pass explicit labels in this mode since the default
+    is to use the file paths."""
     def readfile(filename):
         f = open(filename, "rb")
         text = f.read()
         f.close()
         return _verifytext(text, filename, ui, opts)
 
+    def readctx(ctx):
+        if not ctx:
+            return None
+        if not repo:
+            raise error.ProgrammingError('simplemerge: repo must be passed if '
+                                         'using contexts')
+        # `wwritedata` is used to get the post-filter data from `ctx` (i.e.,
+        # what would have been in the working copy). Since merges were run in
+        # the working copy, and thus used post-filter data, we do the same to
+        # maintain behavior.
+        return repo.wwritedata(ctx.path(),
+                               _verifytext(ctx.data(), ctx.path(), ui, opts))
+
     mode = opts.get('mode','merge')
     if mode == 'union':
         name_a = None
@@ -436,9 +454,9 @@
             raise error.Abort(_("can only specify three labels."))
 
     try:
-        localtext = readfile(localfile)
-        basetext = readfile(basefile)
-        othertext = readfile(otherfile)
+        localtext = readctx(localctx) if localctx else readfile(localfile)
+        basetext = readctx(basectx) if basectx else readfile(basefile)
+        othertext = readctx(otherctx) if otherctx else readfile(otherfile)
     except error.Abort:
         return 1