@matheusbsbs / matheus-bagliano / commits / 2ebe436c55
Fork 0

add -p: use ALLOC_GROW_BY instead of ALLOW_GROW

This simplifies the code slightly, especially the third case where hunk_nr was incremented a few lines before ALLOC_GROW(). Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Aug 17, 2020 at 13:23 UTC 2ebe436c5531319e820571d04024f7bcae383450
1 file changed +4 -9
add-patch.c
+4 -9
@@ -457,11 +457,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
457 eol = pend;
458
459 if (starts_with(p, "diff ")) {
460 - s->file_diff_nr++;
461 - ALLOC_GROW(s->file_diff, s->file_diff_nr,
460 + ALLOC_GROW_BY(s->file_diff, s->file_diff_nr, 1,
461 file_diff_alloc);
462 file_diff = s->file_diff + s->file_diff_nr - 1;
464 - memset(file_diff, 0, sizeof(*file_diff));
463 hunk = &file_diff->head;
464 hunk->start = p - plain->buf;
465 if (colored_p)
@@ -483,11 +481,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
481 */
482 hunk->splittable_into++;
483
486 - file_diff->hunk_nr++;
487 - ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr,
484 + ALLOC_GROW_BY(file_diff->hunk, file_diff->hunk_nr, 1,
485 file_diff->hunk_alloc);
486 hunk = file_diff->hunk + file_diff->hunk_nr - 1;
490 - memset(hunk, 0, sizeof(*hunk));
487
488 hunk->start = p - plain->buf;
489 if (colored)
@@ -511,7 +507,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
507 if (file_diff->mode_change)
508 BUG("double mode change?\n\n%.*s",
509 (int)(eol - plain->buf), plain->buf);
514 - if (file_diff->hunk_nr++)
510 + if (file_diff->hunk_nr)
511 BUG("mode change in the middle?\n\n%.*s",
512 (int)(eol - plain->buf), plain->buf);
513
@@ -520,9 +516,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
516 * is _part of_ the header "hunk".
517 */
518 file_diff->mode_change = 1;
523 - ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr,
519 + ALLOC_GROW_BY(file_diff->hunk, file_diff->hunk_nr, 1,
520 file_diff->hunk_alloc);
525 - memset(file_diff->hunk, 0, sizeof(struct hunk));
521 file_diff->hunk->start = p - plain->buf;
522 if (colored_p)
523 file_diff->hunk->colored_start =