Skip to content

Commit 7a48b83

Browse files
artagnongitster
authored andcommitted
for-each-ref: introduce %(HEAD) asterisk marker
'git branch' shows which branch you are currently on with an '*', but 'git for-each-ref' misses this feature. So, extend its format with %(HEAD) for the same effect. Now you can use the following format in for-each-ref: %(HEAD) %(refname:short) to display an asterisk next to the current ref. Signed-off-by: Ramkumar Ramachandra <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 189a546 commit 7a48b83

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ upstream::
9393
from the displayed ref. Respects `:short` in the same way as
9494
`refname` above.
9595

96+
HEAD::
97+
'*' if HEAD matches current ref (the checked out branch), ' '
98+
otherwise.
99+
96100
In addition to the above, for commit and tag objects, the header
97101
field names (`tree`, `parent`, `object`, `type`, and `tag`) can
98102
be used to specify the value in the header field.

builtin/for-each-ref.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ static struct {
7575
{ "upstream" },
7676
{ "symref" },
7777
{ "flag" },
78+
{ "HEAD" },
7879
};
7980

8081
/*
@@ -675,8 +676,16 @@ static void populate_value(struct refinfo *ref)
675676
v->s = xstrdup(buf + 1);
676677
}
677678
continue;
678-
}
679-
else
679+
} else if (!strcmp(name, "HEAD")) {
680+
const char *head;
681+
unsigned char sha1[20];
682+
head = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
683+
if (!strcmp(ref->refname, head))
684+
v->s = "*";
685+
else
686+
v->s = " ";
687+
continue;
688+
} else
680689
continue;
681690

682691
formatp = strchr(name, ':');

t/t6300-for-each-ref.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ test_atom head contents:body ''
7777
test_atom head contents:signature ''
7878
test_atom head contents 'Initial
7979
'
80+
test_atom head HEAD '*'
8081

8182
test_atom tag refname refs/tags/testtag
8283
test_atom tag upstream ''
@@ -110,6 +111,7 @@ test_atom tag contents:body ''
110111
test_atom tag contents:signature ''
111112
test_atom tag contents 'Tagging at 1151939927
112113
'
114+
test_atom tag HEAD ' '
113115

114116
test_expect_success 'Check invalid atoms names are errors' '
115117
test_must_fail git for-each-ref --format="%(INVALID)" refs/heads

0 commit comments

Comments
 (0)