diff tests/test-rollback.t @ 15183:59e8bc22506e

rollback: avoid unsafe rollback when not at tip (issue2998) You can get into trouble if you commit, update back to an older changeset, and then rollback. The update removes your valuable changes from the working dir, then rollback removes them history. Oops: you've just irretrievably lost data running nothing but core Mercurial commands. (More subtly: rollback from a shared clone that was already at an older changeset -- no update required, just rollback from the wrong directory.) The fix assumes that only "commit" transactions have irreplaceable data, and allows rolling back non-commit transactions as always. But when rolling back a commit, check that the working dir is checked out to tip, i.e. the changeset we're about to destroy. If not, abort. You can get back the old (dangerous) behaviour with --force.
author Greg Ward <greg@gerg.ca>
date Fri, 30 Sep 2011 21:58:54 -0400
parents 7c26ce9edbd2
children 0292f88d3b86
line wrap: on
line diff
--- a/tests/test-rollback.t	Fri Sep 30 15:11:19 2011 -0500
+++ b/tests/test-rollback.t	Fri Sep 30 21:58:54 2011 -0400
@@ -83,7 +83,7 @@
   $ hg bookmark bar
   $ cat .hg/undo.branch ; echo
   test
-  $ hg rollback
+  $ hg rollback -f
   repository tip rolled back to revision 1 (undo commit)
   $ hg id -n
   0
@@ -146,3 +146,37 @@
   working directory now based on revision 0
   $ hg id default
   791dd2169706
+
+update to older changeset and then refuse rollback, because
+that would lose data (issue2998)
+  $ cd ../t
+  $ hg -q update
+  $ rm `hg status -un`
+  $ template='{rev}:{node|short}  [{branch}]  {desc|firstline}\n'
+  $ echo 'valuable new file' > b
+  $ echo 'valuable modification' >> a
+  $ hg commit -A -m'a valuable change'
+  adding b
+  $ hg update 0
+  1 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ hg rollback
+  abort: rollback of last commit while not checked out may lose data (use -f to force)
+  [255]
+  $ hg tip -q
+  2:4d9cd3795eea
+  $ hg rollback -f
+  repository tip rolled back to revision 1 (undo commit)
+  $ hg status
+  $ hg log --removed b   # yep, it's gone
+
+same again, but emulate an old client that doesn't write undo.desc
+  $ hg -q update
+  $ echo 'valuable modification redux' >> a
+  $ hg commit -m'a valuable change redux'
+  $ rm .hg/undo.desc
+  $ hg update 0
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ hg rollback
+  rolling back unknown transaction
+  $ cat a
+  a