log: use commit_stack

Calling commit_stack_push() to add commits is simpler and more efficient than using REALLOC_ARRAY. Calling commit_stack_pop() to consume them in LIFO order is also a tad simpler than calculating the array index from the end. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Dec 24, 2025 at 18:03 UTC 052efdd60f860dc5bc50a92f10911402d9cc71b4
1 file changed +15 -16
builtin/log.c
+15 -16
@@ -1896,11 +1896,11 @@ int cmd_format_patch(int argc,
1896 {
1897 struct format_config cfg;
1898 struct commit *commit;
1899 - struct commit **list = NULL;
1899 + struct commit_stack list = COMMIT_STACK_INIT;
1900 struct rev_info rev;
1901 char *to_free = NULL;
1902 struct setup_revision_opt s_r_opt;
1903 - size_t nr = 0, total, i;
1903 + size_t total, i;
1904 int use_stdout = 0;
1905 int start_number = -1;
1906 int just_numbers = 0;
@@ -2283,14 +2283,12 @@ int cmd_format_patch(int argc,
2283 if (ignore_if_in_upstream && has_commit_patch_id(commit, &ids))
2284 continue;
2285
2286 - nr++;
2287 - REALLOC_ARRAY(list, nr);
2288 - list[nr - 1] = commit;
2286 + commit_stack_push(&list, commit);
2287 }
2290 - if (nr == 0)
2288 + if (!list.nr)
2289 /* nothing to do */
2290 goto done;
2293 - total = nr;
2291 + total = list.nr;
2292 if (cover_letter == -1) {
2293 if (cfg.config_cover_letter == COVER_AUTO)
2294 cover_letter = (total > 1);
@@ -2308,7 +2306,7 @@ int cmd_format_patch(int argc,
2306 if (!cover_letter && total != 1)
2307 die(_("--interdiff requires --cover-letter or single patch"));
2308 rev.idiff_oid1 = &idiff_prev.oid[idiff_prev.nr - 1];
2311 - rev.idiff_oid2 = get_commit_tree_oid(list[0]);
2309 + rev.idiff_oid2 = get_commit_tree_oid(list.items[0]);
2310 rev.idiff_title = diff_title(&idiff_title, reroll_count,
2311 _("Interdiff:"),
2312 _("Interdiff against v%d:"));
@@ -2324,7 +2322,7 @@ int cmd_format_patch(int argc,
2322 die(_("--range-diff requires --cover-letter or single patch"));
2323
2324 infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev,
2327 - origin, list[0]);
2325 + origin, list.items[0]);
2326 rev.rdiff1 = rdiff1.buf;
2327 rev.rdiff2 = rdiff2.buf;
2328 rev.creation_factor = creation_factor;
@@ -2360,11 +2358,11 @@ int cmd_format_patch(int argc,
2358 }
2359
2360 memset(&bases, 0, sizeof(bases));
2363 - base = get_base_commit(&cfg, list, nr);
2361 + base = get_base_commit(&cfg, list.items, list.nr);
2362 if (base) {
2363 reset_revision_walk();
2364 clear_object_flags(the_repository, UNINTERESTING);
2367 - prepare_bases(&bases, base, list, nr);
2365 + prepare_bases(&bases, base, list.items, list.nr);
2366 }
2367
2368 if (in_reply_to || cfg.thread || cover_letter) {
@@ -2381,7 +2379,8 @@ int cmd_format_patch(int argc,
2379 if (cfg.thread)
2380 gen_message_id(&rev, "cover");
2381 make_cover_letter(&rev, !!output_directory,
2384 - origin, nr, list, description_file, branch_name, quiet, &cfg);
2382 + origin, list.nr, list.items,
2383 + description_file, branch_name, quiet, &cfg);
2384 print_bases(&bases, rev.diffopt.file);
2385 print_signature(signature, rev.diffopt.file);
2386 total++;
@@ -2395,12 +2394,12 @@ int cmd_format_patch(int argc,
2394 if (show_progress)
2395 progress = start_delayed_progress(the_repository,
2396 _("Generating patches"), total);
2398 - for (i = 0; i < nr; i++) {
2399 - size_t idx = nr - i - 1;
2397 + while (list.nr) {
2398 + size_t idx = list.nr - 1;
2399 int shown;
2400
2401 display_progress(progress, total - idx);
2403 - commit = list[idx];
2402 + commit = commit_stack_pop(&list);
2403 rev.nr = total - idx + (start_number - 1);
2404
2405 /* Make the second and subsequent mails replies to the first */
@@ -2469,7 +2468,7 @@ int cmd_format_patch(int argc,
2468 }
2469 }
2470 stop_progress(&progress);
2472 - free(list);
2471 + commit_stack_clear(&list);
2472 if (ignore_if_in_upstream)
2473 free_patch_ids(&ids);
2474