add -p: optimize line selection for short hunks
authorPhillip Wood <phillip.wood@dunelm.org.uk>
Fri, 16 Mar 2018 10:13:46 +0000 (10:13 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 16 Mar 2018 19:08:32 +0000 (12:08 -0700)
If there are fewer than ten changes in a hunk then make spaces
optional when selecting individual lines. This means that for short
hunks one can just type 1-357 to stage lines 1, 2, 3, 5 & 7.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-add.txt
git-add--interactive.perl
t/t3701-add-interactive.sh
index 01ff4d7d24b50a57a99999e040d244f338809c60..f3c81dfb11b5188e6944d2781fcb1f14442fea85 100644 (file)
@@ -340,10 +340,11 @@ patch::
 If you press "l" then the hunk will be reprinted with each insertion or
 deletion labelled with a number and you will be prompted to enter which
 lines you wish to select. Individual line numbers should be separated by
 If you press "l" then the hunk will be reprinted with each insertion or
 deletion labelled with a number and you will be prompted to enter which
 lines you wish to select. Individual line numbers should be separated by
-a space or comma, to specify a range of lines use a dash between
-them. If the upper bound of a range of lines is omitted it defaults to
-the last line. To invert the selection prefix it with "-" so "-3-5,8"
-will select everything except lines 3, 4, 5 and 8.
+a space or comma (these can be omitted if there are fewer than ten
+labelled lines), to specify a range of lines use a dash between them. If
+the upper bound of a range of lines is omitted it defaults to the last
+line. To invert the selection prefix it with "-" so "-3-5,8" will select
+everything except lines 3, 4, 5 and 8.
 +
 After deciding the fate for all hunks, if there is any hunk
 that was chosen, the index is updated with the selected hunks.
 +
 After deciding the fate for all hunks, if there is any hunk
 that was chosen, the index is updated with the selected hunks.
index 8bc0626f0e29cc8a4ea3408352b5c867c34b0834..86e373a1011e012c2fd349fca2b85866ef599dfb 100755 (executable)
@@ -1082,6 +1082,29 @@ sub check_hunk_label {
        return 1;
 }
 
        return 1;
 }
 
+sub split_hunk_selection {
+       local $_;
+       my @fields = @_;
+       my @ret;
+       for (@fields) {
+               while ($_ ne '') {
+                       if (/^[0-9]-$/) {
+                               push @ret, $_;
+                               last;
+                       } elsif (/^([0-9](?:-[0-9])?)(.*)/) {
+                               push @ret, $1;
+                               $_ = $2;
+                       } else {
+                               error_msg sprintf
+                                   __("invalid hunk line '%s'\n"),
+                                   substr($_, 0, 1);
+                               return ();
+                       }
+               }
+       }
+       return @ret;
+}
+
 sub parse_hunk_selection {
        local $_;
        my ($hunk, $line) = @_;
 sub parse_hunk_selection {
        local $_;
        my ($hunk, $line) = @_;
@@ -1100,6 +1123,9 @@ sub parse_hunk_selection {
                        }
                }
        }
                        }
                }
        }
+       if ($max_label < 10) {
+               @fields = split_hunk_selection(@fields) or return undef;
+       }
        for (@fields) {
                if (my ($lo, $hi) = /^([0-9]+)-([0-9]*)$/) {
                        if ($hi eq '') {
        for (@fields) {
                if (my ($lo, $hi) = /^([0-9]+)-([0-9]*)$/) {
                        if ($hi eq '') {
index c1703725f23926cd17a0621503d99b3fc42a328d..b842b3b4c592d557c092caee44244c2df3218c15 100755 (executable)
@@ -410,7 +410,7 @@ test_expect_success 'setup expected diff' '
 '
 
 test_expect_success 'can reset individual lines of patch' '
 '
 
 test_expect_success 'can reset individual lines of patch' '
-       printf "%s\n" l "-1 3" |
+       printf "%s\n" l -13 |
        EDITOR=: git reset -p 2>error &&
        test_must_be_empty error &&
        git diff --cached HEAD >actual &&
        EDITOR=: git reset -p 2>error &&
        test_must_be_empty error &&
        git diff --cached HEAD >actual &&