Patchwork interactive: do not prompt about files given in command line

login
register
mail settings
Submitter Denis Laxalde
Date April 5, 2019, 9:32 a.m.
Message ID <74a00c7446609592ccb6.1554456727@steppe.logilab.fr>
Download mbox | patch
Permalink /patch/39493/
State Accepted
Headers show

Comments

Denis Laxalde - April 5, 2019, 9:32 a.m.
# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1554370518 -7200
#      Thu Apr 04 11:35:18 2019 +0200
# Node ID 74a00c7446609592ccb6a3dc5ac98769ac36bfe8
# Parent  4ee906aa7b60fb6b113e4dc187fbb5a8f42e557c
interactive: do not prompt about files given in command line

For commit and revert commands with --interactive and explicit files
given in the command line, we now skip the invite to "examine changes to
<file> ? [Ynesfdaq?]". The reason for this is that, if <file> is
specified by the user, asking for confirmation is redundant.

In patch.filterpatch(), we now use an optional "match" argument to
conditionally call the prompt() function when entering a new "header"
item. We use .exact() method to compare with files from the "header" in
order to only consider (rel)path patterns.

Add tests with glob patterns for commit and revert, to make sure we
still ask to examine files in these cases.
Pulkit Goyal - April 5, 2019, 11:46 a.m.
On Fri, Apr 5, 2019 at 12:47 PM Denis Laxalde <denis@laxalde.org> wrote:

> # HG changeset patch
> # User Denis Laxalde <denis.laxalde@logilab.fr>
> # Date 1554370518 -7200
> #      Thu Apr 04 11:35:18 2019 +0200
> # Node ID 74a00c7446609592ccb6a3dc5ac98769ac36bfe8
> # Parent  4ee906aa7b60fb6b113e4dc187fbb5a8f42e557c
> interactive: do not prompt about files given in command line
>
> For commit and revert commands with --interactive and explicit files
> given in the command line, we now skip the invite to "examine changes to
> <file> ? [Ynesfdaq?]". The reason for this is that, if <file> is
> specified by the user, asking for confirmation is redundant.
>
> In patch.filterpatch(), we now use an optional "match" argument to
> conditionally call the prompt() function when entering a new "header"
> item. We use .exact() method to compare with files from the "header" in
> order to only consider (rel)path patterns.
>
> Add tests with glob patterns for commit and revert, to make sure we
> still ask to examine files in these cases.
>

Queued, many many thanks! I recently had an user complaining about this on
twitter.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -201,7 +201,8 @@  def setupwrapcolorwrite(ui):
     setattr(ui, 'write', wrap)
     return oldwrite
 
-def filterchunks(ui, originalhunks, usecurses, testfile, operation=None):
+def filterchunks(ui, originalhunks, usecurses, testfile, match,
+                 operation=None):
     try:
         if usecurses:
             if testfile:
@@ -216,9 +217,9 @@  def filterchunks(ui, originalhunks, usec
         ui.warn('%s\n' % e.message)
         ui.warn(_('falling back to text mode\n'))
 
-    return patch.filterpatch(ui, originalhunks, operation)
-
-def recordfilter(ui, originalhunks, operation=None):
+    return patch.filterpatch(ui, originalhunks, match, operation)
+
+def recordfilter(ui, originalhunks, match, operation=None):
     """ Prompts the user to filter the originalhunks and return a list of
     selected hunks.
     *operation* is used for to build ui messages to indicate the user what
@@ -230,7 +231,7 @@  def recordfilter(ui, originalhunks, oper
     oldwrite = setupwrapcolorwrite(ui)
     try:
         newchunks, newopts = filterchunks(ui, originalhunks, usecurses,
-                                          testfile, operation)
+                                          testfile, match, operation)
     finally:
         ui.write = oldwrite
     return newchunks, newopts
@@ -312,10 +313,11 @@  def dorecord(ui, repo, commitfunc, cmdsu
         diffopts.showfunc = True
         originaldiff = patch.diff(repo, changes=status, opts=diffopts)
         originalchunks = patch.parsepatch(originaldiff)
+        match = scmutil.match(repo[None], pats)
 
         # 1. filter patch, since we are intending to apply subset of it
         try:
-            chunks, newopts = filterfn(ui, originalchunks)
+            chunks, newopts = filterfn(ui, originalchunks, match)
         except error.PatchError as err:
             raise error.Abort(_('error parsing patch: %s') % err)
         opts.update(newopts)
@@ -3082,8 +3084,9 @@  def revert(ui, repo, ctx, parents, *pats
             prefetch(repo, [ctx.rev()],
                      matchfiles(repo,
                                 [f for sublist in oplist for f in sublist]))
+            match = scmutil.match(repo[None], pats)
             _performrevert(repo, parents, ctx, names, uipathfn, actions,
-                           interactive, tobackup)
+                           match, interactive, tobackup)
 
         if targetsubs:
             # Revert the subrepos on the revert list
@@ -3096,7 +3099,7 @@  def revert(ui, repo, ctx, parents, *pats
                                       % (sub, short(ctx.node())))
 
 def _performrevert(repo, parents, ctx, names, uipathfn, actions,
-                   interactive=False, tobackup=None):
+                   match, interactive=False, tobackup=None):
     """function that actually perform all the actions computed for revert
 
     This is an independent function to let extension to plug in and react to
@@ -3192,7 +3195,7 @@  def _performrevert(repo, parents, ctx, n
 
         try:
 
-            chunks, opts = recordfilter(repo.ui, originalchunks,
+            chunks, opts = recordfilter(repo.ui, originalchunks, match,
                                         operation=operation)
             if operation == 'discard':
                 chunks = patch.reversehunks(chunks)
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1065,7 +1065,7 @@  def getmessages():
         }
     }
 
-def filterpatch(ui, headers, operation=None):
+def filterpatch(ui, headers, match, operation=None):
     """Interactively filter patch chunks into applied-only chunks"""
     messages = getmessages()
 
@@ -1182,9 +1182,13 @@  the hunk is left unchanged.
         seen.add(hdr)
         if skipall is None:
             h.pretty(ui)
+        files = h.files()
         msg = (_('examine changes to %s?') %
-               _(' and ').join("'%s'" % f for f in h.files()))
-        r, skipfile, skipall, np = prompt(skipfile, skipall, msg, None)
+               _(' and ').join("'%s'" % f for f in files))
+        if all(match.exact(f) for f in files):
+            r, skipall, np = True, None, None
+        else:
+            r, skipfile, skipall, np = prompt(skipfile, skipall, msg, None)
         if not r:
             continue
         applied[h.filename()] = [h]
diff --git a/tests/test-commit-interactive.t b/tests/test-commit-interactive.t
--- a/tests/test-commit-interactive.t
+++ b/tests/test-commit-interactive.t
@@ -26,10 +26,8 @@  Select no files
   > EOF
   diff --git a/empty-rw b/empty-rw
   new file mode 100644
-  examine changes to 'empty-rw'? [Ynesfdaq?] n
-  
-  no changes to record
-  [1]
+  abort: empty commit message
+  [255]
 
   $ hg tip -p
   changeset:   -1:000000000000
@@ -47,8 +45,6 @@  Select files but no hunks
   > EOF
   diff --git a/empty-rw b/empty-rw
   new file mode 100644
-  examine changes to 'empty-rw'? [Ynesfdaq?] y
-  
   abort: empty commit message
   [255]
 
@@ -72,12 +68,9 @@  Record empty file
 
   $ hg commit -i -d '0 0' -m empty empty-rw<<EOF
   > y
-  > y
   > EOF
   diff --git a/empty-rw b/empty-rw
   new file mode 100644
-  examine changes to 'empty-rw'? [Ynesfdaq?] y
-  
 
   $ hg tip -p
   changeset:   0:c0708cf4e46e
@@ -249,8 +242,6 @@  Add plain file
   > EOF
   diff --git a/plain b/plain
   new file mode 100644
-  examine changes to 'plain'? [Ynesfdaq?] y
-  
   @@ -0,0 +1,10 @@
   +1
   +2
@@ -306,8 +297,6 @@  Modify end of plain file, also test that
   > EOF
   diff --git a/plain b/plain
   1 hunks, 1 lines changed
-  examine changes to 'plain'? [Ynesfdaq?] y
-  
   @@ -8,3 +8,4 @@ 7
    8
    9
@@ -325,8 +314,6 @@  Modify end of plain file, no EOL
   > EOF
   diff --git a/plain b/plain
   1 hunks, 1 lines changed
-  examine changes to 'plain'? [Ynesfdaq?] y
-  
   @@ -9,3 +9,4 @@ 8
    9
    10
@@ -467,8 +454,6 @@  Modify end of plain file, add EOL
   > EOF
   diff --git a/plain b/plain
   1 hunks, 1 lines changed
-  examine changes to 'plain'? [Ynesfdaq?] y
-  
   @@ -9,4 +9,4 @@ 8
    9
    10
@@ -480,8 +465,6 @@  Modify end of plain file, add EOL
   
   diff --git a/plain2 b/plain2
   new file mode 100644
-  examine changes to 'plain2'? [Ynesfdaq?] y
-  
   @@ -0,0 +1,1 @@
   +1
   record change 2/2 to 'plain2'? [Ynesfdaq?] y
@@ -504,8 +487,6 @@  changes numbering
   > EOF
   diff --git a/plain b/plain
   2 hunks, 3 lines changed
-  examine changes to 'plain'? [Ynesfdaq?] y
-  
   @@ -1,4 +1,4 @@
   -1
   +2
@@ -524,8 +505,6 @@  changes numbering
   
   diff --git a/plain2 b/plain2
   1 hunks, 1 lines changed
-  examine changes to 'plain2'? [Ynesfdaq?] y
-  
   @@ -1,1 +1,2 @@
    1
   +2
@@ -572,14 +551,11 @@  Trim beginning, modify end
 Record end
 
   $ hg commit -i -d '11 0' -m end-only plain <<EOF
-  > y
   > n
   > y
   > EOF
   diff --git a/plain b/plain
   2 hunks, 4 lines changed
-  examine changes to 'plain'? [Ynesfdaq?] y
-  
   @@ -1,9 +1,6 @@
   -2
   -2
@@ -630,8 +606,6 @@  Record beginning
   > EOF
   diff --git a/plain b/plain
   1 hunks, 3 lines changed
-  examine changes to 'plain'? [Ynesfdaq?] y
-  
   @@ -1,6 +1,3 @@
   -2
   -2
@@ -671,14 +645,11 @@  Add to beginning, trim from end
 Record end
 
   $ hg commit -i --traceback -d '13 0' -m end-again plain<<EOF
-  > y
   > n
   > y
   > EOF
   diff --git a/plain b/plain
   2 hunks, 4 lines changed
-  examine changes to 'plain'? [Ynesfdaq?] y
-  
   @@ -1,6 +1,9 @@
   +1
   +2
@@ -714,13 +685,10 @@  Record beginning, middle, and test that 
   $ hg commit -i --config diff.noprefix=True -d '14 0' -m middle-only plain <<EOF
   > y
   > y
-  > y
   > n
   > EOF
   diff --git a/plain b/plain
   3 hunks, 7 lines changed
-  examine changes to 'plain'? [Ynesfdaq?] y
-  
   @@ -1,2 +1,5 @@
   +1
   +2
@@ -781,8 +749,6 @@  Record end
   > EOF
   diff --git a/plain b/plain
   1 hunks, 2 lines changed
-  examine changes to 'plain'? [Ynesfdaq?] y
-  
   @@ -9,3 +9,5 @@ 6
    7
    8
@@ -823,8 +789,6 @@  Record end
   > EOF
   diff --git a/subdir/a b/subdir/a
   1 hunks, 1 lines changed
-  examine changes to 'subdir/a'? [Ynesfdaq?] y
-  
   @@ -1,1 +1,2 @@
    a
   +a
@@ -879,6 +843,35 @@  Help, quit
   abort: user quit
   [255]
 
+Patterns
+
+  $ hg commit -i 'glob:f*' << EOF
+  > y
+  > n
+  > y
+  > n
+  > EOF
+  diff --git a/subdir/f1 b/subdir/f1
+  1 hunks, 1 lines changed
+  examine changes to 'subdir/f1'? [Ynesfdaq?] y
+  
+  @@ -1,1 +1,2 @@
+   a
+  +a
+  record change 1/2 to 'subdir/f1'? [Ynesfdaq?] n
+  
+  diff --git a/subdir/f2 b/subdir/f2
+  1 hunks, 1 lines changed
+  examine changes to 'subdir/f2'? [Ynesfdaq?] y
+  
+  @@ -1,1 +1,2 @@
+   b
+  +b
+  record change 2/2 to 'subdir/f2'? [Ynesfdaq?] n
+  
+  no changes to record
+  [1]
+
 #if gettext
 
 Test translated help message
diff --git a/tests/test-diff-color.t b/tests/test-diff-color.t
--- a/tests/test-diff-color.t
+++ b/tests/test-diff-color.t
@@ -157,14 +157,11 @@  record
   $ chmod +x a
   $ hg record -m moda a <<EOF
   > y
-  > y
   > EOF
   \x1b[0;1mdiff --git a/a b/a\x1b[0m (esc)
   \x1b[0;36;1mold mode 100644\x1b[0m (esc)
   \x1b[0;36;1mnew mode 100755\x1b[0m (esc)
   1 hunks, 1 lines changed
-  \x1b[0;33mexamine changes to 'a'? [Ynesfdaq?]\x1b[0m y (esc)
-  
   \x1b[0;35m@@ -2,7 +2,7 @@ c\x1b[0m (esc)
    c
    a
diff --git a/tests/test-keyword.t b/tests/test-keyword.t
--- a/tests/test-keyword.t
+++ b/tests/test-keyword.t
@@ -383,13 +383,10 @@  record chunk
   >>> open('a', 'wb').writelines(lines)
   $ hg record -d '10 1' -m rectest a<<EOF
   > y
-  > y
   > n
   > EOF
   diff --git a/a b/a
   2 hunks, 2 lines changed
-  examine changes to 'a'? [Ynesfdaq?] y
-  
   @@ -1,3 +1,4 @@
    expand $Id$
   +foo
@@ -448,8 +445,6 @@  Record all chunks in file a
   > EOF
   diff --git a/a b/a
   2 hunks, 2 lines changed
-  examine changes to 'a'? [Ynesfdaq?] y
-  
   @@ -1,3 +1,4 @@
    expand $Id$
   +foo
@@ -519,8 +514,6 @@  record added file alone
   > EOF
   diff --git a/r b/r
   new file mode 100644
-  examine changes to 'r'? [Ynesfdaq?] y
-  
   @@ -0,0 +1,1 @@
   +$Id$
   record this change to 'r'? [Ynesfdaq?] y
diff --git a/tests/test-record.t b/tests/test-record.t
--- a/tests/test-record.t
+++ b/tests/test-record.t
@@ -76,10 +76,8 @@  Select no files
   > EOF
   diff --git a/empty-rw b/empty-rw
   new file mode 100644
-  examine changes to 'empty-rw'? [Ynesfdaq?] n
-  
-  no changes to record
-  [1]
+  abort: empty commit message
+  [255]
 
   $ hg tip -p
   changeset:   -1:000000000000
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
@@ -149,11 +149,9 @@  Test that --interactive lift the need fo
   g
 
 Test that a noop revert doesn't do an unnecessary backup
-  $ (echo y; echo n) | hg revert -i -r 2 folder1/g
+  $ (echo n) | hg revert -i -r 2 folder1/g
   diff --git a/folder1/g b/folder1/g
   1 hunks, 1 lines changed
-  examine changes to 'folder1/g'? [Ynesfdaq?] y
-  
   @@ -3,4 +3,3 @@
    3
    4
@@ -165,11 +163,9 @@  Test that a noop revert doesn't do an un
   g
 
 Test --no-backup
-  $ (echo y; echo y) | hg revert -i -C -r 2 folder1/g
+  $ (echo y) | hg revert -i -C -r 2 folder1/g
   diff --git a/folder1/g b/folder1/g
   1 hunks, 1 lines changed
-  examine changes to 'folder1/g'? [Ynesfdaq?] y
-  
   @@ -3,4 +3,3 @@
    3
    4
@@ -270,7 +266,6 @@  Test --no-backup
   M f
   M folder1/g
   $ hg revert --interactive f << EOF
-  > y
   > ?
   > y
   > n
@@ -278,8 +273,6 @@  Test --no-backup
   > EOF
   diff --git a/f b/f
   2 hunks, 2 lines changed
-  examine changes to 'f'? [Ynesfdaq?] y
-  
   @@ -1,6 +1,5 @@
   -a
    1
@@ -327,6 +320,25 @@  Test --no-backup
   4
   5
   $ rm f.orig
+
+Patterns
+
+  $ hg revert -i 'glob:f*' << EOF
+  > y
+  > n
+  > EOF
+  diff --git a/f b/f
+  1 hunks, 1 lines changed
+  examine changes to 'f'? [Ynesfdaq?] y
+  
+  @@ -4,4 +4,3 @@
+   3
+   4
+   5
+  -b
+  discard this change to 'f'? [Ynesfdaq?] n
+  
+
   $ hg update -C .
   2 files updated, 0 files merged, 0 files removed, 0 files unresolved