# [PATCH v4] branch.c: change install_branch_config() to use skip_prefix()

3 messages from 2014-03-03 to 2014-03-03. Participants: Guanglin Xu, Eric Sunshine.
Thread: https://gitlist.dev/t/36029

## Guanglin Xu, 2014-03-03 06:36

Subject: [PATCH v4] branch.c: change install_branch_config() to use skip_prefix()
Message-ID: <1393828581-65523-1-git-send-email-mzguanglin@gmail.com>
URL: https://gitlist.dev/e/1393828581-65523-1-git-send-email-mzguanglin%40gmail.com

```
to avoid a magic code of 11.

Helped-by: Sun He <sunheeh...@gmail.com>
Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
Helped-by: Jacopo Notarstefano <jaco...@gmail.com>

Signed-off-by: Guanglin Xu <mzguanglin@gmail.com>
---

This is an implementation of the idea#2 of GSoC 2014 microproject.

 branch.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/branch.c b/branch.c
index 723a36b..4eec0ac 100644
--- a/branch.c
+++ b/branch.c
@@ -49,8 +49,12 @@ static int should_setup_rebase(const char *origin)
 
 void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
 {
-	const char *shortname = remote + 11;
-	int remote_is_branch = starts_with(remote, "refs/heads/");
+	const char *shortname = skip_prefix(remote ,"refs/heads/");
+	int remote_is_branch;
+	if (NULL == shortname)
+		remote_is_branch = 0;
+	else
+		remote_is_branch = 1;
 	struct strbuf key = STRBUF_INIT;
 	int rebasing = should_setup_rebase(origin);
 
-- 
1.9.0

```

## Eric Sunshine, 2014-03-03 08:12

Subject: Re: [PATCH v4] branch.c: change install_branch_config() to use skip_prefix()
Message-ID: <CAPig+cQ7Zg-jPewuaYJ7K8aZBfJQPPt64Pzn9g1GdbikTQORzQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQ7Zg-jPewuaYJ7K8aZBfJQPPt64Pzn9g1GdbikTQORzQ%40mail.gmail.com
In-Reply-To: <1393828581-65523-1-git-send-email-mzguanglin@gmail.com>

```
On Mon, Mar 3, 2014 at 1:36 AM, Guanglin Xu <mzguanglin@gmail.com> wrote:
> to avoid a magic code of 11.
>
> Helped-by: Sun He <sunheeh...@gmail.com>
> Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
> Helped-by: Jacopo Notarstefano <jaco...@gmail.com>
>
> Signed-off-by: Guanglin Xu <mzguanglin@gmail.com>
> ---
>
> This is an implementation of the idea#2 of GSoC 2014 microproject.
>
>  branch.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/branch.c b/branch.c
> index 723a36b..4eec0ac 100644
> --- a/branch.c
> +++ b/branch.c
> @@ -49,8 +49,12 @@ static int should_setup_rebase(const char *origin)
>
>  void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
>  {
> -       const char *shortname = remote + 11;
> -       int remote_is_branch = starts_with(remote, "refs/heads/");
> +       const char *shortname = skip_prefix(remote ,"refs/heads/");
> +       int remote_is_branch;
> +       if (NULL == shortname)
> +               remote_is_branch = 0;
> +       else
> +               remote_is_branch = 1;

A bit verbose. Perhaps just:

    int remote_is_branch = !!shortname;

which you will find to be idiomatic in this project.

>         struct strbuf key = STRBUF_INIT;
>         int rebasing = should_setup_rebase(origin);
>
> --
> 1.9.0

```

## Guanglin Xu, 2014-03-03 08:57

Subject: Re: [PATCH v4] branch.c: change install_branch_config() to use skip_prefix()
Message-ID: <CAATe9ugnq6bm8W05RP=uq+nPEZqsyRYFosRvyJwOSot+ynPOKw@mail.gmail.com>
URL: https://gitlist.dev/e/CAATe9ugnq6bm8W05RP%3Duq%2BnPEZqsyRYFosRvyJwOSot%2BynPOKw%40mail.gmail.com
In-Reply-To: <CAPig+cQ7Zg-jPewuaYJ7K8aZBfJQPPt64Pzn9g1GdbikTQORzQ@mail.gmail.com>

```
Hi Eric,

Yes, you're right. "!!" is comfortably concise and also idiomatic in
Git sources.

Thanks,

Guanglin

2014-03-03 16:12 GMT+08:00 Eric Sunshine <sunshine@sunshineco.com>:
> On Mon, Mar 3, 2014 at 1:36 AM, Guanglin Xu <mzguanglin@gmail.com> wrote:
>> to avoid a magic code of 11.
>>
>> Helped-by: Sun He <sunheeh...@gmail.com>
>> Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
>> Helped-by: Jacopo Notarstefano <jaco...@gmail.com>
>>
>> Signed-off-by: Guanglin Xu <mzguanglin@gmail.com>
>> ---
>>
>> This is an implementation of the idea#2 of GSoC 2014 microproject.
>>
>>  branch.c | 8 ++++++--
>>  1 file changed, 6 insertions(+), 2 deletions(-)
>>
>> diff --git a/branch.c b/branch.c
>> index 723a36b..4eec0ac 100644
>> --- a/branch.c
>> +++ b/branch.c
>> @@ -49,8 +49,12 @@ static int should_setup_rebase(const char *origin)
>>
>>  void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
>>  {
>> -       const char *shortname = remote + 11;
>> -       int remote_is_branch = starts_with(remote, "refs/heads/");
>> +       const char *shortname = skip_prefix(remote ,"refs/heads/");
>> +       int remote_is_branch;
>> +       if (NULL == shortname)
>> +               remote_is_branch = 0;
>> +       else
>> +               remote_is_branch = 1;
>
> A bit verbose. Perhaps just:
>
>     int remote_is_branch = !!shortname;
>
> which you will find to be idiomatic in this project.
>
>>         struct strbuf key = STRBUF_INIT;
>>         int rebasing = should_setup_rebase(origin);
>>
>> --
>> 1.9.0

```
