Skip to content

Commit 05f700e

Browse files
phillipwoodgitster
authored andcommitted
add -p: optimize line selection for short hunks
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4fa64a5 commit 05f700e

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

Documentation/git-add.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,11 @@ patch::
340340
If you press "l" then the hunk will be reprinted with each insertion or
341341
deletion labelled with a number and you will be prompted to enter which
342342
lines you wish to select. Individual line numbers should be separated by
343-
a space or comma, to specify a range of lines use a dash between
344-
them. If the upper bound of a range of lines is omitted it defaults to
345-
the last line. To invert the selection prefix it with "-" so "-3-5,8"
346-
will select everything except lines 3, 4, 5 and 8.
343+
a space or comma (these can be omitted if there are fewer than ten
344+
labelled lines), to specify a range of lines use a dash between them. If
345+
the upper bound of a range of lines is omitted it defaults to the last
346+
line. To invert the selection prefix it with "-" so "-3-5,8" will select
347+
everything except lines 3, 4, 5 and 8.
347348
+
348349
After deciding the fate for all hunks, if there is any hunk
349350
that was chosen, the index is updated with the selected hunks.

git-add--interactive.perl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,29 @@ sub check_hunk_label {
10821082
return 1;
10831083
}
10841084

1085+
sub split_hunk_selection {
1086+
local $_;
1087+
my @fields = @_;
1088+
my @ret;
1089+
for (@fields) {
1090+
while ($_ ne '') {
1091+
if (/^[0-9]-$/) {
1092+
push @ret, $_;
1093+
last;
1094+
} elsif (/^([0-9](?:-[0-9])?)(.*)/) {
1095+
push @ret, $1;
1096+
$_ = $2;
1097+
} else {
1098+
error_msg sprintf
1099+
__("invalid hunk line '%s'\n"),
1100+
substr($_, 0, 1);
1101+
return ();
1102+
}
1103+
}
1104+
}
1105+
return @ret;
1106+
}
1107+
10851108
sub parse_hunk_selection {
10861109
local $_;
10871110
my ($hunk, $line) = @_;
@@ -1100,6 +1123,9 @@ sub parse_hunk_selection {
11001123
}
11011124
}
11021125
}
1126+
if ($max_label < 10) {
1127+
@fields = split_hunk_selection(@fields) or return undef;
1128+
}
11031129
for (@fields) {
11041130
if (my ($lo, $hi) = /^([0-9]+)-([0-9]*)$/) {
11051131
if ($hi eq '') {

t/t3701-add-interactive.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ test_expect_success 'setup expected diff' '
410410
'
411411

412412
test_expect_success 'can reset individual lines of patch' '
413-
printf "%s\n" l "-1 3" |
413+
printf "%s\n" l -13 |
414414
EDITOR=: git reset -p 2>error &&
415415
test_must_be_empty error &&
416416
git diff --cached HEAD >actual &&

0 commit comments

Comments
 (0)