unlink temporary patch files even when an exception is raised
authorBenoit Boissinot <benoit.boissinot@ens-lyon.org>
Tue, 05 Sep 2006 01:11:14 +0200
changeset 3057 d16b93f4a6ca
parent 3056 6848528f7ebd
child 3058 11e3396e3a2b
unlink temporary patch files even when an exception is raised
mercurial/patch.py
--- a/mercurial/patch.py	Wed Aug 30 13:42:57 2006 -0700
+++ b/mercurial/patch.py	Tue Sep 05 01:11:14 2006 +0200
@@ -227,14 +227,14 @@
     """apply the patch <patchname> to the working directory.
     a list of patched files is returned"""
 
-    (dopatch, gitpatches) = readgitpatch(patchname)
+    # helper function
+    def __patch(patchname):
+        """patch and updates the files and fuzz variables"""
+        files = {}
+        fuzz = False
 
-    files = {}
-    fuzz = False
-    if dopatch:
-        if dopatch == 'filter':
-            patchname = dogitpatch(patchname, gitpatches, cwd=cwd)
-        patcher = util.find_in_path('gpatch', os.environ.get('PATH', ''), 'patch')
+        patcher = util.find_in_path('gpatch', os.environ.get('PATH', ''),
+                                    'patch')
         args = []
         if cwd:
             args.append('-d %s' % util.shellquote(cwd))
@@ -261,14 +261,24 @@
                     ui.warn(pf + '\n')
                     printed_file = True
                 ui.warn(line + '\n')
-            
-        if dopatch == 'filter':
-            os.unlink(patchname)
-
         code = fp.close()
         if code:
             raise util.Abort(_("patch command failed: %s") %
                              util.explain_exit(code)[0])
+        return files, fuzz
+
+    (dopatch, gitpatches) = readgitpatch(patchname)
+
+    if dopatch:
+        if dopatch == 'filter':
+            patchname = dogitpatch(patchname, gitpatches, cwd=cwd)
+        try:
+            files, fuzz = __patch(patchname)
+        finally:
+            if dopatch == 'filter':
+                os.unlink(patchname)
+    else:
+        files, fuzz = {}, False
 
     for gp in gitpatches:
         files[gp.path] = (gp.op, gp)