@matheusbsbs / matheus-bagliano / commits / 80399aec5a
Fork 0

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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Dec 13, 2019 at 08:07 UTC 80399aec5ab4dbcc4f3f964da4169174f1e0024e
1 file changed +53 -38
add-patch.c
+53 -38
@@ -29,9 +29,12 @@ struct add_p_state {
29
30 /* parsed diff */
31 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;
38 };
39
40 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)
134 struct strbuf *plain = &s->plain, *colored = NULL;
135 struct child_process cp = CHILD_PROCESS_INIT;
136 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;
139 struct hunk *hunk = NULL;
140 int res;
141
@@ -171,7 +175,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
175 }
176 argv_array_clear(&args);
177
174 - /* parse hunks */
178 + /* parse files and hunks */
179 p = plain->buf;
180 pend = p + plain->len;
181 while (p != pend) {
@@ -180,17 +184,23 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
184 eol = pend;
185
186 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;
196 } else if (p == plain->buf)
197 BUG("diff starts with unexpected line:\n"
198 "%.*s\n", (int)(eol - p), p);
199 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;
204 memset(hunk, 0, sizeof(*hunk));
205
206 hunk->start = p - plain->buf;
@@ -265,16 +275,17 @@ static void render_hunk(struct add_p_state *s, struct hunk *hunk,
275 hunk->end - hunk->start);
276 }
277
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)
280 {
281 struct hunk *hunk;
282 size_t i;
283 ssize_t delta = 0;
284
274 - render_hunk(s, &s->head, 0, 0, out);
285 + render_hunk(s, &file_diff->head, 0, 0, out);
286
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;
289 if (hunk->use != USE_HUNK)
290 delta += hunk->header.old_count
291 - hunk->header.new_count;
@@ -294,7 +305,8 @@ N_("y - stage this hunk\n"
305 "K - leave this hunk undecided, see previous hunk\n"
306 "? - print help\n");
307
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)
310 {
311 size_t hunk_index = 0;
312 ssize_t i, undecided_previous, undecided_next;
@@ -303,27 +315,27 @@ static int patch_update_file(struct add_p_state *s)
315 struct child_process cp = CHILD_PROCESS_INIT;
316 int colored = !!s->colored.len;
317
306 - if (!s->hunk_nr)
318 + if (!file_diff->hunk_nr)
319 return 0;
320
321 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);
323 fputs(s->buf.buf, stdout);
324 for (;;) {
313 - if (hunk_index >= s->hunk_nr)
325 + if (hunk_index >= file_diff->hunk_nr)
326 hunk_index = 0;
315 - hunk = s->hunk + hunk_index;
327 + hunk = file_diff->hunk + hunk_index;
328
329 undecided_previous = -1;
330 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) {
332 undecided_previous = i;
333 break;
334 }
335
336 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) {
339 undecided_next = i;
340 break;
341 }
@@ -344,11 +356,12 @@ static int patch_update_file(struct add_p_state *s)
356 strbuf_addstr(&s->buf, ",K");
357 if (undecided_next >= 0)
358 strbuf_addstr(&s->buf, ",j");
347 - if (hunk_index + 1 < s->hunk_nr)
359 + if (hunk_index + 1 < file_diff->hunk_nr)
360 strbuf_addstr(&s->buf, ",J");
361 color_fprintf(stdout, s->s.prompt_color,
362 "(%"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);
365 color_fprintf(stdout, s->s.prompt_color,
366 _("Stage this hunk [y,n,a,d%s,?]? "),
367 s->buf.buf);
@@ -364,19 +377,19 @@ static int patch_update_file(struct add_p_state *s)
377 hunk->use = USE_HUNK;
378 soft_increment:
379 hunk_index = undecided_next < 0 ?
367 - s->hunk_nr : undecided_next;
380 + file_diff->hunk_nr : undecided_next;
381 } else if (ch == 'n') {
382 hunk->use = SKIP_HUNK;
383 goto soft_increment;
384 } 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;
387 if (hunk->use == UNDECIDED_HUNK)
388 hunk->use = USE_HUNK;
389 }
390 } 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;
393 if (hunk->use == UNDECIDED_HUNK)
394 hunk->use = SKIP_HUNK;
395 }
@@ -386,7 +399,7 @@ soft_increment:
399 else
400 err(s, _("No previous hunk"));
401 } else if (s->answer.buf[0] == 'J') {
389 - if (hunk_index + 1 < s->hunk_nr)
402 + if (hunk_index + 1 < file_diff->hunk_nr)
403 hunk_index++;
404 else
405 err(s, _("No next hunk"));
@@ -406,14 +419,14 @@ soft_increment:
419 }
420
421 /* 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)
424 break;
425
413 - if (i < s->hunk_nr) {
426 + if (i < file_diff->hunk_nr) {
427 /* At least one hunk selected: apply */
428 strbuf_reset(&s->buf);
416 - reassemble_patch(s, &s->buf);
429 + reassemble_patch(s, file_diff, &s->buf);
430
431 discard_index(s->s.r->index);
432 setup_child_process(s, &cp, "apply", "--cached", NULL);
@@ -434,6 +447,7 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
447 struct add_p_state s = {
448 { r }, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
449 };
450 + size_t i;
451
452 init_add_i_state(&s.s, r);
453
@@ -446,8 +460,9 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
460 return -1;
461 }
462
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;
466
467 strbuf_release(&s.answer);
468 strbuf_release(&s.buf);