From: Jeff King <peff@peff.net>
To: "René Scharfe" <l.s.r@web.de>
Cc: Git Mailing List <git@vger.kernel.org>,
Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH] name-rev: use skip_prefix() instead of starts_with()
Date: Tue, 26 Nov 2019 11:07:37 -0500 [thread overview]
Message-ID: <20191126160737.GC25729@sigill.intra.peff.net> (raw)
In-Reply-To: <23925fba-9413-0596-b21a-f49aac922f88@web.de>
On Tue, Nov 26, 2019 at 04:23:31PM +0100, René Scharfe wrote:
> diff --git a/builtin/name-rev.c b/builtin/name-rev.c
> index b0f0776947..c261d661d7 100644
> --- a/builtin/name-rev.c
> +++ b/builtin/name-rev.c
> @@ -161,10 +161,8 @@ static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
> {
> if (shorten_unambiguous)
> refname = shorten_unambiguous_ref(refname, 0);
> - else if (starts_with(refname, "refs/heads/"))
> - refname = refname + 11;
> - else if (starts_with(refname, "refs/"))
> - refname = refname + 5;
> + else if (!skip_prefix(refname, "refs/heads/", &refname))
> + skip_prefix(refname, "refs/", &refname);
> return refname;
And this one is correct because we were already mutating the pointer.
Good.
Collapsing the conditional makes sense. IMHO it might be a little easier
to follow if we keep else-if non-negated, like:
if (shorten_unambiguous)
refname = shorten_unambiguous(refname, 0);
else if (skip_prefix(refname, "refs/heads/", &refname))
; /* refname already advanced */
else
skip_prefix(refname, "refs/", &refname);
but I'm fine with it either way.
Also, I think we are leaking the result of shorten_unambiguous_refename
here (the caller won't free it, as we return a const; but anyway we
sometimes return a pointer into the existing const string so it wouldn't
be safe). That's all outside your patch, obviously.
-Peff
prev parent reply other threads:[~2019-11-26 16:07 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-11-26 15:23 [PATCH] name-rev: use skip_prefix() instead of starts_with() René Scharfe
2019-11-26 16:07 ` Jeff King [this message]
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=20191126160737.GC25729@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=l.s.r@web.de \
/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).