Skip to content

Commit fb601ae

Browse files
committed
built-in add -p: implement the 'g' ("goto") command
With this patch, it is now possible to see a summary of the available hunks and to navigate between them (by number). A test is added to verify that this behavior matches the one of the Perl version of `git add -p`. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 30a2c35 commit fb601ae

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

add-patch.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,54 @@ static int edit_hunk_loop(struct add_p_state *s,
902902
}
903903
}
904904

905+
#define SUMMARY_HEADER_WIDTH 20
906+
#define SUMMARY_LINE_WIDTH 80
907+
static void summarize_hunk(struct add_p_state *s, struct hunk *hunk,
908+
struct strbuf *out)
909+
{
910+
struct hunk_header *header = &hunk->header;
911+
struct strbuf *plain = &s->plain;
912+
size_t len = out->len, i;
913+
914+
strbuf_addf(out, " -%lu,%lu +%lu,%lu ",
915+
header->old_offset, header->old_count,
916+
header->new_offset, header->new_count);
917+
if (out->len - len < SUMMARY_HEADER_WIDTH)
918+
strbuf_addchars(out, ' ',
919+
SUMMARY_HEADER_WIDTH + len - out->len);
920+
for (i = hunk->start; i < hunk->end; i = find_next_line(plain, i))
921+
if (plain->buf[i] != ' ')
922+
break;
923+
if (i < hunk->end)
924+
strbuf_add(out, plain->buf + i, find_next_line(plain, i) - i);
925+
if (out->len - len > SUMMARY_LINE_WIDTH)
926+
strbuf_setlen(out, len + SUMMARY_LINE_WIDTH);
927+
strbuf_complete_line(out);
928+
}
929+
930+
#define DISPLAY_HUNKS_LINES 20
931+
static size_t display_hunks(struct add_p_state *s,
932+
struct file_diff *file_diff, size_t start_index)
933+
{
934+
size_t end_index = start_index + DISPLAY_HUNKS_LINES;
935+
936+
if (end_index > file_diff->hunk_nr)
937+
end_index = file_diff->hunk_nr;
938+
939+
while (start_index < end_index) {
940+
struct hunk *hunk = file_diff->hunk + start_index++;
941+
942+
strbuf_reset(&s->buf);
943+
strbuf_addf(&s->buf, "%c%2d: ", hunk->use == USE_HUNK ? '+'
944+
: hunk->use == SKIP_HUNK ? '-' : ' ',
945+
(int)start_index);
946+
summarize_hunk(s, hunk, &s->buf);
947+
fputs(s->buf.buf, stdout);
948+
}
949+
950+
return end_index;
951+
}
952+
905953
static const char help_patch_text[] =
906954
N_("y - stage this hunk\n"
907955
"n - do not stage this hunk\n"
@@ -911,6 +959,7 @@ N_("y - stage this hunk\n"
911959
"J - leave this hunk undecided, see next hunk\n"
912960
"k - leave this hunk undecided, see previous undecided hunk\n"
913961
"K - leave this hunk undecided, see previous hunk\n"
962+
"g - select a hunk to go to\n"
914963
"s - split the current hunk into smaller hunks\n"
915964
"e - manually edit the current hunk\n"
916965
"? - print help\n");
@@ -969,6 +1018,8 @@ static int patch_update_file(struct add_p_state *s,
9691018
strbuf_addstr(&s->buf, ",j");
9701019
if (hunk_index + 1 < file_diff->hunk_nr)
9711020
strbuf_addstr(&s->buf, ",J");
1021+
if (file_diff->hunk_nr > 1)
1022+
strbuf_addstr(&s->buf, ",g");
9721023
if (hunk->splittable_into > 1)
9731024
strbuf_addstr(&s->buf, ",s");
9741025
if (hunk_index + 1 > file_diff->mode_change &&
@@ -1034,6 +1085,41 @@ static int patch_update_file(struct add_p_state *s,
10341085
hunk_index = undecided_next;
10351086
else
10361087
err(s, _("No next hunk"));
1088+
} else if (s->answer.buf[0] == 'g') {
1089+
char *pend;
1090+
unsigned long response;
1091+
1092+
if (file_diff->hunk_nr < 2) {
1093+
err(s, _("No other hunks to goto"));
1094+
continue;
1095+
}
1096+
strbuf_remove(&s->answer, 0, 1);
1097+
strbuf_trim(&s->answer);
1098+
i = hunk_index > 10 ? hunk_index - 10 : 0;
1099+
while (s->answer.len == 0) {
1100+
i = display_hunks(s, file_diff, i);
1101+
printf("%s", i < file_diff->hunk_nr ?
1102+
_("go to which hunk (<ret> to see "
1103+
"more)? ") : _("go to which hunk? "));
1104+
fflush(stdout);
1105+
if (strbuf_getline(&s->answer,
1106+
stdin) == EOF)
1107+
break;
1108+
strbuf_trim_trailing_newline(&s->answer);
1109+
}
1110+
1111+
strbuf_trim(&s->answer);
1112+
response = strtoul(s->answer.buf, &pend, 10);
1113+
if (*pend || pend == s->answer.buf)
1114+
err(s, _("Invalid number: '%s'"),
1115+
s->answer.buf);
1116+
else if (0 < response && response <= file_diff->hunk_nr)
1117+
hunk_index = response - 1;
1118+
else
1119+
err(s, Q_("Sorry, only %d hunk available.",
1120+
"Sorry, only %d hunks available.",
1121+
file_diff->hunk_nr),
1122+
(int)file_diff->hunk_nr);
10371123
} else if (s->answer.buf[0] == 's') {
10381124
size_t splittable_into = hunk->splittable_into;
10391125
if (splittable_into < 2)

t/t3701-add-interactive.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,22 @@ test_expect_success 'split hunk setup' '
413413
test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
414414
'
415415

416+
test_expect_success 'goto hunk' '
417+
test_when_finished "git reset" &&
418+
tr _ " " >expect <<-EOF &&
419+
Stage this hunk [y,n,q,a,d,K,g,/,e,?]? + 1: -1,2 +1,3 +15
420+
_ 2: -2,4 +3,8 +21
421+
go to which hunk? @@ -1,2 +1,3 @@
422+
_10
423+
+15
424+
_20
425+
Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
426+
EOF
427+
test_write_lines s y g 1 | git add -p >actual &&
428+
tail -n 7 <actual >actual.trimmed &&
429+
test_cmp expect actual.trimmed
430+
'
431+
416432
test_expect_success 'split hunk "add -p (edit)"' '
417433
# Split, say Edit and do nothing. Then:
418434
#

0 commit comments

Comments
 (0)