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

Re: [PATCH 1/5] attr.c: add push_stack() helper

From
Stefan Beller <sbeller@google.com>
Date
Jun 8, 2016, 23:43 UTC
Message-ID
<CAGZ79ka+BXOAMr7JxJXmhsmXTwTcdidJ72=9mbCkHiycj-t6AQ@mail.gmail.com>
In-Reply-To
<20160608225818.726-2-gitster@pobox.com>
On Wed, Jun 8, 2016 at 3:58 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 7 quoted lines
> +static void push_stack(struct attr_stack **attr_stack_p,
> +                      struct attr_stack *elem, char *origin, size_t originlen)
> +{
> +       if (elem) {
> +               elem->origin = origin;
> +               if (origin)
> +                       elem->originlen = originlen;

Why do we need to be conditional on origin for setting the originlen, in all occurrences below, we pass in a reasonable number (0), so I would leave out the condition here.

We make use of the `originlen` in `fill` only, so maybe we can even get rid of the length and when we need it compute it with strlen? (I am unsure on this tradeoff)

Show 8 quoted lines
>
>         if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
>                 elem = read_attr(GITATTRIBUTES_FILE, 1);
> -               elem->origin = xstrdup("");
> -               elem->originlen = 0;
> -               elem->prev = attr_stack;
> -               attr_stack = elem;
> +               push_stack(&attr_stack, elem, xstrdup(""), 0);

I wonder why we need to pass an empty-but-not-null string here? In `fill` we use

  const char *base = stk->origin ? stk->origin : "";
so there it would be the same. In prepare_stack we have
       /*
        * Pop the ones from directories that are not the prefix of
        * the path we are checking. Break out of the loop when we see
        * the root one (whose origin is an empty string "") or the builtin
        * one (whose origin is NULL) without popping it.
        */
        while (attr_stack->origin) {

which seems to answer my question that we make a difference between empty and null `origin`. However I wonder if that could be made more clear? (By a well named bit flag in the attr_stack?)

Show 5 quoted lines
> -                       strbuf_add(&pathbuf, path, cp - path);
> -                       strbuf_addch(&pathbuf, '/');
> -                       strbuf_addstr(&pathbuf, GITATTRIBUTES_FILE);
> +                       strbuf_addf(&pathbuf,
> +                                   "%.*s/%s", (int)(cp - path), path,

This is neat way of handling strings that are not null terminated! I have the suspicion I could have used this before.

As this is meant as a refactoring patch, which doesn't want to change semantics, it looks good to me, the questions are rather meant for followups.

Thanks, Stefan

Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 13 in “A bit more "attributes"”
  1. 0/5 A bit more "attributes"Junio C Hamano, Jun 8, 2016
  2. 1/5 attr.c: add push_stack() helperJunio C Hamano, Jun 8, 2016
  3. Stefan BellerJun 8, 2016
  4. Junio C HamanoJun 9, 2016
  5. 2/5 attr.c: pass struct git_attr_check down the callchainJunio C Hamano, Jun 8, 2016
  6. Stefan BellerJun 8, 2016
  7. 3/5 fixup! d5ad6c13Junio C Hamano, Jun 8, 2016
  8. Stefan BellerJun 9, 2016
  9. Junio C HamanoJun 9, 2016
  10. 4/5 attr.c: correct ugly hack for git_all_attrs()Junio C Hamano, Jun 8, 2016
  11. 5/5 attr.c: always pass check[] to collect_some_attrs()Junio C Hamano, Jun 8, 2016
  12. Stefan BellerJun 9, 2016
  13. Junio C HamanoJun 9, 2016

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.