
sorting might be a list allocated in ref_default_sorting() (in this case it's a fixed single item list, which has nevertheless been xcalloc'd), or it might be a list allocated in parse_opt_ref_sorting(). In either case we could free these lists - but instead we UNLEAK as we're at the end of cmd_for_each_ref. (There's no existing implementation of clear_ref_sorting(), and writing a loop to free the list seems more trouble than it's worth.) filter.with_commit/no_commit are populated via OPT_CONTAINS/OPT_NO_CONTAINS, both of which create new entries via parse_opt_commits(), and also need to be free'd or UNLEAK'd. Because free_commit_list() already exists, we choose to use that over an UNLEAK. LSAN output from t0041: Direct leak of 16 byte(s) in 1 object(s) allocated from: #0 0x49a9d2 in calloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3 #1 0x9ac252 in xcalloc wrapper.c:140:8 #2 0x8a4a55 in ref_default_sorting ref-filter.c:2486:32 #3 0x56c6b1 in cmd_for_each_ref builtin/for-each-ref.c:72:13 #4 0x4cd91d in run_builtin git.c:467:11 #5 0x4cb5f3 in handle_builtin git.c:719:3 #6 0x4ccf47 in run_argv git.c:808:4 #7 0x4caf49 in cmd_main git.c:939:19 #8 0x69dabe in main common-main.c:52:11 #9 0x7f2bdc570349 in __libc_start_main (/lib64/libc.so.6+0x24349) Direct leak of 16 byte(s) in 1 object(s) allocated from: #0 0x49a85d in malloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:145:3 #1 0x9abf54 in do_xmalloc wrapper.c:41:8 #2 0x9abf2a in xmalloc wrapper.c:62:9 #3 0x717486 in commit_list_insert commit.c:540:33 #4 0x8644cf in parse_opt_commits parse-options-cb.c:98:2 #5 0x869bb5 in get_value parse-options.c:181:11 #6 0x8677dc in parse_long_opt parse-options.c:378:10 #7 0x8659bd in parse_options_step parse-options.c:817:11 #8 0x867fcd in parse_options parse-options.c:870:10 #9 0x56c62b in cmd_for_each_ref builtin/for-each-ref.c:59:2 #10 0x4cd91d in run_builtin git.c:467:11 #11 0x4cb5f3 in handle_builtin git.c:719:3 #12 0x4ccf47 in run_argv git.c:808:4 #13 0x4caf49 in cmd_main git.c:939:19 #14 0x69dabe in main common-main.c:52:11 #15 0x7f2bdc570349 in __libc_start_main (/lib64/libc.so.6+0x24349) Signed-off-by: Andrzej Hunt <ajrhunt@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
91 lines
3.1 KiB
C
91 lines
3.1 KiB
C
#include "builtin.h"
|
|
#include "cache.h"
|
|
#include "config.h"
|
|
#include "refs.h"
|
|
#include "object.h"
|
|
#include "parse-options.h"
|
|
#include "ref-filter.h"
|
|
|
|
static char const * const for_each_ref_usage[] = {
|
|
N_("git for-each-ref [<options>] [<pattern>]"),
|
|
N_("git for-each-ref [--points-at <object>]"),
|
|
N_("git for-each-ref [--merged [<commit>]] [--no-merged [<commit>]]"),
|
|
N_("git for-each-ref [--contains [<commit>]] [--no-contains [<commit>]]"),
|
|
NULL
|
|
};
|
|
|
|
int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
|
|
{
|
|
int i;
|
|
struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
|
|
int maxcount = 0, icase = 0;
|
|
struct ref_array array;
|
|
struct ref_filter filter;
|
|
struct ref_format format = REF_FORMAT_INIT;
|
|
|
|
struct option opts[] = {
|
|
OPT_BIT('s', "shell", &format.quote_style,
|
|
N_("quote placeholders suitably for shells"), QUOTE_SHELL),
|
|
OPT_BIT('p', "perl", &format.quote_style,
|
|
N_("quote placeholders suitably for perl"), QUOTE_PERL),
|
|
OPT_BIT(0 , "python", &format.quote_style,
|
|
N_("quote placeholders suitably for python"), QUOTE_PYTHON),
|
|
OPT_BIT(0 , "tcl", &format.quote_style,
|
|
N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
|
|
|
|
OPT_GROUP(""),
|
|
OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
|
|
OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
|
|
OPT__COLOR(&format.use_color, N_("respect format colors")),
|
|
OPT_REF_SORT(sorting_tail),
|
|
OPT_CALLBACK(0, "points-at", &filter.points_at,
|
|
N_("object"), N_("print only refs which points at the given object"),
|
|
parse_opt_object_name),
|
|
OPT_MERGED(&filter, N_("print only refs that are merged")),
|
|
OPT_NO_MERGED(&filter, N_("print only refs that are not merged")),
|
|
OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")),
|
|
OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")),
|
|
OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
|
|
OPT_END(),
|
|
};
|
|
|
|
memset(&array, 0, sizeof(array));
|
|
memset(&filter, 0, sizeof(filter));
|
|
|
|
format.format = "%(objectname) %(objecttype)\t%(refname)";
|
|
|
|
git_config(git_default_config, NULL);
|
|
|
|
parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
|
|
if (maxcount < 0) {
|
|
error("invalid --count argument: `%d'", maxcount);
|
|
usage_with_options(for_each_ref_usage, opts);
|
|
}
|
|
if (HAS_MULTI_BITS(format.quote_style)) {
|
|
error("more than one quoting style?");
|
|
usage_with_options(for_each_ref_usage, opts);
|
|
}
|
|
if (verify_ref_format(&format))
|
|
usage_with_options(for_each_ref_usage, opts);
|
|
|
|
if (!sorting)
|
|
sorting = ref_default_sorting();
|
|
ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
|
|
filter.ignore_case = icase;
|
|
|
|
filter.name_patterns = argv;
|
|
filter.match_as_path = 1;
|
|
filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
|
|
ref_array_sort(sorting, &array);
|
|
|
|
if (!maxcount || array.nr < maxcount)
|
|
maxcount = array.nr;
|
|
for (i = 0; i < maxcount; i++)
|
|
show_ref_array_item(array.items[i], &format);
|
|
ref_array_clear(&array);
|
|
free_commit_list(filter.with_commit);
|
|
free_commit_list(filter.no_commit);
|
|
UNLEAK(sorting);
|
|
return 0;
|
|
}
|