Skip to content

Commit 2ebe436

Browse files
phillipwoodgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 47ae905 commit 2ebe436

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

add-patch.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
457457
eol = pend;
458458

459459
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,
462461
file_diff_alloc);
463462
file_diff = s->file_diff + s->file_diff_nr - 1;
464-
memset(file_diff, 0, sizeof(*file_diff));
465463
hunk = &file_diff->head;
466464
hunk->start = p - plain->buf;
467465
if (colored_p)
@@ -483,11 +481,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
483481
*/
484482
hunk->splittable_into++;
485483

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,
488485
file_diff->hunk_alloc);
489486
hunk = file_diff->hunk + file_diff->hunk_nr - 1;
490-
memset(hunk, 0, sizeof(*hunk));
491487

492488
hunk->start = p - plain->buf;
493489
if (colored)
@@ -511,7 +507,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
511507
if (file_diff->mode_change)
512508
BUG("double mode change?\n\n%.*s",
513509
(int)(eol - plain->buf), plain->buf);
514-
if (file_diff->hunk_nr++)
510+
if (file_diff->hunk_nr)
515511
BUG("mode change in the middle?\n\n%.*s",
516512
(int)(eol - plain->buf), plain->buf);
517513

@@ -520,9 +516,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
520516
* is _part of_ the header "hunk".
521517
*/
522518
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,
524520
file_diff->hunk_alloc);
525-
memset(file_diff->hunk, 0, sizeof(struct hunk));
526521
file_diff->hunk->start = p - plain->buf;
527522
if (colored_p)
528523
file_diff->hunk->colored_start =

0 commit comments

Comments
 (0)