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

Re: [PATCH 2/6] commit-graph.h: store object directory in 'struct commit_graph'

From
Taylor Blau <me@ttaylorr.com>
Date
Feb 3, 2020, 03:58 UTC
Message-ID
<20200203035819.GA23625@syl.local>
In-Reply-To
<CAN0heSrrrWjBKnzjMfzEkTMVTge2AfVdwsp6D5Mx==5S8-ZLJQ@mail.gmail.com>
Hi Martin,

Thanks for your review! Your comments were all quite helpful, and I applied all of your suggested changes.

On Fri, Jan 31, 2020 at 07:52:02AM +0100, Martin Ågren wrote:
Show 5 quoted lines
> On Fri, 31 Jan 2020 at 00:03, Taylor Blau <me@ttaylorr.com> wrote:
> > Instead of getting rid of the 'struct object_directory *', store that
> > insead of a 'char *odb' in 'struct commit_graph'. Once the 'struct
>
> s/insead/instead/
Typo. Thanks for noticing. I fixed this in my local copy of this branch.
Show 18 quoted lines
> >         if (open_ok)
> >                 graph = load_commit_graph_one_fd_st(fd, &st);
> > -        else
> > -               graph = read_commit_graph_one(the_repository, opts.obj_dir);
> > +       else {
> > +               struct object_directory *odb;
> > +               if ((odb = find_odb(the_repository, opts.obj_dir)))
> > +                       graph = read_commit_graph_one(the_repository, odb);
> > +       }
>
> I'm a tiny bit allergic to this assignment-within-if. It's wrapped by
> another pair of parentheses, which both compilers and humans know to
> interpret as "trust me, this is not a mistake", but I still find this
> easier to read:
>
>   odb = find_odb(...);
>   if (odb)
>           ....

To be honest, I'm not such a fan of this style myself, but it seemed odd to me to write:

  struct object_directory *odb;
  odb = ...;
  if (odb) {
  }

when we were really only trying to call 'find_odb()' and do something with its result, but only if it was non-NULL. I counted 152 of these assign-if's laying around with:

  $ git grep 'if ((.* =[^=]' | wc -l

but it seems like they are in poor style (as evidenced by your and Junio's response later in the thread). So, I removed this and instead promoted 'odb' to a local variable at the function level, since we do that promotion anyway in a couple of patches later. This reduces the churn, and avoids either an assign-if, or a define/assign/check.

> > --- a/builtin/commit.c
> > +++ b/builtin/commit.c
>
> > +#include "object-store.h"

No; this is a stray left over from some development on this branch. I'll remove it.

Show 11 quoted lines
> This is the only change in this file, which looks a bit odd. I haven't
> actually applied your patches, to be honest, but is this inclusion
> really needed?
>
> > --- a/commit-graph.c
> > +++ b/commit-graph.c
>
> > +struct object_directory *find_odb(struct repository *r, const char *obj_dir)
>
> This doesn't look commit-graph related -- could/should it go somewhere
> else?

I'll respond in more complete detail further down in the thread, but the short answer is "yes, this should go in builtin/commit-graph.c".

Show 30 quoted lines
> > +{
> > +       struct object_directory *odb;
> > +       char *obj_dir_real = real_pathdup(obj_dir, 1);
> > +       int cmp = -1;
> > +
> > +       prepare_alt_odb(r);
> > +       for (odb = r->objects->odb; odb; odb = odb->next) {
> > +               cmp = strcmp(obj_dir_real, real_path(odb->path));
> > +               if (!cmp)
> > +                       break;
> > +       }
>
> At this point, either odb is NULL or cmp is zero. Those are the only two
> ways out of the loop.
>
> > +       free(obj_dir_real);
> > +
> > +       if (cmp)
> > +               odb = NULL;
>
> Meaning that this doesn't do much? If the most recent comparison failed,
> it's because we didn't find anything, so odb will be NULL.
>
> > +       return odb;
> > +}
>
> I think you could drop `cmp` and that final check, and write the loop
> body as "if (!strcmp(...)) break". You could also have an empty loop
> body, but I wouldn't go there -- I'd find that less readable. (Maybe
> that's just me.)

Thanks, I changed this to remove the 'cmp' check outside of the loop, which I agree is unnecessary.

> Martin

Thanks, Taylor

Previous: Junio C HamanoNext: Taylor Blau
Message 12 of 28 in “commit-graph: use 'struct object_directory *' everywhere”
  1. 0/6 commit-graph: use 'struct object_directory *' everywhereTaylor Blau, Jan 30, 2020
  2. 1/6 t5318: don't pass non-object directory to '--object-dir'Taylor Blau, Jan 30, 2020
  3. 5/6 commit-graph.c: remove path normalization, comparisonTaylor Blau, Jan 30, 2020
  4. 6/6 commit-graph.h: use odb in 'load_commit_graph_one_fd_st'Taylor Blau, Jan 30, 2020
  5. 2/6 commit-graph.h: store object directory in 'struct commit_graph'Taylor Blau, Jan 30, 2020
  6. Martin ÅgrenJan 31, 2020
  7. Jeff KingJan 31, 2020
  8. Martin ÅgrenJan 31, 2020
  9. Taylor BlauFeb 3, 2020
  10. Jeff KingFeb 3, 2020
  11. Junio C HamanoJan 31, 2020
  12. Taylor BlauFeb 3, 2020
  13. 4/6 commit-graph.h: store an odb in 'struct write_commit_graph_context'Taylor Blau, Jan 30, 2020
  14. 3/6 builtin/commit-graph.c: die() with unknown '--object-dir'Taylor Blau, Jan 30, 2020
  15. Jeff KingJan 31, 2020
  16. Derrick StoleeJan 31, 2020
  17. Taylor BlauFeb 3, 2020
  18. 0/5 commit-graph: use 'struct object_directory *' everywhereTaylor Blau, Feb 3, 2020
  19. 1/5 t5318: don't pass non-object directory to '--object-dir'Taylor Blau, Feb 3, 2020
  20. 2/5 commit-graph.h: store an odb in 'struct write_commit_graph_context'Taylor Blau, Feb 3, 2020
  21. 2/5 commit-graph.h: store an odb in 'struct write_commit_graph_context'Taylor Blau, Feb 4, 2020
  22. Junio C HamanoFeb 4, 2020
  23. Taylor BlauFeb 4, 2020
  24. Junio C HamanoFeb 4, 2020
  25. 5/5 commit-graph.h: use odb in 'load_commit_graph_one_fd_st'Taylor Blau, Feb 3, 2020
  26. 4/5 commit-graph.c: remove path normalization, comparisonTaylor Blau, Feb 3, 2020
  27. 3/5 commit-graph.h: store object directory in 'struct commit_graph'Taylor Blau, Feb 3, 2020
  28. Jeff KingFeb 5, 2020

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.