From: "SZEDER Gábor" <szeder@ira.uka.de>
To: "Shawn O. Pearce" <spearce@spearce.org>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>
Subject: [RFC/PATCH 1/2 v2] for-each-ref: add new format 'refbasename'
Date: Fri, 29 Aug 2008 18:45:08 +0200 [thread overview]
Message-ID: <20080829164508.GF8000@neumann> (raw)
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:
> 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
next prev parent reply other threads:[~2008-08-29 16:46 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-08-29 10:37 [RFC/PATCH 1/2] for-each-ref: add new format 'refbasename' SZEDER Gábor
2008-08-29 10:37 ` [RFC/PATCH 2/2] bash: use for-each-ref " SZEDER Gábor
2008-08-29 14:34 ` [RFC/PATCH 1/2] for-each-ref: add new " Shawn O. Pearce
2008-08-29 16:45 ` SZEDER Gábor [this message]
2008-08-29 18:21 ` Bert Wesarg
2008-08-29 21:41 ` [PATCH] for-each-ref: new 'refshort' format Bert Wesarg
2008-08-31 4:31 ` Junio C Hamano
2008-08-31 7:11 ` Bert Wesarg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080829164508.GF8000@neumann \
--to=szeder@ira.uka.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=spearce@spearce.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).