Skip to content

Commit 7a8fea2

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 fb601ae commit 7a8fea2

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