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

Re: [PATCH] Show branch information in short output of git status

From
Knittl <knittl89@googlemail.com>
Date
May 6, 2010, 12:24 UTC
Message-ID
<AANLkTikDkrNrzPmIhmcBRKtLKV70f4Kp8wTw6I6ctB4O@mail.gmail.com>
In-Reply-To
<20100505050640.GC8779@coredump.intra.peff.net>
Hi jeff,
thank you for your response and your comments.
On Wed, May 5, 2010 at 7:06 AM, Jeff King <peff@peff.net> wrote:
Show 12 quoted lines
> On Sun, May 02, 2010 at 11:13:41AM +0200, Knittl wrote:
>
>> This patch adds a first line in the output of `git status -s`, showing
>> which branch the user is currently on, and in case of tracking branches
>> the number of commits on each branch.
>
> I don't generally use "git status -s", so I don't have a strong opinion,
> but should this perhaps be optional?  I imagine people using it fall
> into one of two categories:
>
>  1. They want to waste less vertical screen real estate than "git
>     status" does.

it's one line ;) but i can understand your concern. would `git status -s -v` or `git status -s --show-branch` make sense?

Show 12 quoted lines
>  2. They prefer to see files in a single list with status marked,
>     rather than in separate lists based on status.
>
> Your patch will probably annoy people in group (1) unless they have some
> way to turn it off.
>
> A few other comments on the patch itself:
>
>> +     color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
>
> I thought the doubled '##' looked funny at first, but the more I look at
> it, I think lining it up with the filenames looks good.

i had one hash and two spaces at first, but as you said, two hashes look more lined up.

Show 6 quoted lines
>> +     if(!s->branch) return;
>
> Style nits. This should be:
>
>  if (!s->branch)
>          return;
done …
Show 15 quoted lines
>> +     branch = branch_get(s->branch + 11);
>> +     if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
>> +
>> +             if(s->is_initial) {
>> +                     color_fprintf(s->fp,
>> +                             color(WT_STATUS_HEADER, s),
>> +                             "Initial commit ... ");
>> +             }
>> +             color_fprintf_ln(s->fp, branch_color,
>> +                     "%s", branch_name);
>> +             return;
>> +     }
>
> Why do we only mention it as an initial commit if there is no tracking?
> We get if it is an initial commit and no tracking:

i don't think it's (easily, without messing around with the .git directory) possible to create a branch without commits that tracks another branch (without commits). or is it?

Show 5 quoted lines
>  ## Initial commit ... master
>
> but if there is a tracking branch setup, we get no "Initial commit" at
> all. And the use of "..." is confusing, as it usually implies symmetric
> difference, which doesn't really make sense here.

i used ... to make it look consistent with normal branch output, but i guess you are right. i changed it to `Initial commit on <branchname>

Show 22 quoted lines
>> +     base = branch->merge[0]->dst;
>> +     base = shorten_unambiguous_ref(base, 0);
>> +     color_fprintf_ln(s->fp, branch_color,
>> +                     "%s (%d) ... %s (%d)",
>> +                     branch_name, num_ours,
>> +                     base, num_theirs);
>> +}
>
> Here we get:
>
>  ## master (1) ... origin/master (1)
>
> which kind of makes sense. The "..." implies symmetric difference. But
> in other spots, like "git fetch" and "git push" output, we usually try
> to make it cut-and-pastable in case one wants to view the actual
> history. So maybe something like:
>
>  ## master...origin/master [ahead 1, behind 1]
>
> The latter matches how "git branch -v" prints it. I dunno. I guess this
> is just bikeshedding really, so maybe others will chime in with
> suggestions and you can pick the one you like best.

oh, i didn't know `git branch -v`. the cut&paste argument is really strong, so i totally agree with you and changed my code.

branch names and ahead/behind numbers appear now in green and red to match the output of `git branch -l`

here is the updated patch:
 - initial commit is also printed when there is tracking information
(i still haven't managed to create a situation like that. `git branch
oldmaster; rm .git/refs/heads/master; git branch master --set-upstream
oldmaster` will reset branch master to oldmaster (a bug?))
 - colors to match output of `git branch` (green: current, red: remote)
 - output format is copy-pasteable, ahead/behind information is in the
same format as in `git branch -v`
 - branch information is still printed by default, i have to look into
commandline option parsing first. i was thinking of `git status -v -b`
(as in `git checkout -b` to mean branch)
cheers, daniel
(i forgot to send my reply to the mailing list too …)
---------8<----------------
From 82b4481d38ae0cd62030aeea67160656b7c763e2 Mon Sep 17 00:00:00 2001
From: Daniel Knittl-Frank <knittl89+git@googlemail.com>
Date: Tue, 20 Apr 2010 22:40:54 +0200
Subject: [PATCH] Show branch information in short output of git status

This patch adds a first line in the output of `git status -s`, showing which branch the user is currently on, and in case of tracking branches the number of commits on each branch.

Signed-off-by: Daniel Knittl-Frank <knittl89+git@googlemail.com>
---
 wt-status.c |   63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 wt-status.h |    4 ++-
 2 files changed, 66 insertions(+), 1 deletions(-)
diff --git a/wt-status.c b/wt-status.c
index 8ca59a2..f7f1269 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -9,6 +9,7 @@
 #include "quote.h"
 #include "run-command.h"
 #include "remote.h"
+#include "refs.h"

 static char default_wt_status_colors[][COLOR_MAXLEN] = {
 	GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
@@ -17,6 +18,8 @@ static char default_wt_status_colors[][COLOR_MAXLEN] = {
 	GIT_COLOR_RED,    /* WT_STATUS_UNTRACKED */
 	GIT_COLOR_RED,    /* WT_STATUS_NOBRANCH */
 	GIT_COLOR_RED,    /* WT_STATUS_UNMERGED */
+	GIT_COLOR_GREEN,  /* WT_STATUS_LOCAL_BRANCH */
+	GIT_COLOR_RED,    /* WT_STATUS_REMOTE_BRANCH */
 };

 static const char *color(int slot, struct wt_status *s)
@@ -721,9 +724,69 @@ static void wt_shortstatus_untracked(int
null_termination, struct string_list_it
 	}
 }

+static void wt_shortstatus_print_tracking(struct wt_status *s)
+{
+	struct branch *branch;
+	const char *header_color = color(WT_STATUS_HEADER, s);
+	const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
+	const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
+
+	const char *base;
+	const char *branch_name;
+	int num_ours, num_theirs;
+
+	color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
+
+	if(!s->branch)
+		return;
+	branch_name = s->branch;
+
+	if (!prefixcmp(branch_name, "refs/heads/"))
+		branch_name += 11;
+	else if (!strcmp(branch_name, "HEAD")) {
+		branch_name = "HEAD (no branch)";
+		branch_color_local = color(WT_STATUS_NOBRANCH, s);
+	}
+
+	branch = branch_get(s->branch + 11);
+	if(s->is_initial) {
+		color_fprintf(s->fp, header_color, "Initial commit on ");
+	}
+	if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
+		color_fprintf_ln(s->fp, branch_color_local,
+			"%s", branch_name);
+		return;
+	}
+
+	base = branch->merge[0]->dst;
+	base = shorten_unambiguous_ref(base, 0);
+	color_fprintf(s->fp, branch_color_local, "%s", branch_name);
+	color_fprintf(s->fp, header_color, "...");
+	color_fprintf(s->fp, branch_color_remote, "%s", base);
+
+	color_fprintf(s->fp, header_color, " [");
+	if (!num_ours) {
+		color_fprintf(s->fp, header_color, "behind ");
+		color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
+	} else if (!num_theirs) {
+		color_fprintf(s->fp, header_color, "ahead ");
+		color_fprintf(s->fp, branch_color_local, "%d", num_ours);
+	} else {
+		color_fprintf(s->fp, header_color, "ahead ");
+		color_fprintf(s->fp, branch_color_local, "%d", num_ours);
+		color_fprintf(s->fp, header_color, ", behind ");
+		color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
+	}
+
+	color_fprintf_ln(s->fp, header_color, "]");
+}
+
 void wt_shortstatus_print(struct wt_status *s, int null_termination)
 {
 	int i;
+
+	wt_shortstatus_print_tracking(s);
+
 	for (i = 0; i < s->change.nr; i++) {
 		struct wt_status_change_data *d;
 		struct string_list_item *it;
diff --git a/wt-status.h b/wt-status.h
index 9120673..ca5db99 100644
--- a/wt-status.h
+++ b/wt-status.h
@@ -12,6 +12,8 @@ enum color_wt_status {
 	WT_STATUS_UNTRACKED,
 	WT_STATUS_NOBRANCH,
 	WT_STATUS_UNMERGED,
+	WT_STATUS_LOCAL_BRANCH,
+	WT_STATUS_REMOTE_BRANCH,
 };

 enum untracked_status_type {
@@ -42,7 +44,7 @@ struct wt_status {
 	int relative_paths;
 	int submodule_summary;
 	enum untracked_status_type show_untracked_files;
-	char color_palette[WT_STATUS_UNMERGED+1][COLOR_MAXLEN];
+	char color_palette[WT_STATUS_REMOTE_BRANCH+1][COLOR_MAXLEN];

 	/* These are computed during processing of the individual sections */
 	int commitable;
-- 
1.7.1.13.g5cd87

-- 
typed with http://neo-layout.org
myFtPhp -- visit http://myftphp.sf.net -- v. 0.4.7 released!
Previous: Jeff KingNext: Knittl
Message 3 of 13 in “Show branch information in short output of git status”
  1. Show branch information in short output of git statusKnittl, May 2, 2010
  2. Jeff KingMay 5, 2010
  3. KnittlMay 6, 2010
  4. KnittlMay 7, 2010
  5. Jeff KingMay 12, 2010
  6. KnittlMay 14, 2010
  7. Jeff KingMay 23, 2010
  8. Michael J GruberMay 25, 2010
  9. Jeff KingMay 25, 2010
  10. Michael J GruberMay 25, 2010
  11. Documentation/SubmittingPatches: clarify GMail section and SMTPMichael J Gruber, May 25, 2010
  12. Jeff KingMay 25, 2010
  13. Ævar Arnfjörð BjarmasonMay 25, 2010

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.