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@xxxxxxxxxx> --- On Fri, Aug 29, 2008 at 07:34:48AM -0700, Shawn O. Pearce wrote: > SZEDER GGGbor <szeder@xxxxxxxxxx> 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 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html