Patchwork D6125: revert: option to choose what to keep, not what to discard

login
register
mail settings
Submitter phabricator
Date March 12, 2019, 10:14 p.m.
Message ID <differential-rev-PHID-DREV-zb66gwlmypka4s26tynk-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/39234/
State Superseded
Headers show

Comments

phabricator - March 12, 2019, 10:14 p.m.
martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  I know the you (the reader) are probably tired of discussing how `hg
  revert -i -r .` should behave and so am I. And I know I'm one of the
  people who argued that showing the diff from the working copy to the
  parent was confusing. I think it is less confusing now that we show
  the diff from the parent to the working copy, but I still find it
  confusing. I think showing the diff of hunks to keep might make it
  easier to understand. So that's what this patch provides an option
  for.
  
  One argument doing it this way is that most people seem to find `hg
  split` natural. I suspect that is because it shows the forward diff
  (from parent commit to the commit) and asks you what to put in the
  first commit. I think the new "keep" mode for revert (this patch)
  matches that.
  
  In "keep" mode, all the changes are still selected by default. That
  means that `hg revert -i` followed by 'A' (keep all) (or 'c' in
  curses) will be different from `hg revert -a`. That's mostly because
  that was simplest. It can also be argued that it's safest. But it can
  also be argued that it should be consistent with `hg revert -a`.
  
  Note that in this mode, you can edit the hunks and it will do what you
  expect (e.g. add new lines to your file if you added a new lines when
  editing). The test case shows that that works.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/cmdutil.py
  mercurial/configitems.py
  mercurial/crecord.py
  mercurial/patch.py
  tests/test-revert-interactive.t

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-devel
phabricator - March 15, 2019, 4:33 p.m.
martinvonz added a comment.


  I understand that it's not at all clear if we want to make this change by default, but I'd appreciate if we can queue this patch as is so I and others can easily start testing it (it's pretty deep inside `_performrevert()`, so it's hard to do it cleanly in an extension). I don't believe I'm changing the behavior for anyone who has not set the new config.

REPOSITORY
  rHG Mercurial

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

To: martinvonz, #hg-reviewers
Cc: mercurial-devel
phabricator - March 19, 2019, 3:07 p.m.
pulkit added a comment.


  I like what this patch does. I am not sure why this needs to be experimental since `hg revert -i` is not. Also, let's document this config option in `hg help revert`.

INLINE COMMENTS

> configitems.py:598
>  )
> +coreconfigitem('experimental', 'revert.interactive.select-to-keep',
> +    default=False,

Is there any reason for it to be experimental? I see `hg revert -i` is not interactive.

REPOSITORY
  rHG Mercurial

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

To: martinvonz, #hg-reviewers
Cc: pulkit, mercurial-devel
phabricator - March 19, 2019, 3:12 p.m.
martinvonz added inline comments.

INLINE COMMENTS

> pulkit wrote in configitems.py:598
> Is there any reason for it to be experimental? I see `hg revert -i` is not interactive.

I made it experimental so we can test it for a bit and see if we prefer this UX. I don't want to change the behavior for everyone without testing it a bit first, and it's hard to test this on more than just myself if it's not in core.

REPOSITORY
  rHG Mercurial

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

To: martinvonz, #hg-reviewers
Cc: pulkit, mercurial-devel

Patch

diff --git a/tests/test-revert-interactive.t b/tests/test-revert-interactive.t
--- a/tests/test-revert-interactive.t
+++ b/tests/test-revert-interactive.t
@@ -444,4 +444,52 @@ 
   > EOF
   add back removed file a (Yn)? n
   $ ls
+  $ hg revert -a
+  undeleting a
   $ cd ..
+
+Test "keep" mode
+
+  $ cat <<EOF >> $HGRCPATH
+  > [experimental]
+  > revert.interactive.select-to-keep = true
+  > EOF
+
+  $ cd repo
+  $ printf "x\na\ny\n" > a
+  $ hg diff
+  diff -r cb9a9f314b8b a
+  --- a/a	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/a	Thu Jan 01 00:00:00 1970 +0000
+  @@ -1,1 +1,3 @@
+  +x
+   a
+  +y
+  $ cat > $TESTTMP/editor.sh << '__EOF__'
+  > echo "+new line" >> "$1"
+  > __EOF__
+
+  $ HGEDITOR="\"sh\" \"${TESTTMP}/editor.sh\"" hg revert -i  <<EOF
+  > y
+  > n
+  > e
+  > EOF
+  diff --git a/a b/a
+  2 hunks, 2 lines changed
+  examine changes to 'a'? [Ynesfdaq?] y
+  
+  @@ -1,1 +1,2 @@
+  +x
+   a
+  keep change 1/2 to 'a'? [Ynesfdaq?] n
+  
+  @@ -1,1 +2,2 @@
+   a
+  +y
+  keep change 2/2 to 'a'? [Ynesfdaq?] e
+  
+  reverting a
+  $ cat a
+  a
+  y
+  new line
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1012,11 +1012,13 @@ 
         'multiple': {
             'apply': _("apply change %d/%d to '%s'?"),
             'discard': _("discard change %d/%d to '%s'?"),
+            'keep': _("keep change %d/%d to '%s'?"),
             'record': _("record change %d/%d to '%s'?"),
         },
         'single': {
             'apply': _("apply this change to '%s'?"),
             'discard': _("discard this change to '%s'?"),
+            'keep': _("keep this change to '%s'?"),
             'record': _("record this change to '%s'?"),
         },
         'help': {
@@ -1040,6 +1042,16 @@ 
                          '$$ Discard &all changes to all remaining files'
                          '$$ &Quit, discarding no changes'
                          '$$ &? (display help)'),
+            'keep': _('[Ynesfdaq?]'
+                         '$$ &Yes, keep this change'
+                         '$$ &No, skip this change'
+                         '$$ &Edit this change manually'
+                         '$$ &Skip remaining changes to this file'
+                         '$$ Keep remaining changes to this &file'
+                         '$$ &Done, skip remaining changes and files'
+                         '$$ Keep &all changes to all remaining files'
+                         '$$ &Quit, keeping all changes'
+                         '$$ &? (display help)'),
             'record': _('[Ynesfdaq?]'
                         '$$ &Yes, record this change'
                         '$$ &No, skip this change'
diff --git a/mercurial/crecord.py b/mercurial/crecord.py
--- a/mercurial/crecord.py
+++ b/mercurial/crecord.py
@@ -566,6 +566,7 @@ 
 _headermessages = { # {operation: text}
     'apply': _('Select hunks to apply'),
     'discard': _('Select hunks to discard'),
+    'keep': _('Select hunks to keep'),
     None: _('Select hunks to record'),
 }
 
diff --git a/mercurial/configitems.py b/mercurial/configitems.py
--- a/mercurial/configitems.py
+++ b/mercurial/configitems.py
@@ -595,6 +595,9 @@ 
 coreconfigitem('experimental', 'removeemptydirs',
     default=True,
 )
+coreconfigitem('experimental', 'revert.interactive.select-to-keep',
+    default=False,
+)
 coreconfigitem('experimental', 'revisions.prefixhexnode',
     default=False,
 )
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -3154,22 +3154,25 @@ 
                                          configprefix='revert.interactive.')
         diffopts.nodates = True
         diffopts.git = True
-        operation = 'discard'
-        reversehunks = True
-        if node != parent:
-            operation = 'apply'
-            reversehunks = False
-        if reversehunks:
+        operation = 'apply'
+        if node == parent:
+            if repo.ui.configbool('experimental',
+                                  'revert.interactive.select-to-keep'):
+                operation = 'keep'
+            else:
+                operation = 'discard'
+
+        if operation == 'apply':
+            diff = patch.diff(repo, None, ctx.node(), m, opts=diffopts)
+        else:
             diff = patch.diff(repo, ctx.node(), None, m, opts=diffopts)
-        else:
-            diff = patch.diff(repo, None, ctx.node(), m, opts=diffopts)
         originalchunks = patch.parsepatch(diff)
 
         try:
 
             chunks, opts = recordfilter(repo.ui, originalchunks,
                                         operation=operation)
-            if reversehunks:
+            if operation == 'discard':
                 chunks = patch.reversehunks(chunks)
 
         except error.PatchError as err:
@@ -3183,6 +3186,7 @@ 
         # chunks are serialized per file, but files aren't sorted
         for f in sorted(set(c.header.filename() for c in chunks if ishunk(c))):
             prntstatusmsg('revert', f)
+        files = set()
         for c in chunks:
             if ishunk(c):
                 abs = c.header.filename()
@@ -3192,6 +3196,10 @@ 
                     bakname = scmutil.backuppath(repo.ui, repo, abs)
                     util.copyfile(target, bakname)
                     tobackup.remove(abs)
+                if abs not in files:
+                    files.add(abs)
+                    if operation == 'keep':
+                        checkout(abs)
             c.write(fp)
         dopatch = fp.tell()
         fp.seek(0)