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

[PATCH 2/3] builtin-branch: use strbuf in fill_tracking_info()

From
Miklos Vajna <vmiklos@frugalware.org>
Date
Nov 17, 2008, 20:48 UTC
Message-ID
<5dee4e908190e7a4ed4a693fdf0fb3b3af88fc57.1226954771.git.vmiklos@frugalware.org>
In-Reply-To
<b3716161b2a3d508ec4306623c5c8889d9367e1d.1226954771.git.vmiklos@frugalware.org>

This is just about using the API, though in case of ~ 10^100 commits, this would fix the problem of writing to unallocated memory as well. ;-)

Signed-off-by: Miklos Vajna <vmiklos@frugalware.org>
---
 builtin-branch.c |   18 ++++++++----------
 1 files changed, 8 insertions(+), 10 deletions(-)
diff --git a/builtin-branch.c b/builtin-branch.c
index b9149b7..c8a8e2a 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -279,7 +279,7 @@ static int ref_cmp(const void *r1, const void *r2)
 	return strcmp(c1->name, c2->name);
 }
 
-static void fill_tracking_info(char *stat, const char *branch_name)
+static void fill_tracking_info(struct strbuf *stat, const char *branch_name)
 {
 	int ours, theirs;
 	struct branch *branch = branch_get(branch_name);
@@ -287,11 +287,11 @@ static void fill_tracking_info(char *stat, const char *branch_name)
 	if (!stat_tracking_info(branch, &ours, &theirs) || (!ours && !theirs))
 		return;
 	if (!ours)
-		sprintf(stat, "[behind %d] ", theirs);
+		strbuf_addf(stat, "[behind %d] ", theirs);
 	else if (!theirs)
-		sprintf(stat, "[ahead %d] ", ours);
+		strbuf_addf(stat, "[ahead %d] ", ours);
 	else
-		sprintf(stat, "[ahead %d, behind %d] ", ours, theirs);
+		strbuf_addf(stat, "[ahead %d, behind %d] ", ours, theirs);
 }
 
 static int matches_merge_filter(struct commit *commit)
@@ -334,11 +334,8 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 	}
 
 	if (verbose) {
-		struct strbuf subject = STRBUF_INIT;
+		struct strbuf subject = STRBUF_INIT, stat = STRBUF_INIT;
 		const char *sub = " **** invalid ref ****";
-		char stat[128];
-
-		stat[0] = '\0';
 
 		commit = item->commit;
 		if (commit && !parse_commit(commit)) {
@@ -348,13 +345,14 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 		}
 
 		if (item->kind == REF_LOCAL_BRANCH)
-			fill_tracking_info(stat, item->name);
+			fill_tracking_info(&stat, item->name);
 
 		printf("%c %s%-*s%s %s %s%s\n", c, branch_get_color(color),
 		       maxwidth, item->name,
 		       branch_get_color(COLOR_BRANCH_RESET),
 		       find_unique_abbrev(item->commit->object.sha1, abbrev),
-		       stat, sub);
+		       stat.buf, sub);
+		strbuf_release(&stat);
 		strbuf_release(&subject);
 	} else {
 		printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
-- 
1.6.0.2
Previous: Miklos VajnaNext: Miklos Vajna
Message 3 of 7 in “builtin-branch: use strbuf”
  1. 0/3 builtin-branch: use strbufMiklos Vajna, Nov 17, 2008
  2. 1/3 builtin-branch: use strbuf in delete_branches()Miklos Vajna, Nov 17, 2008
  3. 2/3 builtin-branch: use strbuf in fill_tracking_info()Miklos Vajna, Nov 17, 2008
  4. 3/3 builtin-branch: use strbuf in rename_branch()Miklos Vajna, Nov 17, 2008
  5. Junio C HamanoNov 18, 2008
  6. Johannes SchindelinNov 19, 2008
  7. Lars HjemliNov 19, 2008

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.