Skip to content

Commit b28061c

Browse files
artagnongitster
authored andcommitted
for-each-ref: introduce %(upstream:track[short])
Introduce %(upstream:track) to display "[ahead M, behind N]" and %(upstream:trackshort) to display "=", ">", "<", or "<>" appropriately (inspired by contrib/completion/git-prompt.sh). Now you can use the following format in for-each-ref: %(refname:short)%(upstream:trackshort) to display refs with terse tracking information. Note that :track and :trackshort only work with "upstream", and error out when used with anything else. Signed-off-by: Ramkumar Ramachandra <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7a48b83 commit b28061c

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ objectname::
9191
upstream::
9292
The name of a local ref which can be considered ``upstream''
9393
from the displayed ref. Respects `:short` in the same way as
94-
`refname` above.
94+
`refname` above. Additionally respects `:track` to show
95+
"[ahead N, behind M]" and `:trackshort` to show the terse
96+
version: ">" (ahead), "<" (behind), "<>" (ahead and behind),
97+
or "=" (in sync). Has no effect if the ref does not have
98+
tracking information associated with it.
9599

96100
HEAD::
97101
'*' if HEAD matches current ref (the checked out branch), ' '

builtin/for-each-ref.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ static void populate_value(struct refinfo *ref)
641641
int deref = 0;
642642
const char *refname;
643643
const char *formatp;
644+
struct branch *branch = NULL;
644645

645646
if (*name == '*') {
646647
deref = 1;
@@ -652,7 +653,6 @@ static void populate_value(struct refinfo *ref)
652653
else if (!prefixcmp(name, "symref"))
653654
refname = ref->symref ? ref->symref : "";
654655
else if (!prefixcmp(name, "upstream")) {
655-
struct branch *branch;
656656
/* only local branches may have an upstream */
657657
if (prefixcmp(ref->refname, "refs/heads/"))
658658
continue;
@@ -679,6 +679,7 @@ static void populate_value(struct refinfo *ref)
679679
} else if (!strcmp(name, "HEAD")) {
680680
const char *head;
681681
unsigned char sha1[20];
682+
682683
head = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
683684
if (!strcmp(ref->refname, head))
684685
v->s = "*";
@@ -689,13 +690,46 @@ static void populate_value(struct refinfo *ref)
689690
continue;
690691

691692
formatp = strchr(name, ':');
692-
/* look for "short" refname format */
693693
if (formatp) {
694+
int num_ours, num_theirs;
695+
694696
formatp++;
695697
if (!strcmp(formatp, "short"))
696698
refname = shorten_unambiguous_ref(refname,
697699
warn_ambiguous_refs);
698-
else
700+
else if (!strcmp(formatp, "track") &&
701+
!prefixcmp(name, "upstream")) {
702+
char buf[40];
703+
704+
stat_tracking_info(branch, &num_ours, &num_theirs);
705+
if (!num_ours && !num_theirs)
706+
v->s = "";
707+
else if (!num_ours) {
708+
sprintf(buf, "[behind %d]", num_theirs);
709+
v->s = xstrdup(buf);
710+
} else if (!num_theirs) {
711+
sprintf(buf, "[ahead %d]", num_ours);
712+
v->s = xstrdup(buf);
713+
} else {
714+
sprintf(buf, "[ahead %d, behind %d]",
715+
num_ours, num_theirs);
716+
v->s = xstrdup(buf);
717+
}
718+
continue;
719+
} else if (!strcmp(formatp, "trackshort") &&
720+
!prefixcmp(name, "upstream")) {
721+
assert(branch);
722+
stat_tracking_info(branch, &num_ours, &num_theirs);
723+
if (!num_ours && !num_theirs)
724+
v->s = "=";
725+
else if (!num_ours)
726+
v->s = "<";
727+
else if (!num_theirs)
728+
v->s = ">";
729+
else
730+
v->s = "<>";
731+
continue;
732+
} else
699733
die("unknown %.*s format %s",
700734
(int)(formatp - name), name, formatp);
701735
}

t/t6300-for-each-ref.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,33 @@ test_expect_success 'Check short upstream format' '
303303
test_cmp expected actual
304304
'
305305

306+
test_expect_success 'setup for upstream:track[short]' '
307+
test_commit two
308+
'
309+
310+
cat >expected <<EOF
311+
[ahead 1]
312+
EOF
313+
314+
test_expect_success 'Check upstream:track format' '
315+
git for-each-ref --format="%(upstream:track)" refs/heads >actual &&
316+
test_cmp expected actual
317+
'
318+
319+
cat >expected <<EOF
320+
>
321+
EOF
322+
323+
test_expect_success 'Check upstream:trackshort format' '
324+
git for-each-ref --format="%(upstream:trackshort)" refs/heads >actual &&
325+
test_cmp expected actual
326+
'
327+
328+
test_expect_success 'Check that :track[short] cannot be used with other atoms' '
329+
test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
330+
test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
331+
'
332+
306333
cat >expected <<EOF
307334
$(git rev-parse --short HEAD)
308335
EOF

0 commit comments

Comments
 (0)