@matheusbsbs / matheus-bagliano / commits / 5cdd628c84
Fork 0

for-each-ref --format='%(symref) %(symref:short)'

New %(symref) output atom expands to the name of the ref a symbolic ref points at, or an empty string if the ref being shown is not a symref. This may help scripted Porcelain writers. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Feb 13, 2010 at 11:57 UTC 5cdd628c84d808feb6ec407b2ecd74dfea63f865
1 file changed +19 -1
builtin-for-each-ref.c
+19 -1
@@ -33,6 +33,8 @@ struct ref_sort {
33 struct refinfo {
34 char *refname;
35 unsigned char objectname[20];
36 + int flag;
37 + const char *symref;
38 struct atom_value *value;
39 };
40
@@ -68,6 +70,7 @@ static struct {
70 { "body" },
71 { "contents" },
72 { "upstream" },
73 + { "symref" },
74 };
75
76 /*
@@ -82,7 +85,7 @@ static struct {
85 */
86 static const char **used_atom;
87 static cmp_type *used_atom_type;
85 -static int used_atom_cnt, sort_atom_limit, need_tagged;
88 +static int used_atom_cnt, sort_atom_limit, need_tagged, need_symref;
89
90 /*
91 * Used to parse format string and sort specifiers
@@ -135,6 +138,8 @@ static int parse_atom(const char *atom, const char *ep)
138 used_atom_type[at] = valid_atom[i].cmp_type;
139 if (*atom == '*')
140 need_tagged = 1;
141 + if (!strcmp(used_atom[at], "symref"))
142 + need_symref = 1;
143 return at;
144 }
145
@@ -566,6 +571,16 @@ static void populate_value(struct refinfo *ref)
571
572 ref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt);
573
574 + if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
575 + unsigned char unused1[20];
576 + const char *symref;
577 + symref = resolve_ref(ref->refname, unused1, 1, NULL);
578 + if (symref)
579 + ref->symref = xstrdup(symref);
580 + else
581 + ref->symref = "";
582 + }
583 +
584 /* Fill in specials first */
585 for (i = 0; i < used_atom_cnt; i++) {
586 const char *name = used_atom[i];
@@ -581,6 +596,8 @@ static void populate_value(struct refinfo *ref)
596
597 if (!prefixcmp(name, "refname"))
598 refname = ref->refname;
599 + else if (!prefixcmp(name, "symref"))
600 + refname = ref->symref ? ref->symref : "";
601 else if (!prefixcmp(name, "upstream")) {
602 struct branch *branch;
603 /* only local branches may have an upstream */
@@ -726,6 +743,7 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f
743 ref = xcalloc(1, sizeof(*ref));
744 ref->refname = xstrdup(refname);
745 hashcpy(ref->objectname, sha1);
746 + ref->flag = flag;
747
748 cnt = cb->grab_cnt;
749 cb->grab_array = xrealloc(cb->grab_array,