git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC/PATCH 1/2 v2] for-each-ref: add new format 'refbasename'

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Aug 29, 2008, 16:45 UTC
Message-ID
<20080829164508.GF8000@neumann>
In-Reply-To
<20080829143448.GC7403@spearce.org>

for-each-ref's refname format outputs each ref in its full format, e.g. 'refs/heads/foo' and 'refs/tags/bar'. However, there are tools that need only the last part of the refname, e.g. only 'foo' and 'bar'. Such a tool is git's bash completion script, which spends considerable amount of time removing the unneeded parts from for-each-ref's output.

Therefore, we introduce a new for-each-ref format called 'refbasename', which strips the leading parts of the refname:

  * If the refname doesn't contain any '/', then it is printed as is.
  * If the refname contains one '/', then the string following the '/'
    is printed (e.g. 'refs/foo' becomes 'foo').
  * If the refname contains two (or more) '/', then the string following
    the second '/' is printed (e.g. 'refs/heads/foo' becomes 'foo').
    
Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
On Fri, Aug 29, 2008 at 07:34:48AM -0700, Shawn O. Pearce wrote:
Show 12 quoted lines
> SZEDER GGGbor <szeder@ira.uka.de> wrote:
> > +		} else if (!strcmp(name, "refbasename")) {
> > +			char * p = strchr(ref->refname, '/');
> > +			p = strchr(p+1, '/');
> > +			v->s = p+1;
> 
> Please be careful here and check for !p.  A refname may be missing
> one or two '/' in which case you will cause the process to segfault.
> 
> I don't think its a good idea to assume you'll always have to '/'
> in the name.  "refs/foo" can be created by git-update-ref.  Or if
> we ever started to report on HEAD this output tag would crash.
Ah, I feared as much.

This raises the question what should the refbasename format print in those cases. The first case (no '/' at all) is trivial, but the other two are a bit problematic:

  * The refname 'foo/bar' seems to be valid, or at least the command
    sequence 'mkdir .git/foo; cp .git/refs/heads/master .git/foo/bar;
    git checkout foo/bar' works as expected.  OTOH, 'git for-each-ref
    --format="%(refname)"' doesn't print anything.
  * The refname 'refs/head/foo/bar' should become 'foo/bar':  it's in 
    sync with how 'git branch' lists it, and this behaviour is needed
    by the bash completion changes.  However, unfortunately it
    contradicts to the format's name, because basename removes all
    directory components.  Maybe someone has a better idea on how to
    name this format...
 builtin-for-each-ref.c  |   12 ++++++++++++
 t/t6300-for-each-ref.sh |    2 ++
 2 files changed, 14 insertions(+), 0 deletions(-)
diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c
index 21e92bb..23d064b 100644
--- a/builtin-for-each-ref.c
+++ b/builtin-for-each-ref.c
@@ -66,6 +66,7 @@ static struct {
 	{ "subject" },
 	{ "body" },
 	{ "contents" },
+	{ "refbasename" },
 };
 
 /*
@@ -577,6 +578,17 @@ static void populate_value(struct refinfo *ref)
 			char *s = xmalloc(len + 4);
 			sprintf(s, "%s^{}", ref->refname);
 			v->s = s;
+		} else if (!strcmp(name, "refbasename")) {
+			char * slash1 = strchr(ref->refname, '/');
+			if (!slash1)
+				v->s = ref->refname;
+			else {
+				char * slash2 = strchr(slash1 + 1, '/');
+				if (!slash2)
+					v->s = slash1 + 1;
+				else
+					v->s = slash2 + 1;
+			}
 		}
 	}
 
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 8ced593..b317a01 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -66,6 +66,7 @@ test_atom head subject 'Initial'
 test_atom head body ''
 test_atom head contents 'Initial
 '
+test_atom head refbasename 'master'
 
 test_atom tag refname refs/tags/testtag
 test_atom tag objecttype tag
@@ -95,6 +96,7 @@ test_atom tag subject 'Tagging at 1151939927'
 test_atom tag body ''
 test_atom tag contents 'Tagging at 1151939927
 '
+test_atom tag refbasename 'testtag'
 
 test_expect_success 'Check invalid atoms names are errors' '
 	test_must_fail git-for-each-ref --format="%(INVALID)" refs/heads
-- 
1.6.0.1.149.g903b0
Previous: Shawn O. PearceNext: Bert Wesarg
Message 4 of 8 in “for-each-ref: add new format 'refbasename'”
  1. 1/2 for-each-ref: add new format 'refbasename'SZEDER Gábor, Aug 29, 2008
  2. 2/2 bash: use for-each-ref format 'refbasename'SZEDER Gábor, Aug 29, 2008
  3. Shawn O. PearceAug 29, 2008
  4. 1/2 for-each-ref: add new format 'refbasename'SZEDER Gábor, Aug 29, 2008
  5. Bert WesargAug 29, 2008
  6. for-each-ref: new 'refshort' formatBert Wesarg, Aug 29, 2008
  7. Junio C HamanoAug 31, 2008
  8. Bert WesargAug 31, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.