Merge branch 'ab/ref-filter-leakfix' into jc/fix-ref-sorting-parse

* ab/ref-filter-leakfix:
  branch: use ref_sorting_release()
  ref-filter API user: add and use a ref_sorting_release()
  tag: use a "goto cleanup" pattern, leak less memory
This commit is contained in:
Junio C Hamano 2021-10-20 12:23:35 -07:00
commit 1a89796e4a
5 changed files with 34 additions and 16 deletions

View File

@ -407,7 +407,8 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
return strbuf_detach(&fmt, NULL);
}
static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, struct ref_format *format)
static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting,
struct ref_format *format, struct string_list *output)
{
int i;
struct ref_array array;
@ -449,7 +450,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
if (column_active(colopts)) {
assert(!filter->verbose && "--column and --verbose are incompatible");
/* format to a string_list to let print_columns() do its job */
string_list_append(&output, out.buf);
string_list_append(output, out.buf);
} else {
fwrite(out.buf, 1, out.len, stdout);
putchar('\n');
@ -753,9 +754,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
ref_sorting_set_sort_flags_all(
sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1);
print_ref_list(&filter, sorting, &format);
print_ref_list(&filter, sorting, &format, &output);
print_columns(&output, colopts, NULL);
string_list_clear(&output, 0);
ref_sorting_release(sorting);
return 0;
} else if (edit_description) {
const char *branch_name;

View File

@ -96,6 +96,6 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
ref_array_clear(&array);
free_commit_list(filter.with_commit);
free_commit_list(filter.no_commit);
UNLEAK(sorting);
ref_sorting_release(sorting);
return 0;
}

View File

@ -432,7 +432,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
int annotate = 0, force = 0;
int cmdmode = 0, create_tag_object = 0;
const char *msgfile = NULL, *keyid = NULL;
struct msg_arg msg = { 0, STRBUF_INIT };
struct msg_arg msg = { .buf = STRBUF_INIT };
struct ref_transaction *transaction;
struct strbuf err = STRBUF_INIT;
struct ref_filter filter;
@ -482,6 +482,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
OPT_END()
};
int ret = 0;
setup_ref_filter_porcelain_msg();
@ -529,7 +530,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
filter.ignore_case = icase;
if (cmdmode == 'l') {
int ret;
if (column_active(colopts)) {
struct column_options copts;
memset(&copts, 0, sizeof(copts));
@ -540,7 +540,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
ret = list_tags(&filter, sorting, &format);
if (column_active(colopts))
stop_column_filter();
return ret;
goto cleanup;
}
if (filter.lines != -1)
die(_("-n option is only allowed in list mode"));
@ -552,12 +552,15 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
die(_("--points-at option is only allowed in list mode"));
if (filter.reachable_from || filter.unreachable_from)
die(_("--merged and --no-merged options are only allowed in list mode"));
if (cmdmode == 'd')
return delete_tags(argv);
if (cmdmode == 'd') {
ret = delete_tags(argv);
goto cleanup;
}
if (cmdmode == 'v') {
if (format.format && verify_ref_format(&format))
usage_with_options(git_tag_usage, options);
return for_each_tag_name(argv, verify_tag, &format);
ret = for_each_tag_name(argv, verify_tag, &format);
goto cleanup;
}
if (msg.given || msgfile) {
@ -626,10 +629,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
printf(_("Updated tag '%s' (was %s)\n"), tag,
find_unique_abbrev(&prev, DEFAULT_ABBREV));
UNLEAK(buf);
UNLEAK(ref);
UNLEAK(reflog_msg);
UNLEAK(msg);
UNLEAK(err);
return 0;
cleanup:
ref_sorting_release(sorting);
strbuf_release(&buf);
strbuf_release(&ref);
strbuf_release(&reflog_msg);
strbuf_release(&msg.buf);
strbuf_release(&err);
return ret;
}

View File

@ -2705,6 +2705,15 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
return 0;
}
void ref_sorting_release(struct ref_sorting *sorting)
{
while (sorting) {
struct ref_sorting *next = sorting->next;
free(sorting);
sorting = next;
}
}
int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
{
struct ref_filter *rf = opt->value;

View File

@ -127,6 +127,8 @@ void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *atom);
int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
/* Default sort option based on refname */
struct ref_sorting *ref_default_sorting(void);
/* Release a "struct ref_sorting" */
void ref_sorting_release(struct ref_sorting *);
/* Function to parse --merged and --no-merged options */
int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
/* Get the current HEAD's description */