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>

Junio C Hamano committed Dec 3, 2013 at 14:33 UTC 50d829c11a3c82a8b23f2e165ab6944dfd9bbb36
1 file changed +14 -21
builtin/push.c
+14 -21
@@ -41,29 +41,22 @@ static void set_refspecs(const char **refs, int nr)
41 for (i = 0; i < nr; i++) {
42 const char *ref = refs[i];
43 if (!strcmp("tag", ref)) {
44 - char *tag;
45 - int len;
44 + struct strbuf tagref = STRBUF_INIT;
45 if (nr <= ++i)
46 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 + }
60 add_refspec(ref);
61 }
62 }