summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhillip Wood <phillip.wood@dunelm.org.uk>2018-03-16 10:13:46 +0000
committerJunio C Hamano <gitster@pobox.com>2018-03-16 12:08:32 -0700
commit05f700ef262d1d318a45dd0ce299bbd076d95963 (patch)
treef89c8fd25e9e89bc236769754e88029ff72a3b3f
parent4fa64a54f4c6dd3e1ec05b95e40823d081e2d238 (diff)
downloadgit-pw/add-p-select.tar.gz
add -p: optimize line selection for short hunkspw/add-p-select
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>
-rw-r--r--Documentation/git-add.txt9
-rwxr-xr-xgit-add--interactive.perl26
-rwxr-xr-xt/t3701-add-interactive.sh2
3 files changed, 32 insertions, 5 deletions
diff --git a/Documentation/git-add.txt b/Documentation/git-add.txt
index 01ff4d7d24..f3c81dfb11 100644
--- a/Documentation/git-add.txt
+++ b/Documentation/git-add.txt
@@ -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
-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.
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index 8bc0626f0e..86e373a101 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -1082,6 +1082,29 @@ sub check_hunk_label {
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) = @_;
@@ -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 '') {
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index c1703725f2..b842b3b4c5 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -410,7 +410,7 @@ test_expect_success 'setup expected diff' '
'
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 &&