Patchwork [2,of,3] histedit: store backup file before histedit

login
register
mail settings
Submitter Durham Goode
Date April 6, 2015, 2:30 a.m.
Message ID <43bc5bae453a1832b03a.1428287415@dev2000.prn2.facebook.com>
Download mbox | patch
Permalink /patch/8507/
State Superseded
Commit 6c7f563e56688121ba0fa4876f2424a0e3f0d8e1
Headers show

Comments

Durham Goode - April 6, 2015, 2:30 a.m.
# HG changeset patch
# User Durham Goode <durham@fb.com>
# Date 1428140263 25200
#      Sat Apr 04 02:37:43 2015 -0700
# Node ID 43bc5bae453a1832b03ad63385dae200bc260f99
# Parent  0a1ead2c3d2c81db0acd31a2d36d3fe4153aeb0f
histedit: store backup file before histedit

It's possible for the user to delete some of the commits they started with
during a histedit, and aborting the histedit doesn't bring them back. Let's
store a backup bundle so we can always recover the stack of commits from before
they began.

Patch

diff --git a/hgext/histedit.py b/hgext/histedit.py
--- a/hgext/histedit.py
+++ b/hgext/histedit.py
@@ -163,8 +163,10 @@  import sys
 from mercurial import cmdutil
 from mercurial import discovery
 from mercurial import error
+from mercurial import changegroup
 from mercurial import copies
 from mercurial import context
+from mercurial import exchange
 from mercurial import extensions
 from mercurial import hg
 from mercurial import node
@@ -206,6 +208,7 @@  class histeditstate(object):
         self.parentctxnode = parentctxnode
         self.lock = lock
         self.wlock = wlock
+        self.backupfile = None
         if replacements is None:
             self.replacements = []
         else:
@@ -220,18 +223,24 @@  class histeditstate(object):
                 raise
             raise util.Abort(_('no histedit in progress'))
 
-        parentctxnode, rules, keep, topmost, replacements = pickle.load(fp)
+        data = pickle.load(fp)
+        if len(data) == 6:
+            parentctxnode, rules, keep, topmost, replacements, backupfile = data
+        else:
+            parentctxnode, rules, keep, topmost, replacements = data
+            backupfile = None
 
         self.parentctxnode = parentctxnode
         self.rules = rules
         self.keep = keep
         self.topmost = topmost
         self.replacements = replacements
+        self.backupfile = backupfile
 
     def write(self):
         fp = self.repo.vfs('histedit-state', 'w')
         pickle.dump((self.parentctxnode, self.rules, self.keep,
-                     self.topmost, self.replacements), fp)
+                     self.topmost, self.replacements, self.backupfile), fp)
         fp.close()
 
     def clear(self):
@@ -637,6 +646,15 @@  def _histedit(ui, repo, state, *freeargs
         state.read()
         mapping, tmpnodes, leafs, _ntm = processreplacement(state)
         ui.debug('restore wc to old parent %s\n' % node.short(state.topmost))
+
+        # Recover our old commits if necessary
+        if not state.topmost in repo and state.backupfile:
+            backupfile = repo.join(state.backupfile)
+            f = hg.openpath(ui, backupfile)
+            gen = exchange.readbundle(ui, f, backupfile)
+            changegroup.addchangegroup(repo, gen, 'histedit',
+                                       'bundle:' + backupfile)
+
         # check whether we should update away
         parentnodes = [c.node() for c in repo[None].parents()]
         for n in leafs | set([state.parentctxnode]):
@@ -695,6 +713,13 @@  def _histedit(ui, repo, state, *freeargs
         state.topmost = topmost
         state.replacements = replacements
 
+        # Create a backup so we can always abort completely.
+        backupfile = None
+        if not obsolete.isenabled(repo, obsolete.createmarkersopt):
+            backupfile = repair._bundle(repo, [parentctxnode], [topmost], root,
+                                        'histedit')
+        state.backupfile = backupfile
+
     while state.rules:
         state.write()
         action, ha = state.rules.pop(0)
diff --git a/tests/test-histedit-edit.t b/tests/test-histedit-edit.t
--- a/tests/test-histedit-edit.t
+++ b/tests/test-histedit-edit.t
@@ -147,6 +147,34 @@  qnew should fail while we're in the midd
   $ hg cat e
   a
 
+Stripping necessary commits should not break --abort
+
+  $ hg histedit 1a60820cd1f6 --commands - 2>&1 << EOF| fixbundle
+  > edit 1a60820cd1f6 wat
+  > pick a5e1ba2f7afb foobaz
+  > pick b5f70786f9b0 g
+  > EOF
+  0 files updated, 0 files merged, 2 files removed, 0 files unresolved
+  Make changes as needed, you may commit or record as needed now.
+  When you are finished, run hg histedit --continue to resume.
+
+  $ mv .hg/histedit-state .hg/histedit-state.bak
+  $ hg strip -q -r b5f70786f9b0
+  $ mv .hg/histedit-state.bak .hg/histedit-state
+  $ hg histedit --abort
+  adding changesets
+  adding manifests
+  adding file changes
+  added 1 changesets with 1 changes to 3 files
+  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ hg log -r .
+  changeset:   6:b5f70786f9b0
+  tag:         tip
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     f
+  
+
 check histedit_source
 
   $ hg log --debug --rev 5