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

Re: [PATCH 1/3] ls-tree: make <tree-ish> optional

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jul 4, 2018, 09:29 UTC
Message-ID
<CAPig+cQu-e63NUUXAB_QA+M-rgPfqBLOOm5fdjsSVuWHJt7TJA@mail.gmail.com>
In-Reply-To
<26b538bd-df59-d9a6-460d-0b1042b35250@gmail.com>
On Tue, Jul 3, 2018 at 7:15 PM Joshua Nelson <jyn514@gmail.com> wrote:
Show 12 quoted lines
> On 07/03/2018 03:15 AM, Eric Sunshine wrote:
> >> +               /* taken from checkout.c;
> >> +                * we have a simpler case because we never create a branch */
> >
> > However, this comment doesn't belong in the code, as it won't be
> > particularly helpful to anyone reading the code in the future. This
> > sort of note would be more appropriate in the commit message or, even
> > better, in the commentary section just below the "---" lines following
> > your Signed-off-by:.
>
> I wasn't aware I could put comments in email generated by
> git-send-email, thanks for the tip :)

An even more common workflow is to use git-format-patch to generate the patches locally, then edit the patches to add commentary below the "---" line if needed, proof-read everything, and finally use git-send-email to send out the already-generated patches.

Show 17 quoted lines
> >> +                       object = "HEAD";
> >> +               else {
> >> +                       argv++, argc++;
> >> +                       initialized = 1;
> >> +               }
> >> +       }
> >> +
> >> +       if (!initialized) // if we've already run get_oid, don't run it again
> >> +               if (get_oid(object, &oid))
> >> +                       die("Not a valid object name %s", object);
> >
> > Can't you accomplish the same without the 'initialized' variable by
> > instead initializing 'object' to NULL and then checking it here?
>
> I think my code wasn't very clear here - 'initialized' checks if 'oid'
> has been initialized, not 'object'. AFAIK, structs can't be initialized
> to NULL, but my C is not very good so I'd be interested to learn otherwise.

Oh, the intention of 'initialized' was quite clear, but it seems unnecessary, which is why I was suggesting an alternative. Unless I'm misunderstanding the code (certainly a possibility), I think 'initialized' is redundant, thus you can get by without it Specifically, the only time you set 'initialized' is when you don't set 'object', which means that you can infer whether 'oid' was initialized based upon whether 'object' was set. So, my suggestion was:

    const char *object = NULL;
    ...
    ... conditionals possibly assigning to 'object' ...
    ...
    if (object && get_oid(object, &oid))
        die(_("not a valid object name: %s", object);

If you arrived at that final 'if' statement and object is still NULL, then you know that 'oid' is already initialized; if 'object' is not NULL, then you use it to initialized 'oid'.

Previous: Eric Sunshine
Message 21 of 21 in “ls-tree: make <tree-ish> optional”
  1. 1/3 ls-tree: make <tree-ish> optionalJoshua Nelson, Jul 3, 2018
  2. 2/3 ls-tree: update usage infoJoshua Nelson, Jul 3, 2018
  3. Elijah NewrenJul 3, 2018
  4. Eric SunshineJul 3, 2018
  5. 3/3 ls-tree: add unit tests for argumentsJoshua Nelson, Jul 3, 2018
  6. Elijah NewrenJul 3, 2018
  7. Eric SunshineJul 3, 2018
  8. Elijah NewrenJul 3, 2018
  9. Junio C HamanoJul 3, 2018
  10. Elijah NewrenJul 3, 2018
  11. Joshua NelsonJul 3, 2018
  12. Junio C HamanoJul 6, 2018
  13. Joshua NelsonJul 6, 2018
  14. Junio C HamanoJul 6, 2018
  15. Eric SunshineJul 3, 2018
  16. Joshua NelsonJul 3, 2018
  17. ls-tree: make <tree-ish> optionalJoshua Nelson, Jul 3, 2018
  18. Joshua NelsonJul 4, 2018
  19. Eric SunshineJul 4, 2018
  20. Eric SunshineJul 4, 2018
  21. Eric SunshineJul 4, 2018

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.