# [PATCH] refs.c: use skip_prefix() in prettify_refname()

11 messages from 2017-03-23 to 2017-03-23. Participants: SZEDER Gábor, René Scharfe, Jeff King, Stefan Beller, Junio C Hamano.
Thread: https://gitlist.dev/t/45482

## SZEDER Gábor, 2017-03-23 15:50

Subject: [PATCH] refs.c: use skip_prefix() in prettify_refname()
Message-ID: <20170323155012.6148-1-szeder.dev@gmail.com>
URL: https://gitlist.dev/e/20170323155012.6148-1-szeder.dev%40gmail.com

```
This eliminates three magic numbers.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 refs.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/refs.c b/refs.c
index e7606716d..0272e332c 100644
--- a/refs.c
+++ b/refs.c
@@ -366,11 +366,11 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
 
 const char *prettify_refname(const char *name)
 {
-	return name + (
-		starts_with(name, "refs/heads/") ? 11 :
-		starts_with(name, "refs/tags/") ? 10 :
-		starts_with(name, "refs/remotes/") ? 13 :
-		0);
+	if (skip_prefix(name, "refs/heads/", &name) ||
+	    skip_prefix(name, "refs/tags/", &name) ||
+	    skip_prefix(name, "refs/remotes/", &name))
+		; /* nothing */
+	return name;
 }
 
 static const char *ref_rev_parse_rules[] = {
-- 
2.12.1.485.g1616aa492


```

## René Scharfe, 2017-03-23 19:18

Subject: Re: [PATCH] refs.c: use skip_prefix() in prettify_refname()
Message-ID: <4382e211-63f6-811f-6f33-1cf5d2c087a8@web.de>
URL: https://gitlist.dev/e/4382e211-63f6-811f-6f33-1cf5d2c087a8%40web.de
In-Reply-To: <20170323155012.6148-1-szeder.dev@gmail.com>

```
Am 23.03.2017 um 16:50 schrieb SZEDER Gábor:
> This eliminates three magic numbers.
>
> Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
> ---
>  refs.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/refs.c b/refs.c
> index e7606716d..0272e332c 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -366,11 +366,11 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
>
>  const char *prettify_refname(const char *name)
>  {
> -	return name + (
> -		starts_with(name, "refs/heads/") ? 11 :
> -		starts_with(name, "refs/tags/") ? 10 :
> -		starts_with(name, "refs/remotes/") ? 13 :
> -		0);
> +	if (skip_prefix(name, "refs/heads/", &name) ||
> +	    skip_prefix(name, "refs/tags/", &name) ||
> +	    skip_prefix(name, "refs/remotes/", &name))
> +		; /* nothing */
> +	return name;

Nice, but why add the "if" when it's doing nothing?

René

```

## Jeff King, 2017-03-23 19:23

Subject: Re: [PATCH] refs.c: use skip_prefix() in prettify_refname()
Message-ID: <20170323192313.ytr56pjpnillnh63@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20170323192313.ytr56pjpnillnh63%40sigill.intra.peff.net
In-Reply-To: <4382e211-63f6-811f-6f33-1cf5d2c087a8@web.de>

```
On Thu, Mar 23, 2017 at 08:18:26PM +0100, René Scharfe wrote:

> Am 23.03.2017 um 16:50 schrieb SZEDER Gábor:
> > This eliminates three magic numbers.
> > 
> > Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
> > ---
> >  refs.c | 10 +++++-----
> >  1 file changed, 5 insertions(+), 5 deletions(-)
> > 
> > diff --git a/refs.c b/refs.c
> > index e7606716d..0272e332c 100644
> > --- a/refs.c
> > +++ b/refs.c
> > @@ -366,11 +366,11 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
> > 
> >  const char *prettify_refname(const char *name)
> >  {
> > -	return name + (
> > -		starts_with(name, "refs/heads/") ? 11 :
> > -		starts_with(name, "refs/tags/") ? 10 :
> > -		starts_with(name, "refs/remotes/") ? 13 :
> > -		0);
> > +	if (skip_prefix(name, "refs/heads/", &name) ||
> > +	    skip_prefix(name, "refs/tags/", &name) ||
> > +	    skip_prefix(name, "refs/remotes/", &name))
> > +		; /* nothing */
> > +	return name;
> 
> Nice, but why add the "if" when it's doing nothing?

It's short-circuiting in the conditional.

-Peff

```

## Stefan Beller, 2017-03-23 19:31

Subject: Re: [PATCH] refs.c: use skip_prefix() in prettify_refname()
Message-ID: <CAGZ79kZ-yP=6bKnDg8zKjebDkDdQ4ZEur0sC+CCcv5OTdEC3RA@mail.gmail.com>
URL: https://gitlist.dev/e/CAGZ79kZ-yP%3D6bKnDg8zKjebDkDdQ4ZEur0sC%2BCCcv5OTdEC3RA%40mail.gmail.com
In-Reply-To: <20170323192313.ytr56pjpnillnh63@sigill.intra.peff.net>

```
On Thu, Mar 23, 2017 at 12:23 PM, Jeff King <peff@peff.net> wrote:
> On Thu, Mar 23, 2017 at 08:18:26PM +0100, René Scharfe wrote:
>
>> Am 23.03.2017 um 16:50 schrieb SZEDER Gábor:
>> > This eliminates three magic numbers.
>> >
>> > Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
>> > ---
>> >  refs.c | 10 +++++-----
>> >  1 file changed, 5 insertions(+), 5 deletions(-)
>> >
>> > diff --git a/refs.c b/refs.c
>> > index e7606716d..0272e332c 100644
>> > --- a/refs.c
>> > +++ b/refs.c
>> > @@ -366,11 +366,11 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
>> >
>> >  const char *prettify_refname(const char *name)
>> >  {
>> > -   return name + (
>> > -           starts_with(name, "refs/heads/") ? 11 :
>> > -           starts_with(name, "refs/tags/") ? 10 :
>> > -           starts_with(name, "refs/remotes/") ? 13 :
>> > -           0);
>> > +   if (skip_prefix(name, "refs/heads/", &name) ||
>> > +       skip_prefix(name, "refs/tags/", &name) ||
>> > +       skip_prefix(name, "refs/remotes/", &name))
>> > +           ; /* nothing */
>> > +   return name;
>>
>> Nice, but why add the "if" when it's doing nothing?
>
> It's short-circuiting in the conditional.

You do not need a conditional to short circuit things.
|| works outside an if, too. ;)

Anyway, maybe it's worth spelling it out with an if .. else if
cascade for readability?

After your comment of short-circuiting this code is pretty clear,
so maybe just a small comment would do?

Thanks,
Stefan

>
> -Peff

```

## Junio C Hamano, 2017-03-23 19:33

Subject: Re: [PATCH] refs.c: use skip_prefix() in prettify_refname()
Message-ID: <xmqqvaqzbxfx.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqvaqzbxfx.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <20170323192313.ytr56pjpnillnh63@sigill.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> On Thu, Mar 23, 2017 at 08:18:26PM +0100, René Scharfe wrote:
>
>> Am 23.03.2017 um 16:50 schrieb SZEDER Gábor:
>> > This eliminates three magic numbers.
>> > 
>> > Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
>> > ---
>> >  refs.c | 10 +++++-----
>> >  1 file changed, 5 insertions(+), 5 deletions(-)
>> > 
>> > diff --git a/refs.c b/refs.c
>> > index e7606716d..0272e332c 100644
>> > --- a/refs.c
>> > +++ b/refs.c
>> > @@ -366,11 +366,11 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
>> > 
>> >  const char *prettify_refname(const char *name)
>> >  {
>> > -	return name + (
>> > -		starts_with(name, "refs/heads/") ? 11 :
>> > -		starts_with(name, "refs/tags/") ? 10 :
>> > -		starts_with(name, "refs/remotes/") ? 13 :
>> > -		0);
>> > +	if (skip_prefix(name, "refs/heads/", &name) ||
>> > +	    skip_prefix(name, "refs/tags/", &name) ||
>> > +	    skip_prefix(name, "refs/remotes/", &name))
>> > +		; /* nothing */
>> > +	return name;
>> 
>> Nice, but why add the "if" when it's doing nothing?
>
> It's short-circuiting in the conditional.

I think René meant this:

     /* just for side effects */
     skip_prefix(name, "refs/heads/", &name) ||
     skip_prefix(name, "refs/tags/", &name) ||
     skip_prefix(name, "refs/remotes/", &name);

     return name;

which still short-sircuits, even though I do think it looks
strange; "correct but strange".

```

## René Scharfe, 2017-03-23 19:36

Subject: Re: [PATCH] refs.c: use skip_prefix() in prettify_refname()
Message-ID: <4424eeeb-7eea-ed41-c031-ba7915cb93ab@web.de>
URL: https://gitlist.dev/e/4424eeeb-7eea-ed41-c031-ba7915cb93ab%40web.de
In-Reply-To: <xmqqvaqzbxfx.fsf@gitster.mtv.corp.google.com>

```
Am 23.03.2017 um 20:33 schrieb Junio C Hamano:
> Jeff King <peff@peff.net> writes:
>
>> On Thu, Mar 23, 2017 at 08:18:26PM +0100, René Scharfe wrote:
>>
>>> Am 23.03.2017 um 16:50 schrieb SZEDER Gábor:
>>>> This eliminates three magic numbers.
>>>>
>>>> Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
>>>> ---
>>>>  refs.c | 10 +++++-----
>>>>  1 file changed, 5 insertions(+), 5 deletions(-)
>>>>
>>>> diff --git a/refs.c b/refs.c
>>>> index e7606716d..0272e332c 100644
>>>> --- a/refs.c
>>>> +++ b/refs.c
>>>> @@ -366,11 +366,11 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
>>>>
>>>>  const char *prettify_refname(const char *name)
>>>>  {
>>>> -	return name + (
>>>> -		starts_with(name, "refs/heads/") ? 11 :
>>>> -		starts_with(name, "refs/tags/") ? 10 :
>>>> -		starts_with(name, "refs/remotes/") ? 13 :
>>>> -		0);
>>>> +	if (skip_prefix(name, "refs/heads/", &name) ||
>>>> +	    skip_prefix(name, "refs/tags/", &name) ||
>>>> +	    skip_prefix(name, "refs/remotes/", &name))
>>>> +		; /* nothing */
>>>> +	return name;
>>>
>>> Nice, but why add the "if" when it's doing nothing?
>>
>> It's short-circuiting in the conditional.
>
> I think René meant this:
>
>      /* just for side effects */
>      skip_prefix(name, "refs/heads/", &name) ||
>      skip_prefix(name, "refs/tags/", &name) ||
>      skip_prefix(name, "refs/remotes/", &name);
>
>      return name;
>
> which still short-sircuits, even though I do think it looks
> strange; "correct but strange".

Yes.  At least to me it looks less strange than the same lines wrapped 
in "if ... /* nothing */".

René

```

## Jeff King, 2017-03-23 19:39

Subject: Re: [PATCH] refs.c: use skip_prefix() in prettify_refname()
Message-ID: <20170323193919.twwfwdoulo2hjz24@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20170323193919.twwfwdoulo2hjz24%40sigill.intra.peff.net
In-Reply-To: <xmqqvaqzbxfx.fsf@gitster.mtv.corp.google.com>

```
On Thu, Mar 23, 2017 at 12:33:06PM -0700, Junio C Hamano wrote:

> >> Nice, but why add the "if" when it's doing nothing?
> >
> > It's short-circuiting in the conditional.
> 
> I think René meant this:
> 
>      /* just for side effects */
>      skip_prefix(name, "refs/heads/", &name) ||
>      skip_prefix(name, "refs/tags/", &name) ||
>      skip_prefix(name, "refs/remotes/", &name);
> 
>      return name;
> 
> which still short-sircuits, even though I do think it looks
> strange; "correct but strange".

And it causes the compiler to complain that the value is not used.

-Peff

```

## Junio C Hamano, 2017-03-23 19:40

Subject: Re: [PATCH] refs.c: use skip_prefix() in prettify_refname()
Message-ID: <xmqqr31nbx3h.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqr31nbx3h.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <4424eeeb-7eea-ed41-c031-ba7915cb93ab@web.de>

```
René Scharfe <l.s.r@web.de> writes:

>> I think René meant this:
>>
>>      /* just for side effects */
>>      skip_prefix(name, "refs/heads/", &name) ||
>>      skip_prefix(name, "refs/tags/", &name) ||
>>      skip_prefix(name, "refs/remotes/", &name);
>>
>>      return name;
>>
>> which still short-sircuits, even though I do think it looks
>> strange; "correct but strange".
>
> Yes.  At least to me it looks less strange than the same lines wrapped
> in "if ... /* nothing */".

Yup, after looking at it again, it does not look so "strange" to me.
I probably should have said "unusual but correct", not "correct but
strange".

```

## Junio C Hamano, 2017-03-23 20:05

Subject: Re: [PATCH] refs.c: use skip_prefix() in prettify_refname()
Message-ID: <xmqqmvcbbvy3.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqmvcbbvy3.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <20170323193919.twwfwdoulo2hjz24@sigill.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> On Thu, Mar 23, 2017 at 12:33:06PM -0700, Junio C Hamano wrote:
>
>> >> Nice, but why add the "if" when it's doing nothing?
>> >
>> > It's short-circuiting in the conditional.
>> 
>> I think René meant this:
>> 
>>      /* just for side effects */
>>      skip_prefix(name, "refs/heads/", &name) ||
>>      skip_prefix(name, "refs/tags/", &name) ||
>>      skip_prefix(name, "refs/remotes/", &name);
>> 
>>      return name;
>> 
>> which still short-sircuits, even though I do think it looks
>> strange; "correct but strange".
>
> And it causes the compiler to complain that the value is not used.

Ahh.  OK.

In any case, I've queued the original with "if", which shouldn't
have that problem ;-)

```

## SZEDER Gábor, 2017-03-23 20:06

Subject: Re: [PATCH] refs.c: use skip_prefix() in prettify_refname()
Message-ID: <CAM0VKjkAMrePks4JikNvzs_v71YFidKF7yj0aNVugZ-nzx1kig@mail.gmail.com>
URL: https://gitlist.dev/e/CAM0VKjkAMrePks4JikNvzs_v71YFidKF7yj0aNVugZ-nzx1kig%40mail.gmail.com
In-Reply-To: <20170323193919.twwfwdoulo2hjz24@sigill.intra.peff.net>

```
On Thu, Mar 23, 2017 at 8:39 PM, Jeff King <peff@peff.net> wrote:
> On Thu, Mar 23, 2017 at 12:33:06PM -0700, Junio C Hamano wrote:
>
>> >> Nice, but why add the "if" when it's doing nothing?
>> >
>> > It's short-circuiting in the conditional.
>>
>> I think René meant this:
>>
>>      /* just for side effects */
>>      skip_prefix(name, "refs/heads/", &name) ||
>>      skip_prefix(name, "refs/tags/", &name) ||
>>      skip_prefix(name, "refs/remotes/", &name);
>>
>>      return name;

That was my first version indeed.  Unfortunately:

> And it causes the compiler to complain that the value is not used.

Exactly.  So that 'if' was a way to shut up the compiler complaining
about the unused result of the ||-chain.  Perhaps not the best way to
do that, but I couldn't come up with any better.

I had a note about it attached to the commit, but then run
'format-patch' without '--notes'...  oh, well.
I guess I should have wrote that in the commit message to begin with,
shouldn't I?  Or in an in-code comment.

```

## René Scharfe, 2017-03-23 20:06

Subject: Re: [PATCH] refs.c: use skip_prefix() in prettify_refname()
Message-ID: <83a38f4d-a80e-e41c-a563-05585377ac13@web.de>
URL: https://gitlist.dev/e/83a38f4d-a80e-e41c-a563-05585377ac13%40web.de
In-Reply-To: <20170323193919.twwfwdoulo2hjz24@sigill.intra.peff.net>

```
Am 23.03.2017 um 20:39 schrieb Jeff King:
> On Thu, Mar 23, 2017 at 12:33:06PM -0700, Junio C Hamano wrote:
>
>>>> Nice, but why add the "if" when it's doing nothing?
>>>
>>> It's short-circuiting in the conditional.
>>
>> I think René meant this:
>>
>>      /* just for side effects */
>>      skip_prefix(name, "refs/heads/", &name) ||
>>      skip_prefix(name, "refs/tags/", &name) ||
>>      skip_prefix(name, "refs/remotes/", &name);
>>
>>      return name;
>>
>> which still short-sircuits, even though I do think it looks
>> strange; "correct but strange".
>
> And it causes the compiler to complain that the value is not used.

Ah, that explains it, thanks.  The "if" is strange, but wrapping the 
expression in (void)(...) isn't better.

Clang doesn't warn about the code above by the way.

René

```
