Skip to content

Commit 7fc8ca6

Browse files
committed
built-in add -p: implement the '/' ("search regex") command
This patch implements the hunk searching feature in the C version of `git add -p`. A test is added to verify that this behavior matches the one of the Perl version of `git add -p`. Note that this involves a change of behavior: the Perl version uses (of course) the Perl flavor of regular expressions, while this patch uses the regcomp()/regexec(), i.e. POSIX extended regular expressions. In practice, this behavior change is unlikely to matter. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent df71e7f commit 7fc8ca6

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

add-patch.c

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,7 @@ N_("y - stage this hunk\n"
961961
"k - leave this hunk undecided, see previous undecided hunk\n"
962962
"K - leave this hunk undecided, see previous hunk\n"
963963
"g - select a hunk to go to\n"
964+
"/ - search for a hunk matching the given regex\n"
964965
"s - split the current hunk into smaller hunks\n"
965966
"e - manually edit the current hunk\n"
966967
"? - print help\n");
@@ -1020,7 +1021,7 @@ static int patch_update_file(struct add_p_state *s,
10201021
if (hunk_index + 1 < file_diff->hunk_nr)
10211022
strbuf_addstr(&s->buf, ",J");
10221023
if (file_diff->hunk_nr > 1)
1023-
strbuf_addstr(&s->buf, ",g");
1024+
strbuf_addstr(&s->buf, ",g,/");
10241025
if (hunk->splittable_into > 1)
10251026
strbuf_addstr(&s->buf, ",s");
10261027
if (hunk_index + 1 > file_diff->mode_change &&
@@ -1121,6 +1122,53 @@ static int patch_update_file(struct add_p_state *s,
11211122
"Sorry, only %d hunks available.",
11221123
file_diff->hunk_nr),
11231124
(int)file_diff->hunk_nr);
1125+
} else if (s->answer.buf[0] == '/') {
1126+
regex_t regex;
1127+
int ret;
1128+
1129+
if (file_diff->hunk_nr < 2) {
1130+
err(s, _("No other hunks to search"));
1131+
continue;
1132+
}
1133+
strbuf_remove(&s->answer, 0, 1);
1134+
strbuf_trim_trailing_newline(&s->answer);
1135+
if (s->answer.len == 0) {
1136+
printf("%s", _("search for regex? "));
1137+
fflush(stdout);
1138+
if (strbuf_getline(&s->answer,
1139+
stdin) == EOF)
1140+
break;
1141+
strbuf_trim_trailing_newline(&s->answer);
1142+
if (s->answer.len == 0)
1143+
continue;
1144+
}
1145+
ret = regcomp(&regex, s->answer.buf,
1146+
REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
1147+
if (ret) {
1148+
char errbuf[1024];
1149+
1150+
regerror(ret, &regex, errbuf, sizeof(errbuf));
1151+
err(s, _("Malformed search regexp %s: %s"),
1152+
s->answer.buf, errbuf);
1153+
continue;
1154+
}
1155+
i = hunk_index;
1156+
for (;;) {
1157+
/* render the hunk into a scratch buffer */
1158+
render_hunk(s, file_diff->hunk + i, 0, 0,
1159+
&s->buf);
1160+
if (regexec(&regex, s->buf.buf, 0, NULL, 0)
1161+
!= REG_NOMATCH)
1162+
break;
1163+
i++;
1164+
if (i == file_diff->hunk_nr)
1165+
i = 0;
1166+
if (i != hunk_index)
1167+
continue;
1168+
err(s, _("No hunk matches the given pattern"));
1169+
break;
1170+
}
1171+
hunk_index = i;
11241172
} else if (s->answer.buf[0] == 's') {
11251173
size_t splittable_into = hunk->splittable_into;
11261174
if (splittable_into < 2)

t/t3701-add-interactive.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,20 @@ test_expect_success 'goto hunk' '
429429
test_cmp expect actual.trimmed
430430
'
431431

432+
test_expect_success 'navigate to hunk via regex' '
433+
test_when_finished "git reset" &&
434+
tr _ " " >expect <<-EOF &&
435+
Stage this hunk [y,n,q,a,d,K,g,/,e,?]? @@ -1,2 +1,3 @@
436+
_10
437+
+15
438+
_20
439+
Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
440+
EOF
441+
test_write_lines s y /1,2 | git add -p >actual &&
442+
tail -n 5 <actual >actual.trimmed &&
443+
test_cmp expect actual.trimmed
444+
'
445+
432446
test_expect_success 'split hunk "add -p (edit)"' '
433447
# Split, say Edit and do nothing. Then:
434448
#

0 commit comments

Comments
 (0)