builtin/pack-refs: release allocated memory

Some of the command line options in `cmd_pack_refs()` require us to allocate memory. This memory is never released and thus leaking, but we paper over this leak by declaring the respective variables as `static` function-level variables, which is somewhat awkward. Refactor the code to release the allocated memory and drop the `static` declaration. While at it, remove the useless `flags` variable. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 25, 2024 at 11:03 UTC a75dc71f3772cee10dfb93861610d8ee413ad207
1 file changed +15 -8
builtin/pack-refs.c
+15 -8
@@ -13,15 +13,17 @@ static char const * const pack_refs_usage[] = {
13
14 int cmd_pack_refs(int argc, const char **argv, const char *prefix)
15 {
16 - unsigned int flags = PACK_REFS_PRUNE;
17 - static struct ref_exclusions excludes = REF_EXCLUSIONS_INIT;
18 - static struct string_list included_refs = STRING_LIST_INIT_NODUP;
19 - struct pack_refs_opts pack_refs_opts = { .exclusions = &excludes,
20 - .includes = &included_refs,
21 - .flags = flags };
22 - static struct string_list option_excluded_refs = STRING_LIST_INIT_NODUP;
16 + struct ref_exclusions excludes = REF_EXCLUSIONS_INIT;
17 + struct string_list included_refs = STRING_LIST_INIT_NODUP;
18 + struct pack_refs_opts pack_refs_opts = {
19 + .exclusions = &excludes,
20 + .includes = &included_refs,
21 + .flags = PACK_REFS_PRUNE,
22 + };
23 + struct string_list option_excluded_refs = STRING_LIST_INIT_NODUP;
24 struct string_list_item *item;
25 int pack_all = 0;
26 + int ret;
27
28 struct option opts[] = {
29 OPT_BOOL(0, "all", &pack_all, N_("pack everything")),
@@ -45,5 +47,10 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
47 if (!pack_refs_opts.includes->nr)
48 string_list_append(pack_refs_opts.includes, "refs/tags/*");
49
48 - return refs_pack_refs(get_main_ref_store(the_repository), &pack_refs_opts);
50 + ret = refs_pack_refs(get_main_ref_store(the_repository), &pack_refs_opts);
51 +
52 + clear_ref_exclusions(&excludes);
53 + string_list_clear(&included_refs, 0);
54 + string_list_clear(&option_excluded_refs, 0);
55 + return ret;
56 }