Skip to content

Commit 80399ae

Browse files
dschogitster
authored andcommitted
built-in add -p: support multi-file diffs
For simplicity, the initial implementation in C handled only a single modified file. Now it handles an arbitrary number of files. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7584dd3 commit 80399ae

File tree

1 file changed

+53
-38
lines changed

1 file changed

+53
-38
lines changed

add-patch.c

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ struct add_p_state {
2929

3030
/* parsed diff */
3131
struct strbuf plain, colored;
32-
struct hunk head;
33-
struct hunk *hunk;
34-
size_t hunk_nr, hunk_alloc;
32+
struct file_diff {
33+
struct hunk head;
34+
struct hunk *hunk;
35+
size_t hunk_nr, hunk_alloc;
36+
} *file_diff;
37+
size_t file_diff_nr;
3538
};
3639

3740
static void err(struct add_p_state *s, const char *fmt, ...)
@@ -131,7 +134,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
131134
struct strbuf *plain = &s->plain, *colored = NULL;
132135
struct child_process cp = CHILD_PROCESS_INIT;
133136
char *p, *pend, *colored_p = NULL, *colored_pend = NULL;
134-
size_t i, color_arg_index;
137+
size_t file_diff_alloc = 0, i, color_arg_index;
138+
struct file_diff *file_diff = NULL;
135139
struct hunk *hunk = NULL;
136140
int res;
137141

@@ -171,7 +175,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
171175
}
172176
argv_array_clear(&args);
173177

174-
/* parse hunks */
178+
/* parse files and hunks */
175179
p = plain->buf;
176180
pend = p + plain->len;
177181
while (p != pend) {
@@ -180,17 +184,23 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
180184
eol = pend;
181185

182186
if (starts_with(p, "diff ")) {
183-
if (p != plain->buf)
184-
BUG("multi-file diff not yet handled");
185-
hunk = &s->head;
187+
s->file_diff_nr++;
188+
ALLOC_GROW(s->file_diff, s->file_diff_nr,
189+
file_diff_alloc);
190+
file_diff = s->file_diff + s->file_diff_nr - 1;
191+
memset(file_diff, 0, sizeof(*file_diff));
192+
hunk = &file_diff->head;
193+
hunk->start = p - plain->buf;
194+
if (colored_p)
195+
hunk->colored_start = colored_p - colored->buf;
186196
} else if (p == plain->buf)
187197
BUG("diff starts with unexpected line:\n"
188198
"%.*s\n", (int)(eol - p), p);
189199
else if (starts_with(p, "@@ ")) {
190-
s->hunk_nr++;
191-
ALLOC_GROW(s->hunk, s->hunk_nr,
192-
s->hunk_alloc);
193-
hunk = s->hunk + s->hunk_nr - 1;
200+
file_diff->hunk_nr++;
201+
ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr,
202+
file_diff->hunk_alloc);
203+
hunk = file_diff->hunk + file_diff->hunk_nr - 1;
194204
memset(hunk, 0, sizeof(*hunk));
195205

196206
hunk->start = p - plain->buf;
@@ -265,16 +275,17 @@ static void render_hunk(struct add_p_state *s, struct hunk *hunk,
265275
hunk->end - hunk->start);
266276
}
267277

268-
static void reassemble_patch(struct add_p_state *s, struct strbuf *out)
278+
static void reassemble_patch(struct add_p_state *s,
279+
struct file_diff *file_diff, struct strbuf *out)
269280
{
270281
struct hunk *hunk;
271282
size_t i;
272283
ssize_t delta = 0;
273284

274-
render_hunk(s, &s->head, 0, 0, out);
285+
render_hunk(s, &file_diff->head, 0, 0, out);
275286

276-
for (i = 0; i < s->hunk_nr; i++) {
277-
hunk = s->hunk + i;
287+
for (i = 0; i < file_diff->hunk_nr; i++) {
288+
hunk = file_diff->hunk + i;
278289
if (hunk->use != USE_HUNK)
279290
delta += hunk->header.old_count
280291
- hunk->header.new_count;
@@ -294,7 +305,8 @@ N_("y - stage this hunk\n"
294305
"K - leave this hunk undecided, see previous hunk\n"
295306
"? - print help\n");
296307

297-
static int patch_update_file(struct add_p_state *s)
308+
static int patch_update_file(struct add_p_state *s,
309+
struct file_diff *file_diff)
298310
{
299311
size_t hunk_index = 0;
300312
ssize_t i, undecided_previous, undecided_next;
@@ -303,27 +315,27 @@ static int patch_update_file(struct add_p_state *s)
303315
struct child_process cp = CHILD_PROCESS_INIT;
304316
int colored = !!s->colored.len;
305317

306-
if (!s->hunk_nr)
318+
if (!file_diff->hunk_nr)
307319
return 0;
308320

309321
strbuf_reset(&s->buf);
310-
render_hunk(s, &s->head, 0, colored, &s->buf);
322+
render_hunk(s, &file_diff->head, 0, colored, &s->buf);
311323
fputs(s->buf.buf, stdout);
312324
for (;;) {
313-
if (hunk_index >= s->hunk_nr)
325+
if (hunk_index >= file_diff->hunk_nr)
314326
hunk_index = 0;
315-
hunk = s->hunk + hunk_index;
327+
hunk = file_diff->hunk + hunk_index;
316328

317329
undecided_previous = -1;
318330
for (i = hunk_index - 1; i >= 0; i--)
319-
if (s->hunk[i].use == UNDECIDED_HUNK) {
331+
if (file_diff->hunk[i].use == UNDECIDED_HUNK) {
320332
undecided_previous = i;
321333
break;
322334
}
323335

324336
undecided_next = -1;
325-
for (i = hunk_index + 1; i < s->hunk_nr; i++)
326-
if (s->hunk[i].use == UNDECIDED_HUNK) {
337+
for (i = hunk_index + 1; i < file_diff->hunk_nr; i++)
338+
if (file_diff->hunk[i].use == UNDECIDED_HUNK) {
327339
undecided_next = i;
328340
break;
329341
}
@@ -344,11 +356,12 @@ static int patch_update_file(struct add_p_state *s)
344356
strbuf_addstr(&s->buf, ",K");
345357
if (undecided_next >= 0)
346358
strbuf_addstr(&s->buf, ",j");
347-
if (hunk_index + 1 < s->hunk_nr)
359+
if (hunk_index + 1 < file_diff->hunk_nr)
348360
strbuf_addstr(&s->buf, ",J");
349361
color_fprintf(stdout, s->s.prompt_color,
350362
"(%"PRIuMAX"/%"PRIuMAX") ",
351-
(uintmax_t)hunk_index + 1, (uintmax_t)s->hunk_nr);
363+
(uintmax_t)hunk_index + 1,
364+
(uintmax_t)file_diff->hunk_nr);
352365
color_fprintf(stdout, s->s.prompt_color,
353366
_("Stage this hunk [y,n,a,d%s,?]? "),
354367
s->buf.buf);
@@ -364,19 +377,19 @@ static int patch_update_file(struct add_p_state *s)
364377
hunk->use = USE_HUNK;
365378
soft_increment:
366379
hunk_index = undecided_next < 0 ?
367-
s->hunk_nr : undecided_next;
380+
file_diff->hunk_nr : undecided_next;
368381
} else if (ch == 'n') {
369382
hunk->use = SKIP_HUNK;
370383
goto soft_increment;
371384
} else if (ch == 'a') {
372-
for (; hunk_index < s->hunk_nr; hunk_index++) {
373-
hunk = s->hunk + hunk_index;
385+
for (; hunk_index < file_diff->hunk_nr; hunk_index++) {
386+
hunk = file_diff->hunk + hunk_index;
374387
if (hunk->use == UNDECIDED_HUNK)
375388
hunk->use = USE_HUNK;
376389
}
377390
} else if (ch == 'd') {
378-
for (; hunk_index < s->hunk_nr; hunk_index++) {
379-
hunk = s->hunk + hunk_index;
391+
for (; hunk_index < file_diff->hunk_nr; hunk_index++) {
392+
hunk = file_diff->hunk + hunk_index;
380393
if (hunk->use == UNDECIDED_HUNK)
381394
hunk->use = SKIP_HUNK;
382395
}
@@ -386,7 +399,7 @@ static int patch_update_file(struct add_p_state *s)
386399
else
387400
err(s, _("No previous hunk"));
388401
} else if (s->answer.buf[0] == 'J') {
389-
if (hunk_index + 1 < s->hunk_nr)
402+
if (hunk_index + 1 < file_diff->hunk_nr)
390403
hunk_index++;
391404
else
392405
err(s, _("No next hunk"));
@@ -406,14 +419,14 @@ static int patch_update_file(struct add_p_state *s)
406419
}
407420

408421
/* Any hunk to be used? */
409-
for (i = 0; i < s->hunk_nr; i++)
410-
if (s->hunk[i].use == USE_HUNK)
422+
for (i = 0; i < file_diff->hunk_nr; i++)
423+
if (file_diff->hunk[i].use == USE_HUNK)
411424
break;
412425

413-
if (i < s->hunk_nr) {
426+
if (i < file_diff->hunk_nr) {
414427
/* At least one hunk selected: apply */
415428
strbuf_reset(&s->buf);
416-
reassemble_patch(s, &s->buf);
429+
reassemble_patch(s, file_diff, &s->buf);
417430

418431
discard_index(s->s.r->index);
419432
setup_child_process(s, &cp, "apply", "--cached", NULL);
@@ -434,6 +447,7 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
434447
struct add_p_state s = {
435448
{ r }, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
436449
};
450+
size_t i;
437451

438452
init_add_i_state(&s.s, r);
439453

@@ -446,8 +460,9 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
446460
return -1;
447461
}
448462

449-
if (s.hunk_nr)
450-
patch_update_file(&s);
463+
for (i = 0; i < s.file_diff_nr; i++)
464+
if (patch_update_file(&s, s.file_diff + i))
465+
break;
451466

452467
strbuf_release(&s.answer);
453468
strbuf_release(&s.buf);

0 commit comments

Comments
 (0)