Patchwork [3,of,3,V2] histedit: delete histedit statefile on any exception during abort

login
register
mail settings
Submitter Christian Delahousse
Date Oct. 9, 2015, 6:11 p.m.
Message ID <c5b68adb77fe2a2226bb.1444414294@dev4253.prn1.facebook.com>
Download mbox | patch
Permalink /patch/10919/
State Accepted
Headers show

Comments

Christian Delahousse - Oct. 9, 2015, 6:11 p.m.
# HG changeset patch
# User Christian Delahousse <cdelahousse@fb.com>
# Date 1444088685 25200
#      Mon Oct 05 16:44:45 2015 -0700
# Node ID c5b68adb77fe2a2226bb7213972138eee5ff0034
# Parent  3f0d7431bd305e72b544b87a4eed5515a4d34666
histedit: delete histedit statefile on any exception during abort

When an user aborts a histedit, many things could go wrong. At a minimum, after
a histedit abort failure, their repository should be out of that state. We've
found situations where the user could not exit the histedit state without
manually deleting the histedit state file. This patch ensures that if any
exception happens during an abort, the histedit statefile will be deleted so
that users are out of the histedit state and can at least manually get the repo
back to a workable condition.
Augie Fackler - Oct. 9, 2015, 6:34 p.m.
On Fri, Oct 09, 2015 at 11:11:34AM -0700, Christian Delahousse wrote:
> # HG changeset patch
> # User Christian Delahousse <cdelahousse@fb.com>
> # Date 1444088685 25200
> #      Mon Oct 05 16:44:45 2015 -0700
> # Node ID c5b68adb77fe2a2226bb7213972138eee5ff0034
> # Parent  3f0d7431bd305e72b544b87a4eed5515a4d34666
> histedit: delete histedit statefile on any exception during abort

queued thanks

>
> When an user aborts a histedit, many things could go wrong. At a minimum, after
> a histedit abort failure, their repository should be out of that state. We've
> found situations where the user could not exit the histedit state without
> manually deleting the histedit state file. This patch ensures that if any
> exception happens during an abort, the histedit statefile will be deleted so
> that users are out of the histedit state and can at least manually get the repo
> back to a workable condition.
>
> diff --git a/hgext/histedit.py b/hgext/histedit.py
> --- a/hgext/histedit.py
> +++ b/hgext/histedit.py
> @@ -812,26 +812,35 @@
>          state.write()
>          return
>      elif goal == 'abort':
> -        state.read()
> -        tmpnodes, leafs = newnodestoabort(state)
> -        ui.debug('restore wc to old parent %s\n' % node.short(state.topmost))
> +        try:
> +            state.read()
> +            tmpnodes, leafs = newnodestoabort(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)
> -            os.remove(backupfile)
> +            # 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)
> +                os.remove(backupfile)
>
> -        # check whether we should update away
> -        if repo.unfiltered().revs('parents() and (%n  or %ln::)',
> -                                  state.parentctxnode, leafs | tmpnodes):
> -            hg.clean(repo, state.topmost)
> -        cleanupnode(ui, repo, 'created', tmpnodes)
> -        cleanupnode(ui, repo, 'temp', leafs)
> -        state.clear()
> +            # check whether we should update away
> +            if repo.unfiltered().revs('parents() and (%n  or %ln::)',
> +                                    state.parentctxnode, leafs | tmpnodes):
> +                hg.clean(repo, state.topmost)
> +            cleanupnode(ui, repo, 'created', tmpnodes)
> +            cleanupnode(ui, repo, 'temp', leafs)
> +        except Exception:
> +            if state.inprogress():
> +                ui.warn(_('warning: encountered an exception during histedit '
> +                    '--abort; the repository may not have been completely '
> +                    'cleaned up\n'))
> +            raise
> +        finally:
> +                state.clear()
>          return
>      else:
>          cmdutil.checkunfinished(repo)
> diff --git a/tests/test-histedit-arguments.t b/tests/test-histedit-arguments.t
> --- a/tests/test-histedit-arguments.t
> +++ b/tests/test-histedit-arguments.t
> @@ -324,3 +324,25 @@
>    |
>    o  0:6058cbb6cfd7 one
>
> +
> +Test that abort fails gracefully on exception
> +----------------------------------------------
> +  $ hg histedit . -q --commands - << EOF
> +  > edit 8fda0c726bf2 6 x
> +  > EOF
> +  Make changes as needed, you may commit or record as needed now.
> +  When you are finished, run hg histedit --continue to resume.
> +  [1]
> +Corrupt histedit state file
> +  $ sed 's/8fda0c726bf2/123456789012/' .hg/histedit-state > ../corrupt-histedit
> +  $ mv ../corrupt-histedit .hg/histedit-state
> +  $ hg histedit --abort
> +  warning: encountered an exception during histedit --abort; the repository may not have been completely cleaned up
> +  abort: No such file or directory: * (glob)
> +  [255]
> +Histedit state has been exited
> +  $ hg summary -q
> +  parent: 5:63379946892c
> +  commit: 1 added, 1 unknown (new branch head)
> +  update: 4 new changesets (update)
> +
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> https://selenic.com/mailman/listinfo/mercurial-devel

Patch

diff --git a/hgext/histedit.py b/hgext/histedit.py
--- a/hgext/histedit.py
+++ b/hgext/histedit.py
@@ -812,26 +812,35 @@ 
         state.write()
         return
     elif goal == 'abort':
-        state.read()
-        tmpnodes, leafs = newnodestoabort(state)
-        ui.debug('restore wc to old parent %s\n' % node.short(state.topmost))
+        try:
+            state.read()
+            tmpnodes, leafs = newnodestoabort(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)
-            os.remove(backupfile)
+            # 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)
+                os.remove(backupfile)
 
-        # check whether we should update away
-        if repo.unfiltered().revs('parents() and (%n  or %ln::)',
-                                  state.parentctxnode, leafs | tmpnodes):
-            hg.clean(repo, state.topmost)
-        cleanupnode(ui, repo, 'created', tmpnodes)
-        cleanupnode(ui, repo, 'temp', leafs)
-        state.clear()
+            # check whether we should update away
+            if repo.unfiltered().revs('parents() and (%n  or %ln::)',
+                                    state.parentctxnode, leafs | tmpnodes):
+                hg.clean(repo, state.topmost)
+            cleanupnode(ui, repo, 'created', tmpnodes)
+            cleanupnode(ui, repo, 'temp', leafs)
+        except Exception:
+            if state.inprogress():
+                ui.warn(_('warning: encountered an exception during histedit '
+                    '--abort; the repository may not have been completely '
+                    'cleaned up\n'))
+            raise
+        finally:
+                state.clear()
         return
     else:
         cmdutil.checkunfinished(repo)
diff --git a/tests/test-histedit-arguments.t b/tests/test-histedit-arguments.t
--- a/tests/test-histedit-arguments.t
+++ b/tests/test-histedit-arguments.t
@@ -324,3 +324,25 @@ 
   |
   o  0:6058cbb6cfd7 one
   
+
+Test that abort fails gracefully on exception
+----------------------------------------------
+  $ hg histedit . -q --commands - << EOF
+  > edit 8fda0c726bf2 6 x
+  > EOF
+  Make changes as needed, you may commit or record as needed now.
+  When you are finished, run hg histedit --continue to resume.
+  [1]
+Corrupt histedit state file
+  $ sed 's/8fda0c726bf2/123456789012/' .hg/histedit-state > ../corrupt-histedit
+  $ mv ../corrupt-histedit .hg/histedit-state
+  $ hg histedit --abort
+  warning: encountered an exception during histedit --abort; the repository may not have been completely cleaned up
+  abort: No such file or directory: * (glob)
+  [255]
+Histedit state has been exited
+  $ hg summary -q
+  parent: 5:63379946892c 
+  commit: 1 added, 1 unknown (new branch head)
+  update: 4 new changesets (update)
+