]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ref-filter: add `ref_filter_clear()`
authorJeff King <peff@peff.net>
Mon, 10 Jul 2023 21:12:13 +0000 (17:12 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 10 Jul 2023 21:48:55 +0000 (14:48 -0700)
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>
builtin/branch.c
builtin/for-each-ref.c
builtin/tag.c
ref-filter.c
ref-filter.h
t/t0041-usage.sh
t/t3402-rebase-merge.sh

index 7891dec3615eacdad63fd5de5ba2bcfa3db841cd..07ee874617112965cc2004dd99d5d4ce185cc095 100644 (file)
@@ -858,6 +858,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                print_columns(&output, colopts, NULL);
                string_list_clear(&output, 0);
                ref_sorting_release(sorting);
+               ref_filter_clear(&filter);
                return 0;
        } else if (edit_description) {
                const char *branch_name;
index 99ccb7351853515820ebe429d93c6031760c4306..c01fa6fefe010e351cdda9f5010539e650a2bb4a 100644 (file)
@@ -120,8 +120,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
        strbuf_release(&err);
        strbuf_release(&output);
        ref_array_clear(&array);
-       free_commit_list(filter.with_commit);
-       free_commit_list(filter.no_commit);
+       ref_filter_clear(&filter);
        ref_sorting_release(sorting);
        strvec_clear(&vec);
        return 0;
index 6b41bb73749c4ce1721137d9d9569d28aacd1ba6..aab5e693fea663b9b0cc3ee94e8b26918691adc4 100644 (file)
@@ -645,6 +645,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 
 cleanup:
        ref_sorting_release(sorting);
+       ref_filter_clear(&filter);
        strbuf_release(&buf);
        strbuf_release(&ref);
        strbuf_release(&reflog_msg);
index 048d277cbf798ea8765e38f89b57a697a9936e61..d32f426898adb50c36d0e88621de4ac98cbb6034 100644 (file)
@@ -2866,3 +2866,19 @@ int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
 
        return 0;
 }
+
+void ref_filter_init(struct ref_filter *filter)
+{
+       struct ref_filter blank = REF_FILTER_INIT;
+       memcpy(filter, &blank, sizeof(blank));
+}
+
+void ref_filter_clear(struct ref_filter *filter)
+{
+       oid_array_clear(&filter->points_at);
+       free_commit_list(filter->with_commit);
+       free_commit_list(filter->no_commit);
+       free_commit_list(filter->reachable_from);
+       free_commit_list(filter->unreachable_from);
+       ref_filter_init(filter);
+}
index a920f73b2974b8cbd21bfc0bf7eba954f6ab4ab7..160b8072245ae4043ba5ffd625c9989717f55bd5 100644 (file)
@@ -170,4 +170,7 @@ void filter_ahead_behind(struct repository *r,
                         struct ref_format *format,
                         struct ref_array *array);
 
+void ref_filter_init(struct ref_filter *filter);
+void ref_filter_clear(struct ref_filter *filter);
+
 #endif /*  REF_FILTER_H  */
index c4fc34eb18eb1401c5331667408c496e663c9c69..9ea974b0c6c68a247ddd3f1b7cae806a4c79fbc4 100755 (executable)
@@ -5,6 +5,7 @@ test_description='Test commands behavior when given invalid argument value'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup ' '
index 79b0640c004e23f5346d9bfe1ba9998791633887..e9e03ca4b5eb16e6c40815a25197e3e22bb37561 100755 (executable)
@@ -8,6 +8,7 @@ test_description='git rebase --merge test'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 T="A quick brown fox