hgext/shelve.py
changeset 26522 10f14bb22950
parent 26520 46dec89fe888
child 26523 1d23bf6cd90a
--- a/hgext/shelve.py	Thu Oct 08 01:41:30 2015 +0900
+++ b/hgext/shelve.py	Thu Oct 08 01:41:30 2015 +0900
@@ -190,6 +190,35 @@
                 if err.errno != errno.ENOENT:
                     raise
 
+def _aborttransaction(repo):
+    '''Abort current transaction for shelve/unshelve, but keep dirstate
+    '''
+    backupname = 'dirstate.shelve'
+    dirstatebackup = None
+    try:
+        # create backup of (un)shelved dirstate, because aborting transaction
+        # should restore dirstate to one at the beginning of the
+        # transaction, which doesn't include the result of (un)shelving
+        fp = repo.vfs.open(backupname, "w")
+        dirstatebackup = backupname
+        # clearing _dirty/_dirtypl of dirstate by _writedirstate below
+        # is unintentional. but it doesn't cause problem in this case,
+        # because no code path refers them until transaction is aborted.
+        repo.dirstate._writedirstate(fp) # write in-memory changes forcibly
+
+        tr = repo.currenttransaction()
+        tr.abort()
+
+        # TODO: this should be done via transaction.abort()
+        repo.dirstate.invalidate() # prevent wlock from writing changes out
+
+        # restore to backuped dirstate
+        repo.vfs.rename(dirstatebackup, 'dirstate')
+        dirstatebackup = None
+    finally:
+        if dirstatebackup:
+            repo.vfs.unlink(dirstatebackup)
+
 def createcmd(ui, repo, pats, opts):
     """subcommand that creates a new shelve"""