Skip to content

Commit d3f552b

Browse files
committed
Merge branch 'wp/add-patch-find'
* wp/add-patch-find: add -p: trap Ctrl-D in 'goto' mode add -p: change prompt separator for 'g' In add --patch, Handle K,k,J,j slightly more gracefully. Add / command in add --patch git-add -i/-p: Change prompt separater from slash to comma
2 parents a4f004b + 68c02d7 commit d3f552b

File tree

1 file changed

+64
-22
lines changed

1 file changed

+64
-22
lines changed

git-add--interactive.perl

Lines changed: 64 additions & 22 deletions
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
@@ -929,25 +930,25 @@ sub patch_update_file {
929930
for ($i = 0; $i < $ix; $i++) {
930931
if (!defined $hunk[$i]{USE}) {
931932
$prev = 1;
932-
$other .= '/k';
933+
$other .= ',k';
933934
last;
934935
}
935936
}
936937
if ($ix) {
937-
$other .= '/K';
938+
$other .= ',K';
938939
}
939940
for ($i = $ix + 1; $i < $num; $i++) {
940941
if (!defined $hunk[$i]{USE}) {
941942
$next = 1;
942-
$other .= '/j';
943+
$other .= ',j';
943944
last;
944945
}
945946
}
946947
if ($ix < $num - 1) {
947-
$other .= '/J';
948+
$other .= ',J';
948949
}
949950
if ($num > 1) {
950-
$other .= '/g';
951+
$other .= ',g';
951952
}
952953
for ($i = 0; $i < $num; $i++) {
953954
if (!defined $hunk[$i]{USE}) {
@@ -958,13 +959,13 @@ sub patch_update_file {
958959
last if (!$undecided);
959960

960961
if (hunk_splittable($hunk[$ix]{TEXT})) {
961-
$other .= '/s';
962+
$other .= ',s';
962963
}
963-
$other .= '/e';
964+
$other .= ',e';
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) {
@@ -993,6 +994,9 @@ sub patch_update_file {
993994
}
994995
print "go to which hunk$extra? ";
995996
$response = <STDIN>;
997+
if (!defined $response) {
998+
$response = '';
999+
}
9961000
chomp $response;
9971001
}
9981002
if ($response !~ /^\s*\d+\s*$/) {
@@ -1013,30 +1017,68 @@ sub patch_update_file {
10131017
}
10141018
next;
10151019
}
1016-
elsif ($other =~ /K/ && $line =~ /^K/) {
1017-
$ix--;
1018-
next;
1019-
}
1020-
elsif ($other =~ /J/ && $line =~ /^J/) {
1021-
$ix++;
1020+
elsif ($line =~ m|^/(.*)|) {
1021+
my $search_string;
1022+
eval {
1023+
$search_string = qr{$1}m;
1024+
};
1025+
if ($@) {
1026+
my ($err,$exp) = ($@, $1);
1027+
$err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
1028+
print STDERR "Malformed search regexp $exp: $err\n";
1029+
next;
1030+
}
1031+
my $iy = $ix;
1032+
while (1) {
1033+
my $text = join ("", @{$hunk[$iy]{TEXT}});
1034+
last if ($text =~ $search_string);
1035+
$iy++;
1036+
$iy = 0 if ($iy >= $num);
1037+
if ($ix == $iy) {
1038+
print STDERR "No hunk matches the given pattern\n";
1039+
last;
1040+
}
1041+
}
1042+
$ix = $iy;
10221043
next;
10231044
}
1024-
elsif ($other =~ /k/ && $line =~ /^k/) {
1025-
while (1) {
1045+
elsif ($line =~ /^K/) {
1046+
if ($other =~ /K/) {
10261047
$ix--;
1027-
last if (!$ix ||
1028-
!defined $hunk[$ix]{USE});
1048+
}
1049+
else {
1050+
print STDERR "No previous hunk\n";
10291051
}
10301052
next;
10311053
}
1032-
elsif ($other =~ /j/ && $line =~ /^j/) {
1033-
while (1) {
1054+
elsif ($line =~ /^J/) {
1055+
if ($other =~ /J/) {
10341056
$ix++;
1035-
last if ($ix >= $num ||
1036-
!defined $hunk[$ix]{USE});
1057+
}
1058+
else {
1059+
print STDERR "No next hunk\n";
1060+
}
1061+
next;
1062+
}
1063+
elsif ($line =~ /^k/) {
1064+
if ($other =~ /k/) {
1065+
while (1) {
1066+
$ix--;
1067+
last if (!$ix ||
1068+
!defined $hunk[$ix]{USE});
1069+
}
1070+
}
1071+
else {
1072+
print STDERR "No previous hunk\n";
10371073
}
10381074
next;
10391075
}
1076+
elsif ($line =~ /^j/) {
1077+
if ($other !~ /j/) {
1078+
print STDERR "No next hunk\n";
1079+
next;
1080+
}
1081+
}
10401082
elsif ($other =~ /s/ && $line =~ /^s/) {
10411083
my @split = split_hunk($hunk[$ix]{TEXT}, $hunk[$ix]{DISPLAY});
10421084
if (1 < @split) {

0 commit comments

Comments
 (0)