changeset 37001:3723b42ff953

filemerge: move temp file unlinks to _maketempfiles Differential Revision: https://phab.mercurial-scm.org/D2887
author Kyle Lippincott <spectral@google.com>
date Fri, 19 Jan 2018 19:14:09 -0800
parents 44467a4d472f
children e349ad5cbb71
files mercurial/filemerge.py
diffstat 1 files changed, 9 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/filemerge.py	Fri Mar 16 09:41:21 2018 -0700
+++ b/mercurial/filemerge.py	Fri Jan 19 19:14:09 2018 -0800
@@ -7,6 +7,7 @@
 
 from __future__ import absolute_import
 
+import contextlib
 import os
 import re
 import tempfile
@@ -510,8 +511,8 @@
         return False, 1, None
     unused, unused, unused, back = files
     localpath = _workingpath(repo, fcd)
-    basepath, otherpath = _maketempfiles(repo, fco, fca)
-    try:
+    with _maketempfiles(repo, fco, fca) as temppaths:
+        basepath, otherpath = temppaths
         outpath = ""
         mylabel, otherlabel = labels[:2]
         if len(labels) >= 3:
@@ -549,9 +550,6 @@
         r = ui.system(cmd, cwd=repo.root, environ=env, blockedtag='mergetool')
         repo.ui.debug('merge tool returned: %d\n' % r)
         return True, r, False
-    finally:
-        util.unlink(basepath)
-        util.unlink(otherpath)
 
 def _formatconflictmarker(ctx, template, label, pad):
     """Applies the given template to the ctx, prefixed by the label.
@@ -665,6 +663,7 @@
         # the backup context regardless of where it lives.
         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.
@@ -681,8 +680,11 @@
 
     b = temp("base", fca)
     c = temp("other", fco)
-
-    return b, c
+    try:
+        yield b, c
+    finally:
+        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