git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] builtin/push.c: use strbuf instead of manual allocation

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 4, 2013, 00:39 UTC
Message-ID
<1386117594-22062-2-git-send-email-gitster@pobox.com>
In-Reply-To
<1386117594-22062-1-git-send-email-gitster@pobox.com>

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>
---
 builtin/push.c | 35 ++++++++++++++---------------------
 1 file changed, 14 insertions(+), 21 deletions(-)
diff --git a/builtin/push.c b/builtin/push.c
index 7b1b66c..76e4400 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -41,29 +41,22 @@ static void set_refspecs(const char **refs, int nr)
 	for (i = 0; i < nr; i++) {
 		const char *ref = refs[i];
 		if (!strcmp("tag", ref)) {
-			char *tag;
-			int len;
+			struct strbuf tagref = STRBUF_INIT;
 			if (nr <= ++i)
 				die(_("tag shorthand without <tag>"));
-			len = strlen(refs[i]) + 11;
-			if (deleterefs) {
-				tag = xmalloc(len+1);
-				strcpy(tag, ":refs/tags/");
-			} else {
-				tag = xmalloc(len);
-				strcpy(tag, "refs/tags/");
-			}
-			strcat(tag, refs[i]);
-			ref = tag;
-		} else if (deleterefs && !strchr(ref, ':')) {
-			char *delref;
-			int len = strlen(ref)+1;
-			delref = xmalloc(len+1);
-			strcpy(delref, ":");
-			strcat(delref, ref);
-			ref = delref;
-		} else if (deleterefs)
-			die(_("--delete only accepts plain target ref names"));
+			ref = refs[i];
+			if (deleterefs)
+				strbuf_addf(&tagref, ":refs/tags/%s", ref);
+			else
+				strbuf_addf(&tagref, "refs/tags/%s", ref);
+			ref = strbuf_detach(&tagref, NULL);
+		} else if (deleterefs) {
+			struct strbuf delref = STRBUF_INIT;
+			if (strchr(ref, ':'))
+				die(_("--delete only accepts plain target ref names"));
+			strbuf_addf(&delref, ":%s", ref);
+			ref = strbuf_detach(&delref, NULL);
+		}
 		add_refspec(ref);
 	}
 }
-- 
1.8.5.1-400-gbc1da41
Previous: Junio C HamanoNext: Junio C Hamano
Message 2 of 8 in “Teaching "git push" to map pushed refs”
  1. 0/3 Teaching "git push" to map pushed refsJunio C Hamano, Dec 4, 2013
  2. 1/3 builtin/push.c: use strbuf instead of manual allocationJunio C Hamano, Dec 4, 2013
  3. 2/3 push: use remote.$name.push as a refmapJunio C Hamano, Dec 4, 2013
  4. 3/3 push: also use "upstream" mapping when pushing a single refJunio C Hamano, Dec 4, 2013
  5. 0/3 Teaching "git push" to map pushed refsJunio C Hamano, Dec 5, 2013
  6. 1/3 builtin/push.c: use strbuf instead of manual allocationJunio C Hamano, Dec 5, 2013
  7. 2/3 push: use remote.$name.push as a refmapJunio C Hamano, Dec 5, 2013
  8. 3/3 push: also use "upstream" mapping when pushing a single refJunio C Hamano, Dec 5, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.