Patchwork D2889: filemerge: make the 'local' path match the format that 'base' and 'other' use

login
register
mail settings
Submitter phabricator
Date March 17, 2018, 12:01 a.m.
Message ID <differential-rev-PHID-DREV-icwvftntqee5ngsksb4w-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/29579/
State Superseded
Headers show

Comments

phabricator - March 17, 2018, 12:01 a.m.
spectral created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  If we pass a separate '$output' arg to the merge tool, we produce four files:
  local, base, other, and output.  In this situation, 'output' will be the
  original filename, 'base' and 'other' are temporary files, and previously
  'local' would be the backup file (so if 'output' was foo.txt, 'local' would be
  foo.txt.orig).
  
  This change makes it so that 'local' follows the same pattern as 'base' and
  'other' - it will be a temporary file either in the
  `experimental.mergetempdirprefix`-controlled directory with a name like
  foo~local.txt, or in the normal system-wide temp dir with a name like
  foo~local.RaNd0m.txt.
  
  For the cases where the merge tool does not use an '$output' arg, 'local' is
  still the destination filename, and 'base' and 'other' are unchanged.
  
  The hope is that this is much easier for people to reason about; rather than
  having a tool like Meld pop up with three panes, one of them with the filename
  "foo.txt.orig", one with the filename "foo.txt", and one with
  "foo~other.StuFf2.txt", we can (when the merge temp dir stuff is enabled) make
  it show up as "foo~local.txt", "foo.txt" and "foo~other.txt", respectively.
  
  This also opens the door to future customization, such as getting the
  operation-provided labels and a hash prefix into the filenames (so we see
  something like "foo~dest.abc123", "foo.txt", and "foo~src.d4e5f6").

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/filemerge.py
  tests/test-merge-tools.t

CHANGE DETAILS




To: spectral, #hg-reviewers
Cc: mercurial-devel
phabricator - March 19, 2018, 2:33 p.m.
yuja requested changes to this revision.
yuja added a comment.
This revision now requires changes to proceed.


  Not reviewed yet, but this can't be applied cleanly. Can you rebase?

REPOSITORY
  rHG Mercurial

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

To: spectral, #hg-reviewers, yuja
Cc: yuja, mercurial-devel
phabricator - March 19, 2018, 6:18 p.m.
spectral added a comment.


  In https://phab.mercurial-scm.org/D2889#46482, @yuja wrote:
  
  > Not reviewed yet, but this can't be applied cleanly. Can you rebase?
  
  
  This series should rebase cleanly onto https://phab.mercurial-scm.org/rHGa4a95bd7158de9e932ccf5e8d60095609fbe9994 (which was accepted by pulkit separately from the rest of the series).  I've rebase these three remaining ones and pushed.

REPOSITORY
  rHG Mercurial

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

To: spectral, #hg-reviewers, yuja
Cc: yuja, mercurial-devel
phabricator - March 19, 2018, 6:22 p.m.
spectral requested review of this revision.
spectral added a comment.


  Ah, there's a "Request Review" that I'd never noticed before, choosing that to hopefully get this out of "Requires Revision"

REPOSITORY
  rHG Mercurial

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

To: spectral, #hg-reviewers, yuja
Cc: yuja, mercurial-devel
phabricator - March 21, 2018, 4:23 a.m.
yuja added a comment.


  Can't apply.
  
    % hg up -C a4a95bd7
    2 files updated, 0 files merged, 0 files removed, 0 files unresolved
    
    % hg pimp D2889
    devel-warn: accessing unregistered config item: 'phabricator.batchsize' at: /home/yuya/work/hghacks/mercurial/mercurial/ui.py:624 (configwith)
    devel-warn: accessing unregistered config item: 'phabricator.url' at: /home/yuya/work/hghacks/mercurial/contrib/phabricator.py:124 (readurltoken)
    devel-warn: accessing unregistered config item: 'phabricator.token' at: /home/yuya/work/hghacks/mercurial/contrib/phabricator.py:111 (readlegacytoken)
    phabricator.token is deprecated - please migrate to the phabricator.auth section.
    devel-warn: accessing unregistered config item: 'phabricator.curlcmd' at: /home/yuya/work/hghacks/mercurial/contrib/phabricator.py:162 (callconduit)
    applying patch from stdin
    devel-warn: accessing unregistered config item: 'phabricator.url' at: /home/yuya/work/hghacks/mercurial/contrib/phabricator.py:124 (readurltoken)
    devel-warn: accessing unregistered config item: 'phabricator.token' at: /home/yuya/work/hghacks/mercurial/contrib/phabricator.py:111 (readlegacytoken)
    devel-warn: accessing unregistered config item: 'phabricator.curlcmd' at: /home/yuya/work/hghacks/mercurial/contrib/phabricator.py:162 (callconduit)
    devel-warn: accessing unregistered config item: 'phabricator.url' at: /home/yuya/work/hghacks/mercurial/contrib/phabricator.py:124 (readurltoken)
    devel-warn: accessing unregistered config item: 'phabricator.token' at: /home/yuya/work/hghacks/mercurial/contrib/phabricator.py:111 (readlegacytoken)
    devel-warn: accessing unregistered config item: 'phabricator.curlcmd' at: /home/yuya/work/hghacks/mercurial/contrib/phabricator.py:162 (callconduit)
    patching file tests/test-merge-tools.t
    Hunk #2 FAILED at 1599
    1 out of 2 hunks FAILED -- saving rejects to file tests/test-merge-tools.t.rej
    patching file mercurial/filemerge.py
    Hunk #1 FAILED at 511
    Hunk #3 FAILED at 663
    2 out of 3 hunks FAILED -- saving rejects to file mercurial/filemerge.py.rej
    abort: patch failed to apply

REPOSITORY
  rHG Mercurial

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

To: spectral, #hg-reviewers, yuja
Cc: yuja, mercurial-devel

Patch

diff --git a/tests/test-merge-tools.t b/tests/test-merge-tools.t
--- a/tests/test-merge-tools.t
+++ b/tests/test-merge-tools.t
@@ -1585,7 +1585,7 @@ 
   $ hg update -q -C 2
   $ hg merge -y -r tip --tool echo --config merge-tools.echo.args='$base $local $other $output'
   merging f and f.txt to f.txt
-  */f~base.?????? $TESTTMP/f.txt.orig */f~other.??????.txt $TESTTMP/f.txt (glob)
+  */f~base.?????? */f~local.??????.txt */f~other.??????.txt $TESTTMP/f.txt (glob)
   0 files updated, 1 files merged, 0 files removed, 0 files unresolved
   (branch merge, don't forget to commit)
 
@@ -1600,7 +1600,7 @@ 
   >    --config merge-tools.echo.args='$base $local $other $output' \
   >    --config experimental.mergetempdirprefix=$TESTTMP/hgmerge.
   merging f and f.txt to f.txt
-  $TESTTMP/hgmerge.??????/f~base $TESTTMP/f.txt.orig $TESTTMP/hgmerge.??????/f~other.txt $TESTTMP/f.txt (glob)
+  $TESTTMP/hgmerge.??????/f~base $TESTTMP/hgmerge.??????/f~local.txt $TESTTMP/hgmerge.??????/f~other.txt $TESTTMP/f.txt (glob)
   0 files updated, 1 files merged, 0 files removed, 0 files unresolved
   (branch merge, don't forget to commit)
 Check that debugpicktool examines which merge tool is chosen for
diff --git a/mercurial/filemerge.py b/mercurial/filemerge.py
--- a/mercurial/filemerge.py
+++ b/mercurial/filemerge.py
@@ -512,8 +512,11 @@ 
         return False, 1, None
     unused, unused, unused, back = files
     localpath = _workingpath(repo, fcd)
-    with _maketempfiles(repo, fco, fca) as temppaths:
-        basepath, otherpath = temppaths
+    args = _toolstr(repo.ui, tool, "args")
+
+    with _maketempfiles(repo, fco, fca, repo.wvfs.join(back.path()),
+                        "$output" in args) as temppaths:
+        basepath, otherpath, localoutputpath = temppaths
         outpath = ""
         mylabel, otherlabel = labels[:2]
         if len(labels) >= 3:
@@ -533,11 +536,10 @@ 
                }
         ui = repo.ui
 
-        args = _toolstr(ui, tool, "args")
         if "$output" in args:
             # read input from backup, write to original
             outpath = localpath
-            localpath = repo.wvfs.join(back.path())
+            localpath = localoutputpath
         replace = {'local': localpath, 'base': basepath, 'other': otherpath,
                    'output': outpath, 'labellocal': mylabel,
                    'labelother': otherlabel, 'labelbase': baselabel}
@@ -665,41 +667,59 @@ 
         return context.arbitraryfilectx(back, repo=repo)
 
 @contextlib.contextmanager
-def _maketempfiles(repo, fco, fca):
-    """Writes out `fco` and `fca` as temporary files, so an external merge
-    tool may use them.
+def _maketempfiles(repo, fco, fca, localpath, uselocalpath):
+    """Writes out `fco` and `fca` as temporary files, and (if not None) copies
+    `localpath` to another temporary file, so an external merge tool may use
+    them.
     """
     tmproot = None
     tmprootprefix = repo.ui.config('experimental', 'mergetempdirprefix')
     if tmprootprefix:
         tmproot = tempfile.mkdtemp(prefix=tmprootprefix)
 
-    def temp(prefix, ctx):
-        fullbase, ext = os.path.splitext(ctx.path())
+    def maketempfrompath(prefix, path):
+        fullbase, ext = os.path.splitext(path)
         pre = "%s~%s" % (os.path.basename(fullbase), prefix)
         if tmproot:
             name = os.path.join(tmproot, pre)
             if ext:
                 name += ext
             f = open(name, r"wb")
         else:
-            (fd, name) = tempfile.mkstemp(prefix=pre + '.', suffix=ext)
+            fd, name = tempfile.mkstemp(prefix=pre + '.', suffix=ext)
             f = os.fdopen(fd, r"wb")
+        return f, name
+
+    def tempfromcontext(prefix, ctx):
+        f, name = maketempfrompath(prefix, ctx.path())
         data = repo.wwritedata(ctx.path(), ctx.data())
         f.write(data)
         f.close()
         return name
 
-    b = temp("base", fca)
-    c = temp("other", fco)
+    b = tempfromcontext("base", fca)
+    c = tempfromcontext("other", fco)
+    d = localpath
+    if uselocalpath:
+        if d.endswith('.orig'):
+            d, _ = os.path.splitext(d)
+        f, d = maketempfrompath("local", d)
+        with open(localpath, 'rb') as src:
+            f.write(src.read())
+        f.close()
+
     try:
-        yield b, c
+        yield b, c, d
     finally:
         if tmproot:
             shutil.rmtree(tmproot)
         else:
             util.unlink(b)
             util.unlink(c)
+            # if not uselocalpath, d is the 'orig'/backup file which we
+            # shouldn't delete.
+            if d and uselocalpath:
+                util.unlink(d)
 
 def _filemerge(premerge, repo, wctx, mynode, orig, fcd, fco, fca, labels=None):
     """perform a 3-way merge in the working directory