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

[PATCH 5/5] branch: show upstream branch when double verbose

From
Jeff King <peff@peff.net>
Date
Apr 7, 2009, 07:16 UTC
Message-ID
<20090407071656.GE2924@coredump.intra.peff.net>
In-Reply-To
<20090407070254.GA2870@coredump.intra.peff.net>

This information is easily accessible when we are calculating the relationship. The only reason not to print it all the time is that it consumes a fair bit of screen space, and may not be of interest to the user.

Signed-off-by: Jeff King <peff@peff.net>
---
This one is very RFC. Should this information be part of the regular
"-v"? Should it be part of "git branch" with regular verbosity?
Should the format be different? I wonder if
  master 1234abcd [origin/master: ahead 5, behind 6] whatever

will be interpreted as "origin/master is ahead 5, behind 6" when it is really the reverse. Maybe "[ahead 5, behind 6 from origin/master]" would be better?

 Documentation/git-branch.txt |    4 +++-
 builtin-branch.c             |   23 +++++++++++++++++------
 2 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 31ba7f2..ba3dea6 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -100,7 +100,9 @@ OPTIONS
 
 -v::
 --verbose::
-	Show sha1 and commit subject line for each head.
+	Show sha1 and commit subject line for each head, along with
+	relationship to upstream branch (if any). If given twice, print
+	the name of the upstream branch, as well.
 
 --abbrev=<length>::
 	Alter the sha1's minimum display length in the output listing.
diff --git a/builtin-branch.c b/builtin-branch.c
index ca81d72..3275821 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -301,19 +301,30 @@ static int ref_cmp(const void *r1, const void *r2)
 	return strcmp(c1->name, c2->name);
 }
 
-static void fill_tracking_info(struct strbuf *stat, const char *branch_name)
+static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
+		int show_upstream_ref)
 {
 	int ours, theirs;
 	struct branch *branch = branch_get(branch_name);
 
-	if (!stat_tracking_info(branch, &ours, &theirs) || (!ours && !theirs))
+	if (!stat_tracking_info(branch, &ours, &theirs)) {
+		if (branch && branch->merge && branch->merge[0]->dst &&
+		    show_upstream_ref)
+			strbuf_addf(stat, "[%s] ",
+			    shorten_unambiguous_ref(branch->merge[0]->dst));
 		return;
+	}
+
+	strbuf_addch(stat, '[');
+	if (show_upstream_ref)
+		strbuf_addf(stat, "%s: ",
+			shorten_unambiguous_ref(branch->merge[0]->dst));
 	if (!ours)
-		strbuf_addf(stat, "[behind %d] ", theirs);
+		strbuf_addf(stat, "behind %d] ", theirs);
 	else if (!theirs)
-		strbuf_addf(stat, "[ahead %d] ", ours);
+		strbuf_addf(stat, "ahead %d] ", ours);
 	else
-		strbuf_addf(stat, "[ahead %d, behind %d] ", ours, theirs);
+		strbuf_addf(stat, "ahead %d, behind %d] ", ours, theirs);
 }
 
 static int matches_merge_filter(struct commit *commit)
@@ -379,7 +390,7 @@ 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, verbose > 1);
 
 		strbuf_addf(&out, " %s %s%s",
 			find_unique_abbrev(item->commit->object.sha1, abbrev),
-- 
1.6.2.2.450.gd6aa9.dirty
Previous: Michael J GruberNext: Michael J Gruber
Message 13 of 24 in “making upstream branch information accessible”
  1. 0/5 making upstream branch information accessibleJeff King, Apr 7, 2009
  2. 1/5 for-each-ref: refactor get_short_ref functionJeff King, Apr 7, 2009
  3. 2/5 for-each-ref: refactor refname handlingJeff King, Apr 7, 2009
  4. Junio C HamanoApr 8, 2009
  5. Jeff KingApr 8, 2009
  6. 3/5 for-each-ref: add "upstream" format fieldJeff King, Apr 7, 2009
  7. 4/5 make get_short_ref a public functionJeff King, Apr 7, 2009
  8. Bert WesargApr 7, 2009
  9. Jeff KingApr 9, 2009
  10. Bert WesargApr 9, 2009
  11. Jeff KingApr 13, 2009
  12. Michael J GruberApr 7, 2009
  13. 5/5 branch: show upstream branch when double verboseJeff King, Apr 7, 2009
  14. Michael J GruberApr 7, 2009
  15. Jeff KingApr 9, 2009
  16. Santi BéjarApr 9, 2009
  17. Jeff KingApr 13, 2009
  18. Wincent ColaiutaApr 13, 2009
  19. Paolo CiarrocchiApr 7, 2009
  20. for-each-ref: remove multiple xstrdup() in get_short_ref()Bert Wesarg, Apr 7, 2009
  21. Jeff KingApr 7, 2009
  22. Bert WesargApr 7, 2009
  23. Jeff KingApr 7, 2009
  24. Bert WesargApr 7, 2009

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.