Patchwork D388: filemerge: extract _maketemp and _makebackup

login
register
mail settings
Submitter phabricator
Date Aug. 14, 2017, 8:57 p.m.
Message ID <differential-rev-PHID-DREV-db2wvqq4to5gwfqnfrv2-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/22983/
State Superseded
Headers show

Comments

phabricator - Aug. 14, 2017, 8:57 p.m.
phillco created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/filemerge.py

CHANGE DETAILS




To: phillco, #hg-reviewers
Cc: mercurial-devel
phabricator - Aug. 31, 2017, 7:13 a.m.
martinvonz added inline comments.

INLINE COMMENTS

> filemerge.py:592-594
> +    In addition to preserving the user's pre-existing modifications to `fcd`
> +    (if any), the backup is used to undo certain a premerge, confirm a merge
> +    changed anything, and determine what line endings the new file should have.

Some words missing and/or too many in here

REPOSITORY
  rHG Mercurial

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

To: phillco, #hg-reviewers
Cc: martinvonz, mercurial-devel
phabricator - Aug. 31, 2017, 7:45 p.m.
phillco added a comment.


  (Fixed trailing space)

REPOSITORY
  rHG Mercurial

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

To: phillco, #hg-reviewers
Cc: martinvonz, mercurial-devel
phabricator - Aug. 31, 2017, 7:52 p.m.
martinvonz added a comment.


  In https://phab.mercurial-scm.org/D388#9515, @phillco wrote:
  
  > (Fixed trailing space)
  
  
  Yep, I saw that in tests and fixed it locally, so you'll probably see divergence when you pull after I've pushed them.

REPOSITORY
  rHG Mercurial

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

To: phillco, #hg-reviewers
Cc: martinvonz, mercurial-devel

Patch

diff --git a/mercurial/filemerge.py b/mercurial/filemerge.py
--- a/mercurial/filemerge.py
+++ b/mercurial/filemerge.py
@@ -598,6 +598,45 @@ 
         "o": " [%s]" % labels[1],
     }
 
+def _makebackup(repo, ui, fcd, premerge):
+    """Makes a backup of the local `fcd` file prior to merging.
+
+    In addition to preserving the user's pre-existing modifications to `fcd`
+    (if any), the backup is used to undo certain a premerge, confirm a merge
+    changed anything, and determine what line endings the new file should have.
+    """
+    if fcd.isabsent():
+        return None
+
+    a = repo.wjoin(fcd.path())
+    back = scmutil.origpath(ui, repo, a)
+    if premerge:
+        util.copyfile(a, back)
+    return back
+
+def _maketempfiles(repo, fcd, fco, fca):
+    """Writes out `fco` and `fca` as temporary files, so an external merge
+    tool may use them.
+
+    `fcd` is returned as-is, by convention, because it currently doubles as both
+    the local version and merge destination.
+    """
+    def temp(prefix, ctx):
+        fullbase, ext = os.path.splitext(ctx.path())
+        pre = "%s~%s." % (os.path.basename(fullbase), prefix)
+        (fd, name) = tempfile.mkstemp(prefix=pre, suffix=ext)
+        data = repo.wwritedata(ctx.path(), ctx.data())
+        f = os.fdopen(fd, pycompat.sysstr("wb"))
+        f.write(data)
+        f.close()
+        return name
+
+    a = repo.wjoin(fcd.path())
+    b = temp("base", fca)
+    c = temp("other", fco)
+
+    return a, b, c
+
 def _filemerge(premerge, repo, mynode, orig, fcd, fco, fca, labels=None):
     """perform a 3-way merge in the working directory
 
@@ -611,16 +650,6 @@ 
     Returns whether the merge is complete, the return value of the merge, and
     a boolean indicating whether the file was deleted from disk."""
 
-    def temp(prefix, ctx):
-        fullbase, ext = os.path.splitext(ctx.path())
-        pre = "%s~%s." % (os.path.basename(fullbase), prefix)
-        (fd, name) = tempfile.mkstemp(prefix=pre, suffix=ext)
-        data = repo.wwritedata(ctx.path(), ctx.data())
-        f = os.fdopen(fd, pycompat.sysstr("wb"))
-        f.write(data)
-        f.close()
-        return name
-
     if not fco.cmp(fcd): # files identical?
         return True, None, False
 
@@ -668,17 +697,8 @@ 
             ui.warn(onfailure % fd)
         return True, 1, False
 
-    a = repo.wjoin(fd)
-    b = temp("base", fca)
-    c = temp("other", fco)
-    if not fcd.isabsent():
-        back = scmutil.origpath(ui, repo, a)
-        if premerge:
-            util.copyfile(a, back)
-    else:
-        back = None
-    files = (a, b, c, back)
-
+    back = _makebackup(repo, ui, fcd, premerge)
+    files = _maketempfiles(repo, fcd, fco, fca) + (back,)
     r = 1
     try:
         markerstyle = ui.config('ui', 'mergemarkers')
@@ -706,8 +726,8 @@ 
     finally:
         if not r and back is not None:
             util.unlink(back)
-        util.unlink(b)
-        util.unlink(c)
+        util.unlink(files[1])
+        util.unlink(files[2])
 
 def _check(r, ui, tool, fcd, files):
     fd = fcd.path()