threads / patch / 22638

patchgit-for-each-ref: add new field name "symbolic"

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

## tl;dr

3 messages between Feb 13, 2010 and Feb 13, 2010. Diffs are folded; open one to read it.

replies: 2people: 3as markdown or json

Larry D'Anna· Feb 13, 2010, 05:55 UTC · lore

%(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(-)
Show changes to 2 files +8 −0

Documentation/git-for-each-ref.txt, builtin-for-each-ref.c

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· Feb 13, 2010, 07:45 UTC · re: Larry D'Anna · lore

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

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· Feb 13, 2010, 08:51 UTC · re: Junio C Hamano · lore

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

On Fri, Feb 12, 2010 at 11:45:50PM -0800, Junio C Hamano wrote:
Show 13 quoted lines
> 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

← back to recent threads