D6596: unshelve: add interactive mode

navaneeth.suresh (Navaneeth Suresh) phabricator at mercurial-scm.org
Mon Jul 15 10:06:06 EDT 2019


navaneeth.suresh updated this revision to Diff 15908.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D6596?vs=15907&id=15908

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D6596/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D6596

AFFECTED FILES
  mercurial/commands.py
  mercurial/shelve.py
  tests/test-completion.t
  tests/test-shelve.t

CHANGE DETAILS

diff --git a/tests/test-shelve.t b/tests/test-shelve.t
--- a/tests/test-shelve.t
+++ b/tests/test-shelve.t
@@ -1158,3 +1158,154 @@
   [255]
 
   $ cd ..
+
+-- test for interactive mode on unshelve
+
+  $ hg init a
+  $ cd a
+  $ echo > b
+  $ hg ci -Am b
+  adding b
+  $ echo > c
+  $ echo > d
+  $ hg add .
+  adding c
+  adding d
+  $ hg shelve
+  shelved as default
+  0 files updated, 0 files merged, 2 files removed, 0 files unresolved
+  $ echo > e
+  $ hg add e
+  $ hg ci -m e
+  $ hg shelve --patch
+  default         (1s ago)    changes to: b
+  
+  diff --git a/c b/c
+  new file mode 100644
+  --- /dev/null
+  +++ b/c
+  @@ -0,0 +1,1 @@
+  +
+  diff --git a/d b/d
+  new file mode 100644
+  --- /dev/null
+  +++ b/d
+  @@ -0,0 +1,1 @@
+  +
+  $ hg unshelve -i <<EOF
+  > y
+  > y
+  > y
+  > n
+  > EOF
+  unshelving change 'default'
+  rebasing shelved changes
+  diff --git a/c b/c
+  new file mode 100644
+  examine changes to 'c'?
+  (enter ? for help) [Ynesfdaq?] y
+  
+  @@ -0,0 +1,1 @@
+  +
+  record change 1/2 to 'c'?
+  (enter ? for help) [Ynesfdaq?] y
+  
+  diff --git a/d b/d
+  new file mode 100644
+  examine changes to 'd'?
+  (enter ? for help) [Ynesfdaq?] y
+  
+  @@ -0,0 +1,1 @@
+  +
+  record change 2/2 to 'd'?
+  (enter ? for help) [Ynesfdaq?] n
+  
+  $ ls
+  b
+  c
+  e
+-- shelve should not contain `c` now
+  $ hg shelve --patch
+  default         (1s ago)    changes to: b
+  
+  diff --git a/d b/d
+  new file mode 100644
+  --- /dev/null
+  +++ b/d
+  @@ -0,0 +1,1 @@
+  +
+  $ hg unshelve -i <<EOF
+  > y
+  > y
+  > EOF
+  unshelving change 'default'
+  rebasing shelved changes
+  diff --git a/d b/d
+  new file mode 100644
+  examine changes to 'd'?
+  (enter ? for help) [Ynesfdaq?] y
+  
+  @@ -0,0 +1,1 @@
+  +
+  record this change to 'd'?
+  (enter ? for help) [Ynesfdaq?] y
+  
+  $ ls
+  b
+  c
+  d
+  e
+  $ hg shelve --list
+
+-- now, unshelve selected changes from a file
+
+  $ echo B > foo
+  $ hg add foo
+  $ hg ci -m 'add B to foo'
+  $ cat > foo <<EOF
+  > A
+  > B
+  > C
+  > EOF
+  $ hg shelve
+  shelved as default
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cat foo
+  B
+  $ hg unshelve -i <<EOF
+  > y
+  > y
+  > n
+  > EOF
+  unshelving change 'default'
+  rebasing shelved changes
+  diff --git a/foo b/foo
+  2 hunks, 2 lines changed
+  examine changes to 'foo'?
+  (enter ? for help) [Ynesfdaq?] y
+  
+  @@ -1,1 +1,2 @@
+  +A
+   B
+  record change 1/2 to 'foo'?
+  (enter ? for help) [Ynesfdaq?] y
+  
+  @@ -1,1 +2,2 @@
+   B
+  +C
+  record change 2/2 to 'foo'?
+  (enter ? for help) [Ynesfdaq?] n
+  
+  $ cat foo
+  A
+  B
+  $ hg shelve --patch
+  default         (1s ago)    changes to: add B to foo
+  
+  diff --git a/foo b/foo
+  --- a/foo
+  +++ b/foo
+  @@ -1,2 +1,3 @@
+   A
+   B
+  +C
diff --git a/tests/test-completion.t b/tests/test-completion.t
--- a/tests/test-completion.t
+++ b/tests/test-completion.t
@@ -352,7 +352,7 @@
   tags: template
   tip: patch, git, style, template
   unbundle: update
-  unshelve: abort, continue, keep, name, tool, date
+  unshelve: abort, continue, interactive, keep, name, tool, date
   update: clean, check, merge, date, rev, tool
   verify: full
   version: template
diff --git a/mercurial/shelve.py b/mercurial/shelve.py
--- a/mercurial/shelve.py
+++ b/mercurial/shelve.py
@@ -791,14 +791,39 @@
 
     return repo, shelvectx
 
+def _dounshelveinteractive(ui, repo, shelvectx, basename, opts):
+    """The user might want to unshelve certain changes only from the stored
+    shelve. So, we would create two commits. One with requested changes to
+    unshelve at that time and the latter is shelved for future.
+    """
+    opts['message'] = shelvectx.description()
+    pats = []
+    commitfunc = getcommitfunc(shelvectx.extra(), interactive=True,
+                               editor=True)
+    newnode = cmdutil.dorecord(ui, repo, commitfunc, None, False,
+                               cmdutil.recordfilter, *pats,
+                               **pycompat.strkwargs(opts))
+    snode = repo.commit(text=shelvectx.description(),
+                        extra=shelvectx.extra(),
+                        user=shelvectx.user(),
+                        date=shelvectx.date())
+    m = scmutil.matchfiles(repo, repo[snode].files())
+    if snode:
+        _shelvecreatedcommit(repo, snode, basename, m)
+
+    return newnode, bool(snode)
+
 def _rebaserestoredcommit(ui, repo, opts, tr, oldtiprev, basename, pctx,
                           tmpwctx, shelvectx, branchtorestore,
                           activebookmark):
     """Rebase restored commit from its original location to a destination"""
     # If the shelve is not immediately on top of the commit
     # we'll be merging with, rebase it to be on top.
-    if tmpwctx.node() == shelvectx.p1().node():
-        return shelvectx
+    interactive = opts.get('interactive')
+    if tmpwctx.node() == shelvectx.p1().node() and not interactive:
+        # We won't skip on interactive mode because, the user might want to
+        # unshelve certain changes only.
+        return shelvectx, False
 
     overrides = {
         ('ui', 'forcemerge'): opts.get('tool', ''),
@@ -822,10 +847,15 @@
 
         with repo.dirstate.parentchange():
             repo.setparents(tmpwctx.node(), nodemod.nullid)
-            newnode = repo.commit(text=shelvectx.description(),
-                                  extra=shelvectx.extra(),
-                                  user=shelvectx.user(),
-                                  date=shelvectx.date())
+            if not interactive:
+                ispartialunshelve = False
+                newnode = repo.commit(text=shelvectx.description(),
+                                      extra=shelvectx.extra(),
+                                      user=shelvectx.user(),
+                                      date=shelvectx.date())
+            else:
+                newnode, ispartialunshelve = _dounshelveinteractive(ui, repo,
+                                                shelvectx, basename, opts)
 
         if newnode is None:
             # If it ended up being a no-op commit, then the normal
@@ -840,7 +870,7 @@
             shelvectx = repo[newnode]
             hg.updaterepo(repo, tmpwctx.node(), False)
 
-    return shelvectx
+    return shelvectx, ispartialunshelve
 
 def _forgetunknownfiles(repo, shelvectx, addedbefore):
     # Forget any files that were unknown before the shelve, unknown before
@@ -877,6 +907,7 @@
     opts = pycompat.byteskwargs(opts)
     abortf = opts.get('abort')
     continuef = opts.get('continue')
+    interactive = opts.get('interactive')
     if not abortf and not continuef:
         cmdutil.checkunfinished(repo)
     shelved = list(shelved)
@@ -935,19 +966,19 @@
         if shelvectx.branch() != shelvectx.p1().branch():
             branchtorestore = shelvectx.branch()
 
-        shelvectx = _rebaserestoredcommit(ui, repo, opts, tr, oldtiprev,
-                                          basename, pctx, tmpwctx,
-                                          shelvectx, branchtorestore,
-                                          activebookmark)
+        shelvectx, ispartialunshelve = _rebaserestoredcommit(ui, repo, opts,
+            tr, oldtiprev, basename, pctx, tmpwctx, shelvectx,
+            branchtorestore, activebookmark)
         overrides = {('ui', 'forcemerge'): opts.get('tool', '')}
         with ui.configoverride(overrides, 'unshelve'):
             mergefiles(ui, repo, pctx, shelvectx)
         restorebranch(ui, repo, branchtorestore)
-        _forgetunknownfiles(repo, shelvectx, addedbefore)
+        if not ispartialunshelve:
+            _forgetunknownfiles(repo, shelvectx, addedbefore)
 
-        shelvedstate.clear(repo)
-        _finishunshelve(repo, oldtiprev, tr, activebookmark)
-        unshelvecleanup(ui, repo, basename, opts)
+            shelvedstate.clear(repo)
+            _finishunshelve(repo, oldtiprev, tr, activebookmark)
+            unshelvecleanup(ui, repo, basename, opts)
     finally:
         if tr:
             tr.release()
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -6142,6 +6142,8 @@
            _('abort an incomplete unshelve operation')),
           ('c', 'continue', None,
            _('continue an incomplete unshelve operation')),
+          ('i', 'interactive', None,
+           _('use interactive mode')),
           ('k', 'keep', None,
            _('keep shelve after unshelving')),
           ('n', 'name', '',
@@ -6149,7 +6151,7 @@
           ('t', 'tool', '', _('specify merge tool')),
           ('', 'date', '',
            _('set date for temporary commits (DEPRECATED)'), _('DATE'))],
-         _('hg unshelve [[-n] SHELVED]'),
+         _('hg unshelve [OPTION]... [FILE]... [-n SHELVED]'),
          helpcategory=command.CATEGORY_WORKING_DIRECTORY)
 def unshelve(ui, repo, *shelved, **opts):
     """restore a shelved change to the working directory



To: navaneeth.suresh, #hg-reviewers
Cc: pulkit, mercurial-devel


More information about the Mercurial-devel mailing list