Patchwork D2887: filemerge: move temp file unlinks to _maketempfiles

login
register
mail settings
Submitter phabricator
Date March 17, 2018, 12:01 a.m.
Message ID <differential-rev-PHID-DREV-rcx5wxov5ls4kpyake7j-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/29577/
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.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/filemerge.py

CHANGE DETAILS




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

Patch

diff --git a/mercurial/filemerge.py b/mercurial/filemerge.py
--- a/mercurial/filemerge.py
+++ b/mercurial/filemerge.py
@@ -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