revisions API users: add "goto cleanup" for release_revisions()

Add a release_revisions() to various users of "struct rev_info" which requires a minor refactoring to a "goto cleanup" pattern to use that function. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Apr 13, 2022 at 22:01 UTC 0139c58ab951e8620d6066eb687d0a96e490436a
6 files changed +55 -24
builtin/diff-files.c
+6 -2
@@ -77,8 +77,12 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
77
78 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
79 perror("read_cache_preload");
80 - return -1;
80 + result = -1;
81 + goto cleanup;
82 }
83 +cleanup:
84 result = run_diff_files(&rev, options);
83 - return diff_result_code(&rev.diffopt, result);
85 + result = diff_result_code(&rev.diffopt, result);
86 + release_revisions(&rev);
87 + return result;
88 }
builtin/rev-list.c
+12 -7
@@ -500,6 +500,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
500 int use_bitmap_index = 0;
501 int filter_provided_objects = 0;
502 const char *show_progress = NULL;
503 + int ret = 0;
504
505 if (argc == 2 && !strcmp(argv[1], "-h"))
506 usage(rev_list_usage);
@@ -583,7 +584,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
584 }
585 if (!strcmp(arg, "--test-bitmap")) {
586 test_bitmap_walk(&revs);
586 - return 0;
587 + goto cleanup;
588 }
589 if (skip_prefix(arg, "--progress=", &arg)) {
590 show_progress = arg;
@@ -672,11 +673,11 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
673
674 if (use_bitmap_index) {
675 if (!try_bitmap_count(&revs, filter_provided_objects))
675 - return 0;
676 + goto cleanup;
677 if (!try_bitmap_disk_usage(&revs, filter_provided_objects))
677 - return 0;
678 + goto cleanup;
679 if (!try_bitmap_traversal(&revs, filter_provided_objects))
679 - return 0;
680 + goto cleanup;
681 }
682
683 if (prepare_revision_walk(&revs))
@@ -696,8 +697,10 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
697
698 find_bisection(&revs.commits, &reaches, &all, bisect_flags);
699
699 - if (bisect_show_vars)
700 - return show_bisect_vars(&info, reaches, all);
700 + if (bisect_show_vars) {
701 + ret = show_bisect_vars(&info, reaches, all);
702 + goto cleanup;
703 + }
704 }
705
706 if (filter_provided_objects) {
@@ -752,5 +755,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
755 if (show_disk_usage)
756 printf("%"PRIuMAX"\n", (uintmax_t)total_disk_usage);
757
755 - return 0;
758 +cleanup:
759 + release_revisions(&revs);
760 + return ret;
761 }
builtin/stash.c
+1
@@ -917,6 +917,7 @@ static int show_stash(int argc, const char **argv, const char *prefix)
917 cleanup:
918 strvec_clear(&stash_args);
919 free_stash_info(&info);
920 + release_revisions(&rev);
921 if (do_usage)
922 usage_with_options(git_stash_show_usage, options);
923 return ret;
builtin/submodule--helper.c
+7 -3
@@ -1232,6 +1232,7 @@ static int compute_summary_module_list(struct object_id *head_oid,
1232 struct strvec diff_args = STRVEC_INIT;
1233 struct rev_info rev;
1234 struct module_cb_list list = MODULE_CB_LIST_INIT;
1235 + int ret = 0;
1236
1237 strvec_push(&diff_args, get_diff_cmd(diff_cmd));
1238 if (info->cached)
@@ -1257,11 +1258,13 @@ static int compute_summary_module_list(struct object_id *head_oid,
1258 setup_work_tree();
1259 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1260 perror("read_cache_preload");
1260 - return -1;
1261 + ret = -1;
1262 + goto cleanup;
1263 }
1264 } else if (read_cache() < 0) {
1265 perror("read_cache");
1264 - return -1;
1266 + ret = -1;
1267 + goto cleanup;
1268 }
1269
1270 if (diff_cmd == DIFF_INDEX)
@@ -1269,9 +1272,10 @@ static int compute_summary_module_list(struct object_id *head_oid,
1272 else
1273 run_diff_files(&rev, 0);
1274 prepare_submodule_summary(info, &list);
1275 +cleanup:
1276 strvec_clear(&diff_args);
1277 release_revisions(&rev);
1274 - return 0;
1278 + return ret;
1279 }
1280
1281 static int module_summary(int argc, const char **argv, const char *prefix)
sequencer.c
+16 -7
@@ -5354,6 +5354,7 @@ int sequencer_make_script(struct repository *r, struct strbuf *out, int argc,
5354 int rebase_merges = flags & TODO_LIST_REBASE_MERGES;
5355 int reapply_cherry_picks = flags & TODO_LIST_REAPPLY_CHERRY_PICKS;
5356 int skipped_commit = 0;
5357 + int ret = 0;
5358
5359 repo_init_revisions(r, &revs, NULL);
5360 revs.verbose_header = 1;
@@ -5377,14 +5378,20 @@ int sequencer_make_script(struct repository *r, struct strbuf *out, int argc,
5378 pp.fmt = revs.commit_format;
5379 pp.output_encoding = get_log_output_encoding();
5380
5380 - if (setup_revisions(argc, argv, &revs, NULL) > 1)
5381 - return error(_("make_script: unhandled options"));
5381 + if (setup_revisions(argc, argv, &revs, NULL) > 1) {
5382 + ret = error(_("make_script: unhandled options"));
5383 + goto cleanup;
5384 + }
5385
5383 - if (prepare_revision_walk(&revs) < 0)
5384 - return error(_("make_script: error preparing revisions"));
5386 + if (prepare_revision_walk(&revs) < 0) {
5387 + ret = error(_("make_script: error preparing revisions"));
5388 + goto cleanup;
5389 + }
5390
5386 - if (rebase_merges)
5387 - return make_script_with_merges(&pp, &revs, out, flags);
5391 + if (rebase_merges) {
5392 + ret = make_script_with_merges(&pp, &revs, out, flags);
5393 + goto cleanup;
5394 + }
5395
5396 while ((commit = get_revision(&revs))) {
5397 int is_empty = is_original_commit_empty(commit);
@@ -5408,7 +5415,9 @@ int sequencer_make_script(struct repository *r, struct strbuf *out, int argc,
5415 if (skipped_commit)
5416 advise_if_enabled(ADVICE_SKIPPED_CHERRY_PICKS,
5417 _("use --reapply-cherry-picks to include skipped commits"));
5411 - return 0;
5418 +cleanup:
5419 + release_revisions(&revs);
5420 + return ret;
5421 }
5422
5423 /*
t/helper/test-fast-rebase.c
+13 -5
@@ -99,6 +99,7 @@ int cmd__fast_rebase(int argc, const char **argv)
99 struct merge_result result;
100 struct strbuf reflog_msg = STRBUF_INIT;
101 struct strbuf branch_name = STRBUF_INIT;
102 + int ret = 0;
103
104 /*
105 * test-tool stuff doesn't set up the git directory by default; need to
@@ -137,13 +138,17 @@ int cmd__fast_rebase(int argc, const char **argv)
138 revs.topo_order = 1;
139 strvec_pushl(&rev_walk_args, "", argv[4], "--not", argv[3], NULL);
140
140 - if (setup_revisions(rev_walk_args.nr, rev_walk_args.v, &revs, NULL) > 1)
141 - return error(_("unhandled options"));
141 + if (setup_revisions(rev_walk_args.nr, rev_walk_args.v, &revs, NULL) > 1) {
142 + ret = error(_("unhandled options"));
143 + goto cleanup;
144 + }
145
146 strvec_clear(&rev_walk_args);
147
145 - if (prepare_revision_walk(&revs) < 0)
146 - return error(_("error preparing revisions"));
148 + if (prepare_revision_walk(&revs) < 0) {
149 + ret = error(_("error preparing revisions"));
150 + goto cleanup;
151 + }
152
153 init_merge_options(&merge_opt, the_repository);
154 memset(&result, 0, sizeof(result));
@@ -220,7 +225,10 @@ int cmd__fast_rebase(int argc, const char **argv)
225 COMMIT_LOCK | SKIP_IF_UNCHANGED))
226 die(_("unable to write %s"), get_index_file());
227
228 + ret = (result.clean == 0);
229 +cleanup:
230 strbuf_release(&reflog_msg);
231 strbuf_release(&branch_name);
225 - return (result.clean == 0);
232 + release_revisions(&revs);
233 + return ret;
234 }