[Request] [+- ] D12193: filemerge: make `_maketempfiles()` more reusable

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Wed Feb 16 04:09:57 UTC 2022


martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  `_maketempfiles()` is very specialized for its current use. I hope to
  use it also when creating temporary files for input for tools that do
  partial conflict resolution. That'll be possible if the function is
  more generic. Instead of passing in two contexts (for "other" and
  "base") and an optional path (for "local"), let's pass a single list
  of files to make backups for. Even if we don't end up using for
  partial conflict resolution, this is still a simplification (but I do
  have a WIP patch for partial conflict resolution and it is able to
  benefit from this).

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/filemerge.py

CHANGE DETAILS

diff --git a/mercurial/filemerge.py b/mercurial/filemerge.py
--- a/mercurial/filemerge.py
+++ b/mercurial/filemerge.py
@@ -742,20 +742,26 @@
         return False, 1, None
     localpath = _workingpath(repo, fcd)
     args = _toolstr(repo.ui, tool, b"args")
-    localoutputpath = None
+
+    files = [
+        (b"base", fca.path(), fca.decodeddata()),
+        (b"other", fco.path(), fco.decodeddata()),
+    ]
+    outpath = b""
     if b"$output" in args:
+        # read input from backup, write to original
+        outpath = localpath
         localoutputpath = backup.path()
         # Remove the .orig to make syntax-highlighting more likely.
         if localoutputpath.endswith(b'.orig'):
             localoutputpath, ext = os.path.splitext(localoutputpath)
+        localdata = util.readfile(localpath)
+        files.append((b"local", localoutputpath, localdata))
 
-    with _maketempfiles(
-        fco,
-        fca,
-        localoutputpath,
-    ) as temppaths:
-        basepath, otherpath, localoutputpath = temppaths
-        outpath = b""
+    with _maketempfiles(files) as temppaths:
+        basepath, otherpath = temppaths[:2]
+        if len(temppaths) == 3:
+            localpath = temppaths[2]
 
         def format_label(input):
             if input.label_detail:
@@ -777,10 +783,6 @@
         }
         ui = repo.ui
 
-        if b"$output" in args:
-            # read input from backup, write to original
-            outpath = localpath
-            localpath = localoutputpath
         replace = {
             b'local': localpath,
             b'base': basepath,
@@ -915,10 +917,9 @@
 
 
 @contextlib.contextmanager
-def _maketempfiles(fco, fca, localpath):
-    """Writes out `fco` and `fca` as temporary files, and (if localpath is not
-    None) copies `localpath` to another temporary file, so an external merge
-    tool may use them.
+def _maketempfiles(files):
+    """Creates a temporary file for each (prefix, path, data) tuple in `files`,
+    so an external merge tool may use them.
     """
     tmproot = pycompat.mkdtemp(prefix=b'hgmerge-')
 
@@ -931,18 +932,11 @@
         util.writefile(name, data)
         return name
 
-    def tempfromcontext(prefix, ctx):
-        return maketempfrompath(prefix, ctx.path(), ctx.decodeddata())
-
-    b = tempfromcontext(b"base", fca)
-    c = tempfromcontext(b"other", fco)
-    d = localpath
-    if localpath is not None:
-        data = util.readfile(localpath)
-        d = maketempfrompath(b"local", localpath, data)
-
+    temp_files = []
+    for prefix, path, data in files:
+        temp_files.append(maketempfrompath(prefix, path, data))
     try:
-        yield b, c, d
+        yield temp_files
     finally:
         shutil.rmtree(tmproot)
 



To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mercurial-scm.org/pipermail/mercurial-patches/attachments/20220216/e36ac854/attachment-0001.html>


More information about the Mercurial-patches mailing list