Skip to content

Commit 4fa64a5

Browse files
phillipwoodgitster
authored andcommitted
add -p: allow line selection to be inverted
If the list of lines to be selected begins with '-' select all the lines except the ones listed. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7d4860b commit 4fa64a5

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Documentation/git-add.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ 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
343343
a space or comma, to specify a range of lines use a dash between
344344
them. If the upper bound of a range of lines is omitted it defaults to
345-
the last line.
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.
346347
+
347348
After deciding the fate for all hunks, if there is any hunk
348349
that was chosen, the index is updated with the selected hunks.

git-add--interactive.perl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,9 +1085,21 @@ sub check_hunk_label {
10851085
sub parse_hunk_selection {
10861086
local $_;
10871087
my ($hunk, $line) = @_;
1088-
my $max_label = $hunk->{MAX_LABEL};
1088+
my ($max_label, $invert) = ($hunk->{MAX_LABEL}, undef);
10891089
my @selected = (0) x ($max_label + 1);
10901090
my @fields = split(/[,\s]+/, $line);
1091+
if ($fields[0] =~ /^-(.*)/) {
1092+
$invert = 1;
1093+
if ($1 ne '') {
1094+
$fields[0] = $1;
1095+
} else {
1096+
shift @fields;
1097+
unless (@fields) {
1098+
error_msg __("no lines to invert\n");
1099+
return undef;
1100+
}
1101+
}
1102+
}
10911103
for (@fields) {
10921104
if (my ($lo, $hi) = /^([0-9]+)-([0-9]*)$/) {
10931105
if ($hi eq '') {
@@ -1107,6 +1119,9 @@ sub parse_hunk_selection {
11071119
return undef;
11081120
}
11091121
}
1122+
if ($invert) {
1123+
@selected = map { !$_ } @selected;
1124+
}
11101125
return \@selected;
11111126
}
11121127

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 2 |
413+
printf "%s\n" l "-1 3" |
414414
EDITOR=: git reset -p 2>error &&
415415
test_must_be_empty error &&
416416
git diff --cached HEAD >actual &&

0 commit comments

Comments
 (0)