# [PATCH] git-for-each-ref: add new field name "symbolic"

3 messages from 2010-02-13 to 2010-02-13. Participants: Larry D'Anna, Junio C Hamano, Jeff King.
Thread: https://gitlist.dev/t/22638

## Larry D'Anna, 2010-02-13 05:55

Subject: [PATCH] git-for-each-ref: add new field name "symbolic"
Message-ID: <1266040518-11748-1-git-send-email-larry@elder-gods.org>
URL: https://gitlist.dev/e/1266040518-11748-1-git-send-email-larry%40elder-gods.org

```
%(symbolic) will expand to "true" for symbolic refs, and "false" otherwise.
This is a lot faster than calling git-symbolic-ref on every single ref from your
shell script.

Signed-off-by: Larry D'Anna <larry@elder-gods.org>
---
 Documentation/git-for-each-ref.txt |    3 +++
 builtin-for-each-ref.c             |    5 +++++
 2 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index 7e83288..c2a694f 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -92,6 +92,9 @@ upstream::
 	from the displayed ref. Respects `:short` in the same way as
 	`refname` above.
 
+symbolic::
+	"true" for symbolic refs, "false" otherwise.
+
 In addition to the above, for commit and tag objects, the header
 field names (`tree`, `parent`, `object`, `type`, and `tag`) can
 be used to specify the value in the header field.
diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c
index a5a83f1..fd6973e 100644
--- a/builtin-for-each-ref.c
+++ b/builtin-for-each-ref.c
@@ -33,6 +33,7 @@ struct ref_sort {
 struct refinfo {
 	char *refname;
 	unsigned char objectname[20];
+	int flag;
 	struct atom_value *value;
 };
 
@@ -66,6 +67,7 @@ static struct {
 	{ "creatordate", FIELD_TIME },
 	{ "subject" },
 	{ "body" },
+	{ "symbolic" },
 	{ "contents" },
 	{ "upstream" },
 };
@@ -576,6 +578,8 @@ static void populate_value(struct refinfo *ref)
 
 		if (!prefixcmp(name, "refname"))
 			refname = ref->refname;
+		else if (!prefixcmp(name, "symbolic"))
+			refname = (ref->flag & REF_ISSYMREF) ? "true" : "false";
 		else if (!prefixcmp(name, "upstream")) {
 			struct branch *branch;
 			/* only local branches may have an upstream */
@@ -716,6 +720,7 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f
 	 * by maxcount logic.
 	 */
 	ref = xcalloc(1, sizeof(*ref));
+	ref->flag = flag;
 	ref->refname = xstrdup(refname);
 	hashcpy(ref->objectname, sha1);
 
-- 
1.7.0.rc2.40.g7d8aa

```

## Junio C Hamano, 2010-02-13 07:45

Subject: Re: [PATCH] git-for-each-ref: add new field name "symbolic"
Message-ID: <7v4ollshn5.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v4ollshn5.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1266040518-11748-1-git-send-email-larry@elder-gods.org>

```
Larry D'Anna <larry@elder-gods.org> writes:

> %(symbolic) will expand to "true" for symbolic refs, and "false" otherwise.
> This is a lot faster than calling git-symbolic-ref on every single ref from your
> shell script.

I think this is a good idea, but I suspect that much more than half of
people who iterate over for-each-ref output and run symbolic-ref would
actually want to learn what actual ref a symbolic one points at.  So you
may not be helping as much people as you could by going in this direction.

Instead of expanding to "true", why not make it show the expansion if it
is symbolic, and to empty if it is not?

```

## Jeff King, 2010-02-13 08:51

Subject: Re: [PATCH] git-for-each-ref: add new field name "symbolic"
Message-ID: <20100213085132.GA13181@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20100213085132.GA13181%40coredump.intra.peff.net
In-Reply-To: <7v4ollshn5.fsf@alter.siamese.dyndns.org>

```
On Fri, Feb 12, 2010 at 11:45:50PM -0800, Junio C Hamano wrote:

> Larry D'Anna <larry@elder-gods.org> writes:
> 
> > %(symbolic) will expand to "true" for symbolic refs, and "false" otherwise.
> > This is a lot faster than calling git-symbolic-ref on every single ref from your
> > shell script.
> 
> I think this is a good idea, but I suspect that much more than half of
> people who iterate over for-each-ref output and run symbolic-ref would
> actually want to learn what actual ref a symbolic one points at.  So you
> may not be helping as much people as you could by going in this direction.
> 
> Instead of expanding to "true", why not make it show the expansion if it
> is symbolic, and to empty if it is not?

That was my first thought, too. Unfortunately it needs some pretty heavy
refactoring of the for_each_ref call chain, as we throw away the
dereferenced ref name at the very bottom.

So while I would be in favor of a real "%(symbolic)" as you describe, it
is significantly more work. If we are just going to do a flag check,
though, I would much rather see it called "%(issymbolic)" or something
so that "%(symbolic)" can be used later for the actual value.

One other alternative would be to simply expose "%(flags)" which would
expand to nothing, "symbolic", "packed", or "symbolic,packed".

-Peff

```
