Merge branch 'rs/refspec-leakfix'

Leakfix.

* rs/refspec-leakfix:
  refspec: add and use refspec_appendf()
  push: release strbufs used for refspec formatting
This commit is contained in:
Junio C Hamano 2020-09-18 17:58:00 -07:00
commit 21de7e9c50
6 changed files with 47 additions and 51 deletions

View File

@ -953,7 +953,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
struct ref *mapped_refs; struct ref *mapped_refs;
const struct ref *ref; const struct ref *ref;
struct strbuf key = STRBUF_INIT; struct strbuf key = STRBUF_INIT;
struct strbuf default_refspec = STRBUF_INIT;
struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT; struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
struct transport *transport = NULL; struct transport *transport = NULL;
const char *src_ref_prefix = "refs/heads/"; const char *src_ref_prefix = "refs/heads/";
@ -1157,9 +1156,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
remote = remote_get(option_origin); remote = remote_get(option_origin);
strbuf_addf(&default_refspec, "+%s*:%s*", src_ref_prefix, refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix,
branch_top.buf); branch_top.buf);
refspec_append(&remote->fetch, default_refspec.buf);
transport = transport_get(remote, remote->url[0]); transport = transport_get(remote, remote->url[0]);
transport_set_verbosity(transport, option_verbosity, option_progress); transport_set_verbosity(transport, option_verbosity, option_progress);
@ -1332,7 +1330,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
strbuf_release(&reflog_msg); strbuf_release(&reflog_msg);
strbuf_release(&branch_top); strbuf_release(&branch_top);
strbuf_release(&key); strbuf_release(&key);
strbuf_release(&default_refspec);
junk_mode = JUNK_LEAVE_ALL; junk_mode = JUNK_LEAVE_ALL;
strvec_clear(&ref_prefixes); strvec_clear(&ref_prefixes);

View File

@ -1738,15 +1738,12 @@ static int fetch_one(struct remote *remote, int argc, const char **argv,
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if (!strcmp(argv[i], "tag")) { if (!strcmp(argv[i], "tag")) {
char *tag;
i++; i++;
if (i >= argc) if (i >= argc)
die(_("You need to specify a tag name.")); die(_("You need to specify a tag name."));
tag = xstrfmt("refs/tags/%s:refs/tags/%s", refspec_appendf(&rs, "refs/tags/%s:refs/tags/%s",
argv[i], argv[i]); argv[i], argv[i]);
refspec_append(&rs, tag);
free(tag);
} else { } else {
refspec_append(&rs, argv[i]); refspec_append(&rs, argv[i]);
} }

View File

@ -61,26 +61,27 @@ static struct refspec rs = REFSPEC_INIT_PUSH;
static struct string_list push_options_config = STRING_LIST_INIT_DUP; static struct string_list push_options_config = STRING_LIST_INIT_DUP;
static const char *map_refspec(const char *ref, static void refspec_append_mapped(struct refspec *refspec, const char *ref,
struct remote *remote, struct ref *local_refs) struct remote *remote, struct ref *local_refs)
{ {
const char *branch_name; const char *branch_name;
struct ref *matched = NULL; struct ref *matched = NULL;
/* Does "ref" uniquely name our ref? */ /* Does "ref" uniquely name our ref? */
if (count_refspec_match(ref, local_refs, &matched) != 1) if (count_refspec_match(ref, local_refs, &matched) != 1) {
return ref; refspec_append(refspec, ref);
return;
}
if (remote->push.nr) { if (remote->push.nr) {
struct refspec_item query; struct refspec_item query;
memset(&query, 0, sizeof(struct refspec_item)); memset(&query, 0, sizeof(struct refspec_item));
query.src = matched->name; query.src = matched->name;
if (!query_refspecs(&remote->push, &query) && query.dst) { if (!query_refspecs(&remote->push, &query) && query.dst) {
struct strbuf buf = STRBUF_INIT; refspec_appendf(refspec, "%s%s:%s",
strbuf_addf(&buf, "%s%s:%s", query.force ? "+" : "",
query.force ? "+" : "", query.src, query.dst);
query.src, query.dst); return;
return strbuf_detach(&buf, NULL);
} }
} }
@ -88,14 +89,13 @@ static const char *map_refspec(const char *ref,
skip_prefix(matched->name, "refs/heads/", &branch_name)) { skip_prefix(matched->name, "refs/heads/", &branch_name)) {
struct branch *branch = branch_get(branch_name); struct branch *branch = branch_get(branch_name);
if (branch->merge_nr == 1 && branch->merge[0]->src) { if (branch->merge_nr == 1 && branch->merge[0]->src) {
struct strbuf buf = STRBUF_INIT; refspec_appendf(refspec, "%s:%s",
strbuf_addf(&buf, "%s:%s", ref, branch->merge[0]->src);
ref, branch->merge[0]->src); return;
return strbuf_detach(&buf, NULL);
} }
} }
return ref; refspec_append(refspec, ref);
} }
static void set_refspecs(const char **refs, int nr, const char *repo) static void set_refspecs(const char **refs, int nr, const char *repo)
@ -107,30 +107,26 @@ static void set_refspecs(const char **refs, int nr, const char *repo)
for (i = 0; i < nr; i++) { for (i = 0; i < nr; i++) {
const char *ref = refs[i]; const char *ref = refs[i];
if (!strcmp("tag", ref)) { if (!strcmp("tag", ref)) {
struct strbuf tagref = STRBUF_INIT;
if (nr <= ++i) if (nr <= ++i)
die(_("tag shorthand without <tag>")); die(_("tag shorthand without <tag>"));
ref = refs[i]; ref = refs[i];
if (deleterefs) if (deleterefs)
strbuf_addf(&tagref, ":refs/tags/%s", ref); refspec_appendf(&rs, ":refs/tags/%s", ref);
else else
strbuf_addf(&tagref, "refs/tags/%s", ref); refspec_appendf(&rs, "refs/tags/%s", ref);
ref = strbuf_detach(&tagref, NULL);
} else if (deleterefs) { } else if (deleterefs) {
struct strbuf delref = STRBUF_INIT;
if (strchr(ref, ':')) if (strchr(ref, ':'))
die(_("--delete only accepts plain target ref names")); die(_("--delete only accepts plain target ref names"));
strbuf_addf(&delref, ":%s", ref); refspec_appendf(&rs, ":%s", ref);
ref = strbuf_detach(&delref, NULL);
} else if (!strchr(ref, ':')) { } else if (!strchr(ref, ':')) {
if (!remote) { if (!remote) {
/* lazily grab remote and local_refs */ /* lazily grab remote and local_refs */
remote = remote_get(repo); remote = remote_get(repo);
local_refs = get_local_heads(); local_refs = get_local_heads();
} }
ref = map_refspec(ref, remote, local_refs); refspec_append_mapped(&rs, ref, remote, local_refs);
} } else
refspec_append(&rs, ref); refspec_append(&rs, ref);
} }
} }
@ -192,8 +188,6 @@ static const char message_detached_head_die[] =
static void setup_push_upstream(struct remote *remote, struct branch *branch, static void setup_push_upstream(struct remote *remote, struct branch *branch,
int triangular, int simple) int triangular, int simple)
{ {
struct strbuf refspec = STRBUF_INIT;
if (!branch) if (!branch)
die(_(message_detached_head_die), remote->name); die(_(message_detached_head_die), remote->name);
if (!branch->merge_nr || !branch->merge || !branch->remote_name) if (!branch->merge_nr || !branch->merge || !branch->remote_name)
@ -219,18 +213,14 @@ static void setup_push_upstream(struct remote *remote, struct branch *branch,
die_push_simple(branch, remote); die_push_simple(branch, remote);
} }
strbuf_addf(&refspec, "%s:%s", branch->refname, branch->merge[0]->src); refspec_appendf(&rs, "%s:%s", branch->refname, branch->merge[0]->src);
refspec_append(&rs, refspec.buf);
} }
static void setup_push_current(struct remote *remote, struct branch *branch) static void setup_push_current(struct remote *remote, struct branch *branch)
{ {
struct strbuf refspec = STRBUF_INIT;
if (!branch) if (!branch)
die(_(message_detached_head_die), remote->name); die(_(message_detached_head_die), remote->name);
strbuf_addf(&refspec, "%s:%s", branch->refname, branch->refname); refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname);
refspec_append(&rs, refspec.buf);
} }
static int is_workflow_triangular(struct remote *remote) static int is_workflow_triangular(struct remote *remote)

View File

@ -153,7 +153,7 @@ void refspec_init(struct refspec *rs, int fetch)
rs->fetch = fetch; rs->fetch = fetch;
} }
void refspec_append(struct refspec *rs, const char *refspec) static void refspec_append_nodup(struct refspec *rs, char *refspec)
{ {
struct refspec_item item; struct refspec_item item;
@ -163,7 +163,21 @@ void refspec_append(struct refspec *rs, const char *refspec)
rs->items[rs->nr++] = item; rs->items[rs->nr++] = item;
ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc); ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
rs->raw[rs->raw_nr++] = xstrdup(refspec); rs->raw[rs->raw_nr++] = refspec;
}
void refspec_append(struct refspec *rs, const char *refspec)
{
refspec_append_nodup(rs, xstrdup(refspec));
}
void refspec_appendf(struct refspec *rs, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
refspec_append_nodup(rs, xstrvfmt(fmt, ap));
va_end(ap);
} }
void refspec_appendn(struct refspec *rs, const char **refspecs, int nr) void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)

View File

@ -56,6 +56,8 @@ void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
void refspec_item_clear(struct refspec_item *item); void refspec_item_clear(struct refspec_item *item);
void refspec_init(struct refspec *rs, int fetch); void refspec_init(struct refspec *rs, int fetch);
void refspec_append(struct refspec *rs, const char *refspec); void refspec_append(struct refspec *rs, const char *refspec);
__attribute__((format (printf,2,3)))
void refspec_appendf(struct refspec *rs, const char *fmt, ...);
void refspec_appendn(struct refspec *rs, const char **refspecs, int nr); void refspec_appendn(struct refspec *rs, const char **refspecs, int nr);
void refspec_clear(struct refspec *rs); void refspec_clear(struct refspec *rs);

View File

@ -287,19 +287,15 @@ static void read_branches_file(struct remote *remote)
frag = (char *)git_default_branch_name(); frag = (char *)git_default_branch_name();
add_url_alias(remote, strbuf_detach(&buf, NULL)); add_url_alias(remote, strbuf_detach(&buf, NULL));
strbuf_addf(&buf, "refs/heads/%s:refs/heads/%s", refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
frag, remote->name); frag, remote->name);
refspec_append(&remote->fetch, buf.buf);
/* /*
* Cogito compatible push: push current HEAD to remote #branch * Cogito compatible push: push current HEAD to remote #branch
* (master if missing) * (master if missing)
*/ */
strbuf_reset(&buf); refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag);
strbuf_addf(&buf, "HEAD:refs/heads/%s", frag);
refspec_append(&remote->push, buf.buf);
remote->fetch_tags = 1; /* always auto-follow */ remote->fetch_tags = 1; /* always auto-follow */
strbuf_release(&buf);
} }
static int handle_config(const char *key, const char *value, void *cb) static int handle_config(const char *key, const char *value, void *cb)