[PATCH V2 STABLE] strip: do not overwrite existing backup bundles

Laurens Holst laurens.nospam at grauw.nl
Wed Dec 28 11:55:31 CST 2011


# HG changeset patch
# User Laurens Holst <laurens.hg at grauw.nl>
# Date 1325036331 -3600
# Node ID 894516ae07e4353893e69a3bfabffaf07aa38ccf
# Parent  3317d5c34f384c6d1d8440e19980a2261beecc7a
strip: do not overwrite existing backup bundles

I ran into a data loss issue where a strip’s backup overwrote a previous backup
which had more descendants. This happens because the backup file name composed
of just the hash of the first stripped changeset. The issue is fixed by
suffixing the backup file with a - and a number as needed.

diff -r 3317d5c34f38 -r 894516ae07e4 mercurial/repair.py
--- a/mercurial/repair.py	Wed Dec 21 18:20:15 2011 +0100
+++ b/mercurial/repair.py	Wed Dec 28 02:38:51 2011 +0100
@@ -9,7 +9,7 @@
 from mercurial import changegroup, bookmarks, phases
 from mercurial.node import short
 from mercurial.i18n import _
-import os
+import os, itertools
 
 def _bundle(repo, bases, heads, node, suffix, compress=True):
     """create a bundle with the specified revisions as a backup"""
@@ -17,7 +17,17 @@
     backupdir = repo.join("strip-backup")
     if not os.path.isdir(backupdir):
         os.mkdir(backupdir)
-    name = os.path.join(backupdir, "%s-%s.hg" % (short(node), suffix))
+
+    # never overwrite existing bundle
+    for i in itertools.count(0):
+        if i < 1:
+            filename = "%s-%s.hg" % (short(node), suffix)
+        else:
+            filename = "%s-%s-%d.hg" % (short(node), suffix, i)
+        name = os.path.join(backupdir, filename)
+        if not os.path.exists(name):
+            break
+
     if compress:
         bundletype = "HG10BZ"
     else:
diff -r 3317d5c34f38 -r 894516ae07e4 tests/test-mq-strip.t
--- a/tests/test-mq-strip.t	Wed Dec 21 18:20:15 2011 +0100
+++ b/tests/test-mq-strip.t	Wed Dec 28 02:38:51 2011 +0100
@@ -338,6 +338,18 @@
   saved backup bundle to $TESTTMP/test/.hg/strip-backup/*-backup.hg (glob)
   $ restore
 
+not overwriting previous backups
+
+  $ hg up -C -q null
+  $ hg strip 0
+  saved backup bundle to $TESTTMP/test/.hg/strip-backup/*-backup.hg (glob)
+  $ hg unbundle -q .hg/strip-backup/*
+  $ hg strip 1
+  saved backup bundle to $TESTTMP/test/.hg/strip-backup/*-backup.hg (glob)
+  $ hg strip 0
+  saved backup bundle to $TESTTMP/test/.hg/strip-backup/*-backup-2.hg (glob)
+  $ restore
+
 stripping an empty revset
 
   $ hg strip "1 and not 1"
diff -r 3317d5c34f38 -r 894516ae07e4 tests/test-mq.t
--- a/tests/test-mq.t	Wed Dec 21 18:20:15 2011 +0100
+++ b/tests/test-mq.t	Wed Dec 28 02:38:51 2011 +0100
@@ -762,7 +762,7 @@
 
   $ hg strip -f tip
   0 files updated, 0 files merged, 1 files removed, 0 files unresolved
-  saved backup bundle to $TESTTMP/b/.hg/strip-backup/*-backup.hg (glob)
+  saved backup bundle to $TESTTMP/b/.hg/strip-backup/*-backup-2.hg (glob)
 
 
 cd b; hg qrefresh


More information about the Mercurial-devel mailing list