mercurial/filemerge.py
changeset 36999 e349ad5cbb71
parent 36998 3723b42ff953
child 37077 1e30a26a65d0
--- a/mercurial/filemerge.py	Fri Jan 19 19:14:09 2018 -0800
+++ b/mercurial/filemerge.py	Fri Jan 19 19:07:58 2018 -0800
@@ -10,6 +10,7 @@
 import contextlib
 import os
 import re
+import shutil
 import tempfile
 
 from .i18n import _
@@ -668,12 +669,23 @@
     """Writes out `fco` and `fca` as temporary files, 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())
-        pre = "%s~%s." % (os.path.basename(fullbase), prefix)
-        (fd, name) = tempfile.mkstemp(prefix=pre, suffix=ext)
+        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)
+            f = os.fdopen(fd, r"wb")
         data = repo.wwritedata(ctx.path(), ctx.data())
-        f = os.fdopen(fd, r"wb")
         f.write(data)
         f.close()
         return name
@@ -683,8 +695,11 @@
     try:
         yield b, c
     finally:
-        util.unlink(b)
-        util.unlink(c)
+        if tmproot:
+            shutil.rmtree(tmproot)
+        else:
+            util.unlink(b)
+            util.unlink(c)
 
 def _filemerge(premerge, repo, wctx, mynode, orig, fcd, fco, fca, labels=None):
     """perform a 3-way merge in the working directory