transaction: handle missing file in backupentries (instead of using entries)
authorPierre-Yves David <pierre-yves.david@fb.com>
Wed, 05 Nov 2014 01:38:48 +0000
changeset 23278 aa19432764d6
parent 23277 581d3bc03aad
child 23279 e245775f8fd3
transaction: handle missing file in backupentries (instead of using entries) The case where a backup of a missing file was requested was previously handled by the 'entries' list. As the 'backupentries' is about to gain ability to backup files outside of '.hg/store', we want it to be able to handle the missing file too. Reminder: using 'addbackup' on a missing file means that such file needs to be deleted if we rollback the transaction.
mercurial/transaction.py
--- a/mercurial/transaction.py	Wed Nov 05 01:23:40 2014 +0000
+++ b/mercurial/transaction.py	Wed Nov 05 01:38:48 2014 +0000
@@ -44,14 +44,21 @@
 
     backupfiles = []
     for f, b in backupentries:
-        filepath = opener.join(f)
-        backuppath = opener.join(b)
-        try:
-            util.copyfile(backuppath, filepath)
-            backupfiles.append(b)
-        except IOError:
-            report(_("failed to recover %s\n") % f)
-            raise
+        if b:
+            filepath = opener.join(f)
+            backuppath = opener.join(b)
+            try:
+                util.copyfile(backuppath, filepath)
+                backupfiles.append(b)
+            except IOError:
+                report(_("failed to recover %s\n") % f)
+                raise
+        else:
+            try:
+                opener.unlink(f)
+            except (IOError, OSError), inst:
+                if inst.errno != errno.ENOENT:
+                    raise
 
     opener.unlink(journal)
     backuppath = "%s.backupfiles" % journal
@@ -85,6 +92,7 @@
         self.entries = []
         self.map = {}
         # a list of ('path', 'backuppath') entries.
+        # if 'backuppath' is empty, no file existed at backup time
         self._backupentries = []
         self._backupmap = {}
         self.journal = journal
@@ -179,8 +187,7 @@
             backuppath = self.opener.join(backupfile)
             util.copyfiles(filepath, backuppath, hardlink=hardlink)
         else:
-            self.add(file, 0)
-            return
+            backupfile = ''
 
         self._backupentries.append((file, backupfile))
         self._backupmap[file] = len(self._backupentries) - 1
@@ -331,7 +338,8 @@
         if self.opener.isfile(self._backupjournal):
             self.opener.unlink(self._backupjournal)
             for _f, b in self._backupentries:
-                self.opener.unlink(b)
+                if b:
+                    self.opener.unlink(b)
         self._backupentries = []
         self.journal = None
         # run post close action