Skip to content

Commit 94fcb73

Browse files
committed
git-branch -v: show the remote tracking statistics
This teaches "git branch -v" to insert the remote tracking statistics in brackets, just before the one-liner commit log message for the branch. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b6975ab commit 94fcb73

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

builtin-branch.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,23 @@ static int ref_cmp(const void *r1, const void *r2)
282282
return strcmp(c1->name, c2->name);
283283
}
284284

285+
static void fill_tracking_info(char *stat, const char *branch_name)
286+
{
287+
int ours, theirs;
288+
struct branch *branch = branch_get(branch_name);
289+
290+
if (!stat_tracking_info(branch, &ours, &theirs) || (!ours && !theirs)) {
291+
stat[0] = '\0';
292+
return;
293+
}
294+
if (!ours)
295+
sprintf(stat, "[behind %d] ", theirs);
296+
else if (!theirs)
297+
sprintf(stat, "[ahead %d] ", ours);
298+
else
299+
sprintf(stat, "[ahead %d, behind %d] ", ours, theirs);
300+
}
301+
285302
static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
286303
int abbrev, int current)
287304
{
@@ -310,6 +327,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
310327
if (verbose) {
311328
struct strbuf subject;
312329
const char *sub = " **** invalid ref ****";
330+
char stat[128];
313331

314332
strbuf_init(&subject, 0);
315333

@@ -319,10 +337,15 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
319337
&subject, 0, NULL, NULL, 0, 0);
320338
sub = subject.buf;
321339
}
322-
printf("%c %s%-*s%s %s %s\n", c, branch_get_color(color),
340+
341+
if (item->kind == REF_LOCAL_BRANCH)
342+
fill_tracking_info(stat, item->name);
343+
344+
printf("%c %s%-*s%s %s %s%s\n", c, branch_get_color(color),
323345
maxwidth, item->name,
324346
branch_get_color(COLOR_BRANCH_RESET),
325-
find_unique_abbrev(item->sha1, abbrev), sub);
347+
find_unique_abbrev(item->sha1, abbrev),
348+
stat, sub);
326349
strbuf_release(&subject);
327350
} else {
328351
printf("%c %s%s%s\n", c, branch_get_color(color), item->name,

0 commit comments

Comments
 (0)