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 <artagnon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ramkumar Ramachandra committed Nov 18, 2013 at 23:09 UTC 7a48b8321934384098a8392b3c4c814ec5058d00
3 files changed +17 -2
Documentation/git-for-each-ref.txt
+4
@@ -93,6 +93,10 @@ upstream::
93 from the displayed ref. Respects `:short` in the same way as
94 `refname` above.
95
96 +HEAD::
97 + '*' if HEAD matches current ref (the checked out branch), ' '
98 + otherwise.
99 +
100 In addition to the above, for commit and tag objects, the header
101 field names (`tree`, `parent`, `object`, `type`, and `tag`) can
102 be used to specify the value in the header field.
builtin/for-each-ref.c
+11 -2
@@ -75,6 +75,7 @@ static struct {
75 { "upstream" },
76 { "symref" },
77 { "flag" },
78 + { "HEAD" },
79 };
80
81 /*
@@ -675,8 +676,16 @@ static void populate_value(struct refinfo *ref)
676 v->s = xstrdup(buf + 1);
677 }
678 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
689 continue;
690
691 formatp = strchr(name, ':');
t/t6300-for-each-ref.sh
+2
@@ -77,6 +77,7 @@ test_atom head contents:body ''
77 test_atom head contents:signature ''
78 test_atom head contents 'Initial
79 '
80 +test_atom head HEAD '*'
81
82 test_atom tag refname refs/tags/testtag
83 test_atom tag upstream ''
@@ -110,6 +111,7 @@ test_atom tag contents:body ''
111 test_atom tag contents:signature ''
112 test_atom tag contents 'Tagging at 1151939927
113 '
114 +test_atom tag HEAD ' '
115
116 test_expect_success 'Check invalid atoms names are errors' '
117 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads