git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 04/10] branch: roll show_detached HEAD into regular ref_list

From
Karthik Nayak <karthik.188@gmail.com>
Date
Aug 11, 2015, 13:17 UTC
Message-ID
<CAOLa=ZRVr9a8kj+MLbi7ybYCHJVdHrX42vzMRU8PS6nvHmB_og@mail.gmail.com>
In-Reply-To
<CAPig+cRvdpv_NO8jCrWmFfpuQ+mcxaC5P_oHVrU+tQoH9DKnkg@mail.gmail.com>
On Tue, Aug 11, 2015 at 8:11 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
Show 66 quoted lines
> On Tue, Aug 4, 2015 at 9:01 AM, Karthik Nayak <karthik.188@gmail.com> wrote:
>> Remove show_detached() and make detached HEAD to be rolled into
>> regular ref_list by adding REF_DETACHED_HEAD as a kind of branch and
>> supporting the same in append_ref(). This eliminates the need for an
>> extra function and helps in easier porting of branch.c to use
>> ref-filter APIs.
>>
>> Before show_detached() used to check if the HEAD branch satisfies the
>> '--contains' option, now that is taken care by append_ref().
>>
>> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
>> ---
>> diff --git a/builtin/branch.c b/builtin/branch.c
>> index 65f6d0d..81815c9 100644
>> --- a/builtin/branch.c
>> +++ b/builtin/branch.c
>> @@ -535,6 +540,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
>>         int color;
>>         struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
>>         const char *prefix = "";
>> +       const char *desc = item->name;
>>
>>         if (item->ignore)
>>                 return;
>> @@ -547,6 +553,10 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
>>                 color = BRANCH_COLOR_REMOTE;
>>                 prefix = remote_prefix;
>>                 break;
>> +       case REF_DETACHED_HEAD:
>> +               color = BRANCH_COLOR_CURRENT;
>> +               desc = get_head_description();
>> +               break;
>>         default:
>>                 color = BRANCH_COLOR_PLAIN;
>>                 break;
>> @@ -558,7 +568,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
>>                 color = BRANCH_COLOR_CURRENT;
>>         }
>>
>> -       strbuf_addf(&name, "%s%s", prefix, item->name);
>> +       strbuf_addf(&name, "%s%s", prefix, desc);
>>         if (verbose) {
>>                 int utf8_compensation = strlen(name.buf) - utf8_strwidth(name.buf);
>>                 strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
>> @@ -581,6 +591,8 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
>>         }
>>         strbuf_release(&name);
>>         strbuf_release(&out);
>> +       if (item->kind == REF_DETACHED_HEAD)
>> +               free((void *)desc);
>
> This would be cleaner, and more easily extended to other cases if you
> used a 'to_free' variable. For instance, something like this:
>
>     const char *desc = item->name;
>     char *to_free = NULL;
>     ...
>     case REF_DETACHED_HEAD:
>         ...
>         desc = to_free = get_head_description();
>         break;
>     ...
>     strbuf_addf(&name, "%s%s", prefix, desc);
>     ...
>     free(to_free);
>
This looks neater!
Show 5 quoted lines
> Note that it's safe to free 'to_free' when it's NULL, so you don't
> need to protect the free() with that ugly specialized 'if' which
> checks for REF_DETACHED_HEAD. You can also do away with the "cast to
> non-const" when freeing.
>
Yea makes sense will change to what you suggested.
Show 10 quoted lines
>>  }
>> @@ -642,7 +638,14 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
>>         cb.ref_list = &ref_list;
>>         cb.pattern = pattern;
>>         cb.ret = 0;
>> +       /*
>> +        * First we obtain all regular branch refs and then if the
>
> s/then//
>
Thanks
-- 
Regards,
Karthik Nayak
Previous: Eric SunshineNext: Karthik Nayak
Message 19 of 27 in “Port branch.c to ref-filter.”
  1. 0/10 Port branch.c to ref-filter.Karthik Nayak, Aug 4, 2015
  2. 01/10 ref-filter: add option to filter only branchesKarthik Nayak, Aug 4, 2015
  3. Junio C HamanoAug 11, 2015
  4. Karthik NayakAug 13, 2015
  5. Karthik NayakAug 13, 2015
  6. Karthik NayakAug 13, 2015
  7. Junio C HamanoAug 14, 2015
  8. Karthik NayakAug 14, 2015
  9. Matthieu MoyAug 13, 2015
  10. Karthik NayakAug 13, 2015
  11. 02/10 branch: refactor width computationKarthik Nayak, Aug 4, 2015
  12. Eric SunshineAug 11, 2015
  13. Karthik NayakAug 11, 2015
  14. 03/10 branch: bump get_head_description() to the topKarthik Nayak, Aug 4, 2015
  15. Eric SunshineAug 11, 2015
  16. Karthik NayakAug 11, 2015
  17. 04/10 branch: roll show_detached HEAD into regular ref_listKarthik Nayak, Aug 4, 2015
  18. Eric SunshineAug 11, 2015
  19. Karthik NayakAug 11, 2015
  20. 05/10 branch: move 'current' check down to the presentation layerKarthik Nayak, Aug 4, 2015
  21. 06/10 branch: drop non-commit error reportingKarthik Nayak, Aug 4, 2015
  22. 07/10 branch.c: use 'ref-filter' data structuresKarthik Nayak, Aug 4, 2015
  23. 08/10 branch.c: use 'ref-filter' APIsKarthik Nayak, Aug 4, 2015
  24. 09/10 branch: add '--points-at' optionKarthik Nayak, Aug 4, 2015
  25. Karthik NayakAug 4, 2015
  26. Junio C HamanoAug 5, 2015
  27. Karthik NayakAug 7, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.