Skip to content

Commit dd971cc

Browse files
wrpgitster
authored andcommitted
Add / command in add --patch
This command allows the user to skip hunks that don't match the specified regex. Signed-off-by: William Pursell <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 57886bc commit dd971cc

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

git-add--interactive.perl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ sub help_patch_cmd {
801801
a - stage this and all the remaining hunks in the file
802802
d - do not stage this hunk nor any of the remaining hunks in the file
803803
g - select a hunk to go to
804+
/ - search for a hunk matching the given regex
804805
j - leave this hunk undecided, see next undecided hunk
805806
J - leave this hunk undecided, see next hunk
806807
k - leave this hunk undecided, see previous undecided hunk
@@ -964,7 +965,7 @@ sub patch_update_file {
964965
for (@{$hunk[$ix]{DISPLAY}}) {
965966
print;
966967
}
967-
print colored $prompt_color, "Stage this hunk [y,n,a,d$other,?]? ";
968+
print colored $prompt_color, "Stage this hunk [y,n,a,d,/$other,?]? ";
968969
my $line = <STDIN>;
969970
if ($line) {
970971
if ($line =~ /^y/i) {
@@ -1013,6 +1014,31 @@ sub patch_update_file {
10131014
}
10141015
next;
10151016
}
1017+
elsif ($line =~ m|^/(.*)|) {
1018+
my $search_string;
1019+
eval {
1020+
$search_string = qr{$1}m;
1021+
};
1022+
if ($@) {
1023+
my ($err,$exp) = ($@, $1);
1024+
$err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
1025+
print STDERR "Malformed search regexp $exp: $err\n";
1026+
next;
1027+
}
1028+
my $iy = $ix;
1029+
while (1) {
1030+
my $text = join ("", @{$hunk[$iy]{TEXT}});
1031+
last if ($text =~ $search_string);
1032+
$iy++;
1033+
$iy = 0 if ($iy >= $num);
1034+
if ($ix == $iy) {
1035+
print STDERR "No hunk matches the given pattern\n";
1036+
last;
1037+
}
1038+
}
1039+
$ix = $iy;
1040+
next;
1041+
}
10161042
elsif ($other =~ /K/ && $line =~ /^K/) {
10171043
$ix--;
10181044
next;

0 commit comments

Comments
 (0)