Skip to content

Commit f196c1e

Browse files
avargitster
authored andcommitted
revisions API users: use release_revisions() needing REV_INFO_INIT
Use release_revisions() to various users of "struct rev_list" which need to have their "struct rev_info" zero-initialized before we can start using it. For the bundle.c code see the early exit case added in 3bbbe46 (bundle verify: error out if called without an object database, 2019-05-27). For the relevant bisect.c code see 45b6370 (bisect: libify `check_good_are_ancestors_of_bad` and its dependents, 2020-02-17). For the submodule.c code see the "goto" on "(!left || !right || !sub)" added in 8e6df65 (submodule: refactor show_submodule_summary with helper function, 2016-08-31). Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 296a143 commit f196c1e

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

bisect.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ void read_bisect_terms(const char **read_bad, const char **read_good)
10101010
*/
10111011
enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
10121012
{
1013-
struct rev_info revs;
1013+
struct rev_info revs = REV_INFO_INIT;
10141014
struct commit_list *tried;
10151015
int reaches = 0, all = 0, nr, steps;
10161016
enum bisect_error res = BISECT_OK;
@@ -1035,7 +1035,7 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
10351035

10361036
res = check_good_are_ancestors_of_bad(r, prefix, no_checkout);
10371037
if (res)
1038-
return res;
1038+
goto cleanup;
10391039

10401040
bisect_rev_setup(r, &revs, prefix, "%s", "^%s", 1);
10411041

@@ -1060,14 +1060,16 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
10601060
term_good,
10611061
term_bad);
10621062

1063-
return BISECT_FAILED;
1063+
res = BISECT_FAILED;
1064+
goto cleanup;
10641065
}
10651066

10661067
if (!all) {
10671068
fprintf(stderr, _("No testable commit found.\n"
10681069
"Maybe you started with bad path arguments?\n"));
10691070

1070-
return BISECT_NO_TESTABLE_COMMIT;
1071+
res = BISECT_NO_TESTABLE_COMMIT;
1072+
goto cleanup;
10711073
}
10721074

10731075
bisect_rev = &revs.commits->item->object.oid;
@@ -1087,7 +1089,8 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
10871089
* for negative return values for early returns up
10881090
* until the cmd_bisect__helper() caller.
10891091
*/
1090-
return BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
1092+
res = BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
1093+
goto cleanup;
10911094
}
10921095

10931096
nr = all - reaches - 1;
@@ -1106,7 +1109,10 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
11061109
/* Clean up objects used, as they will be reused. */
11071110
repo_clear_commit_marks(r, ALL_REV_FLAGS);
11081111

1109-
return bisect_checkout(bisect_rev, no_checkout);
1112+
res = bisect_checkout(bisect_rev, no_checkout);
1113+
cleanup:
1114+
release_revisions(&revs);
1115+
return res;
11101116
}
11111117

11121118
static inline int log2i(int n)

builtin/submodule--helper.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
766766
{
767767
char *displaypath;
768768
struct strvec diff_files_args = STRVEC_INIT;
769-
struct rev_info rev;
769+
struct rev_info rev = REV_INFO_INIT;
770770
int diff_files_result;
771771
struct strbuf buf = STRBUF_INIT;
772772
const char *git_dir;
@@ -853,6 +853,7 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
853853
cleanup:
854854
strvec_clear(&diff_files_args);
855855
free(displaypath);
856+
release_revisions(&rev);
856857
}
857858

858859
static void status_submodule_cb(const struct cache_entry *list_item,

bundle.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,16 @@ int verify_bundle(struct repository *r,
196196
* to be verbose about the errors
197197
*/
198198
struct string_list *p = &header->prerequisites;
199-
struct rev_info revs;
199+
struct rev_info revs = REV_INFO_INIT;
200200
const char *argv[] = {NULL, "--all", NULL};
201201
struct commit *commit;
202202
int i, ret = 0, req_nr;
203203
const char *message = _("Repository lacks these prerequisite commits:");
204204

205-
if (!r || !r->objects || !r->objects->odb)
206-
return error(_("need a repository to verify a bundle"));
205+
if (!r || !r->objects || !r->objects->odb) {
206+
ret = error(_("need a repository to verify a bundle"));
207+
goto cleanup;
208+
}
207209

208210
repo_init_revisions(r, &revs, NULL);
209211
for (i = 0; i < p->nr; i++) {
@@ -221,7 +223,7 @@ int verify_bundle(struct repository *r,
221223
error("%s %s", oid_to_hex(oid), name);
222224
}
223225
if (revs.pending.nr != p->nr)
224-
return ret;
226+
goto cleanup;
225227
req_nr = revs.pending.nr;
226228
setup_revisions(2, argv, &revs, NULL);
227229

@@ -284,6 +286,8 @@ int verify_bundle(struct repository *r,
284286
printf_ln("The bundle uses this filter: %s",
285287
list_objects_filter_spec(&header->filter));
286288
}
289+
cleanup:
290+
release_revisions(&revs);
287291
return ret;
288292
}
289293

revision.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,25 @@ struct rev_info {
329329
struct tmp_objdir *remerge_objdir;
330330
};
331331

332+
/**
333+
* Initialize the "struct rev_info" structure with a macro.
334+
*
335+
* This will not fully initialize a "struct rev_info", the
336+
* repo_init_revisions() function needs to be called before
337+
* setup_revisions() and any revision walking takes place.
338+
*
339+
* Use REV_INFO_INIT to make the "struct rev_info" safe for passing to
340+
* release_revisions() when it's inconvenient (e.g. due to a "goto
341+
* cleanup" pattern) to arrange for repo_init_revisions() to be called
342+
* before release_revisions() is called.
343+
*
344+
* Initializing with this REV_INFO_INIT is redundant to invoking
345+
* repo_init_revisions(). If repo_init_revisions() is guaranteed to be
346+
* called before release_revisions() the "struct rev_info" can be left
347+
* uninitialized.
348+
*/
349+
#define REV_INFO_INIT { 0 }
350+
332351
/**
333352
* Initialize a rev_info structure with default values. The third parameter may
334353
* be NULL or can be prefix path, and then the `.prefix` variable will be set
@@ -363,7 +382,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs,
363382

364383
/**
365384
* Free data allocated in a "struct rev_info" after it's been
366-
* initialized with repo_init_revisions().
385+
* initialized with repo_init_revisions() or REV_INFO_INIT.
367386
*/
368387
void release_revisions(struct rev_info *revs);
369388

submodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ void show_submodule_diff_summary(struct diff_options *o, const char *path,
638638
struct object_id *one, struct object_id *two,
639639
unsigned dirty_submodule)
640640
{
641-
struct rev_info rev;
641+
struct rev_info rev = REV_INFO_INIT;
642642
struct commit *left = NULL, *right = NULL;
643643
struct commit_list *merge_bases = NULL;
644644
struct repository *sub;
@@ -665,6 +665,7 @@ void show_submodule_diff_summary(struct diff_options *o, const char *path,
665665

666666
out:
667667
free_commit_list(merge_bases);
668+
release_revisions(&rev);
668669
clear_commit_marks(left, ~0);
669670
clear_commit_marks(right, ~0);
670671
if (sub) {

0 commit comments

Comments
 (0)