ref-filter: add `ref_filter_clear()`

We did not bother to clean up at all in `git branch` or `git tag`, and `git for-each-ref` only cleans up a couple of members. Add and call `ref_filter_clear()` when cleaning up a `struct ref_filter`. Running this patch (without any test changes) indicates a couple of now leak-free tests. This was found by running: $ make SANITIZE=leak $ make -C t GIT_TEST_PASSING_SANITIZE_LEAK=check GIT_TEST_OPTS=--immediate (Note that the `reachable_from` and `unreachable_from` lists should be cleaned as they are used. So this is just covering any case where we might bail before running the reachability check.) Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 10, 2023 at 17:12 UTC b571fb98008b485bfc6f7d6538b79a7e92d731f4
7 files changed +24 -2
builtin/branch.c
+1
@@ -858,6 +858,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
858 print_columns(&output, colopts, NULL);
859 string_list_clear(&output, 0);
860 ref_sorting_release(sorting);
861 + ref_filter_clear(&filter);
862 return 0;
863 } else if (edit_description) {
864 const char *branch_name;
builtin/for-each-ref.c
+1 -2
@@ -120,8 +120,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
120 strbuf_release(&err);
121 strbuf_release(&output);
122 ref_array_clear(&array);
123 - free_commit_list(filter.with_commit);
124 - free_commit_list(filter.no_commit);
123 + ref_filter_clear(&filter);
124 ref_sorting_release(sorting);
125 strvec_clear(&vec);
126 return 0;
builtin/tag.c
+1
@@ -645,6 +645,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
645
646 cleanup:
647 ref_sorting_release(sorting);
648 + ref_filter_clear(&filter);
649 strbuf_release(&buf);
650 strbuf_release(&ref);
651 strbuf_release(&reflog_msg);
ref-filter.c
+16
@@ -2866,3 +2866,19 @@ int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
2866
2867 return 0;
2868 }
2869 +
2870 +void ref_filter_init(struct ref_filter *filter)
2871 +{
2872 + struct ref_filter blank = REF_FILTER_INIT;
2873 + memcpy(filter, &blank, sizeof(blank));
2874 +}
2875 +
2876 +void ref_filter_clear(struct ref_filter *filter)
2877 +{
2878 + oid_array_clear(&filter->points_at);
2879 + free_commit_list(filter->with_commit);
2880 + free_commit_list(filter->no_commit);
2881 + free_commit_list(filter->reachable_from);
2882 + free_commit_list(filter->unreachable_from);
2883 + ref_filter_init(filter);
2884 +}
ref-filter.h
+3
@@ -170,4 +170,7 @@ void filter_ahead_behind(struct repository *r,
170 struct ref_format *format,
171 struct ref_array *array);
172
173 +void ref_filter_init(struct ref_filter *filter);
174 +void ref_filter_clear(struct ref_filter *filter);
175 +
176 #endif /* REF_FILTER_H */
t/t0041-usage.sh
+1
@@ -5,6 +5,7 @@ test_description='Test commands behavior when given invalid argument value'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 +TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
11 test_expect_success 'setup ' '
t/t3402-rebase-merge.sh
+1
@@ -8,6 +8,7 @@ test_description='git rebase --merge test'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11 +TEST_PASSES_SANITIZE_LEAK=true
12 . ./test-lib.sh
13
14 T="A quick brown fox