Skip to content

Commit 50d829c

Browse files
committed
builtin/push.c: use strbuf instead of manual allocation
The command line arguments given to "git push" are massaged into a list of refspecs in set_refspecs() function. This was implemented using xmalloc, strcpy and friends, but it is much easier to read if done using strbuf. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent daad3aa commit 50d829c

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

builtin/push.c

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,22 @@ static void set_refspecs(const char **refs, int nr)
4141
for (i = 0; i < nr; i++) {
4242
const char *ref = refs[i];
4343
if (!strcmp("tag", ref)) {
44-
char *tag;
45-
int len;
44+
struct strbuf tagref = STRBUF_INIT;
4645
if (nr <= ++i)
4746
die(_("tag shorthand without <tag>"));
48-
len = strlen(refs[i]) + 11;
49-
if (deleterefs) {
50-
tag = xmalloc(len+1);
51-
strcpy(tag, ":refs/tags/");
52-
} else {
53-
tag = xmalloc(len);
54-
strcpy(tag, "refs/tags/");
55-
}
56-
strcat(tag, refs[i]);
57-
ref = tag;
58-
} else if (deleterefs && !strchr(ref, ':')) {
59-
char *delref;
60-
int len = strlen(ref)+1;
61-
delref = xmalloc(len+1);
62-
strcpy(delref, ":");
63-
strcat(delref, ref);
64-
ref = delref;
65-
} else if (deleterefs)
66-
die(_("--delete only accepts plain target ref names"));
47+
ref = refs[i];
48+
if (deleterefs)
49+
strbuf_addf(&tagref, ":refs/tags/%s", ref);
50+
else
51+
strbuf_addf(&tagref, "refs/tags/%s", ref);
52+
ref = strbuf_detach(&tagref, NULL);
53+
} else if (deleterefs) {
54+
struct strbuf delref = STRBUF_INIT;
55+
if (strchr(ref, ':'))
56+
die(_("--delete only accepts plain target ref names"));
57+
strbuf_addf(&delref, ":%s", ref);
58+
ref = strbuf_detach(&delref, NULL);
59+
}
6760
add_refspec(ref);
6861
}
6962
}

0 commit comments

Comments
 (0)