Patchwork [3,of,3] shelve: do not restore dirstate when keeping wdir changes

login
register
mail settings
Submitter Jordi Gutiérrez Hermoso
Date March 22, 2019, 3:29 p.m.
Message ID <c0f2ceec4a2d06bb4950.1553268590@chloe>
Download mbox | patch
Permalink /patch/39358/
State Superseded
Headers show

Comments

Jordi Gutiérrez Hermoso - March 22, 2019, 3:29 p.m.
# HG changeset patch
# User Jordi Gutiérrez Hermoso <jordigh@octave.org>
# Date 1553268407 14400
#      Fri Mar 22 11:26:47 2019 -0400
# Node ID c0f2ceec4a2d06bb4950916db3bb91300cb091da
# Parent  6bff7f54a5f20e72e63edbceb2a34d86fb4c86f4
shelve: do not restore dirstate when keeping wdir changes

This completes the implementation of `shelve --keep`. Creating a
backup of the dirstate is necessary so that closing the transaction
won't also destroy the working directory changes, however, restoring
that backup does touch the wdir. Thus, we have to pass down the `keep`
parameter down to a few functions to get the right functionality.

Patch

diff --git a/hgext/shelve.py b/hgext/shelve.py
--- a/hgext/shelve.py
+++ b/hgext/shelve.py
@@ -323,13 +323,14 @@  def _restoreactivebookmark(repo, mark):
     if mark:
         bookmarks.activate(repo, mark)
 
-def _aborttransaction(repo, tr):
+def _aborttransaction(repo, tr, keep=False):
     '''Abort current transaction for shelve/unshelve, but keep dirstate
     '''
     dirstatebackupname = 'dirstate.shelve'
     repo.dirstate.savebackup(tr, dirstatebackupname)
     tr.abort()
-    repo.dirstate.restorebackup(None, dirstatebackupname)
+    if not keep:
+        repo.dirstate.restorebackup(None, dirstatebackupname)
 
 def getshelvename(repo, parent, opts):
     """Decide on the name this shelve is going to have"""
@@ -439,11 +440,11 @@  def _includeunknownfiles(repo, pats, opt
         extra['shelve_unknown'] = '\0'.join(s.unknown)
         repo[None].add(s.unknown)
 
-def _finishshelve(repo, tr):
-    if phases.supportinternal(repo):
+def _finishshelve(repo, tr, keep=False):
+    if phases.supportinternal(repo) and not keep:
         tr.close()
     else:
-        _aborttransaction(repo, tr)
+        _aborttransaction(repo, tr, keep=keep)
 
 def createcmd(ui, repo, pats, opts):
     """subcommand that creates a new shelve"""
@@ -513,7 +514,7 @@  def _docreatecmd(ui, repo, pats, opts):
         if origbranch != repo['.'].branch() and not _isbareshelve(pats, opts):
             repo.dirstate.setbranch(origbranch)
 
-        _finishshelve(repo, tr)
+        _finishshelve(repo, tr, keep=opts['keep'])
     finally:
         _restoreactivebookmark(repo, activebookmark)
         lockmod.release(tr, lock)
diff --git a/tests/test-shelve.t b/tests/test-shelve.t
--- a/tests/test-shelve.t
+++ b/tests/test-shelve.t
@@ -928,6 +928,20 @@  with general delta
   Stream params: {Compression: BZ}
   changegroup -- {nbchanges: 1, version: 02} (mandatory: True)
       330882a04d2ce8487636b1fb292e5beea77fa1e3
+
+Test shelve --keep
+
+  $ hg unshelve
+  unshelving change 'default'
+  $ hg shelve --keep
+  shelved as default
+  $ hg diff
+  diff --git a/jungle b/jungle
+  new file mode 100644
+  --- /dev/null
+  +++ b/jungle
+  @@ -0,0 +1,1 @@
+  +babar
   $ cd ..
 
 Test visibility of in-memory changes inside transaction to external hook