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

Re: [PATCH v3 6/6] unpack-trees.[ch]: define and use a UNPACK_TREES_OPTIONS_INIT

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 1, 2021, 21:39 UTC
Message-ID
<xmqqk0iw4e7v.fsf@gitster.g>
In-Reply-To
<patch-v3-6.6-18358f5d57a-20211001T102056Z-avarab@gmail.com>
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:
Show 21 quoted lines
> diff --git a/archive.c b/archive.c
> index a3bbb091256..210d7235c5a 100644
> --- a/archive.c
> +++ b/archive.c
> @@ -269,7 +269,7 @@ int write_archive_entries(struct archiver_args *args,
>  		write_archive_entry_fn_t write_entry)
>  {
>  	struct archiver_context context;
> -	struct unpack_trees_options opts;
> +	struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT;
>  	struct tree_desc t;
>  	int err;
>  	struct strbuf path_in_archive = STRBUF_INIT;
> @@ -300,7 +300,6 @@ int write_archive_entries(struct archiver_args *args,
>  	 * Setup index and instruct attr to read index only
>  	 */
>  	if (!args->worktree_attributes) {
> -		memset(&opts, 0, sizeof(opts));
>  		opts.index_only = 1;
>  		opts.head_idx = -1;
>  		opts.src_index = args->repo->index;

These two hunks almost makes me say "Meh" (I am adding this parenthetical comment after reading the patch thru and I am not yet convinced otherwise).

The reason why we had memset(0), getting rid of which I have no problem about, is because we didn't prepare the members of the structure using initializer in the first place, no? The "opts" is used only in this block, so instead of these lines, we could have

	if (!args->worktree_attributes) {
		struct unpack_trees_options opts = {
			.index_only = 1,
                        .head_idx = -1,
		        ...
			.fn = oneway_merge;
		};
		init_tree_desc(...);
		if (unpack_trees(1, &t, &opts))
			...

which would be much easier to understand. We can get rid of memset(0), having UNPACK_TREES_OPTIONS_INIT did not help us much.

Some hunks show that we have code between the location where "opts" is declared and where we know for certain that "opts" needs to be actually used and with what values in its members, like ...

Show 21 quoted lines
> diff --git a/builtin/am.c b/builtin/am.c
> index e4a0ff9cd7c..82641ce68ec 100644
> --- a/builtin/am.c
> +++ b/builtin/am.c
> @@ -1901,7 +1901,7 @@ static void am_resolve(struct am_state *state)
>  static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
>  {
>  	struct lock_file lock_file = LOCK_INIT;
> -	struct unpack_trees_options opts;
> +	struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT;
>  	struct tree_desc t[2];
>  
>  	if (parse_tree(head) || parse_tree(remote))
> ...
>  
>  	refresh_cache(REFRESH_QUIET);
>  
> -	memset(&opts, 0, sizeof(opts));
>  	opts.head_idx = 1;
>  	opts.src_index = &the_index;
>  	opts.dst_index = &the_index;

... this one. And it is not necessarily a good idea to initialize "opts" with the actual values we'd use, and _INIT does help us getting rid of memset(0);

But many in this patch are much simpler like this one:
Show 16 quoted lines
> diff --git a/builtin/checkout.c b/builtin/checkout.c
> index 8c69dcdf72a..fea4533dbec 100644
> --- a/builtin/checkout.c
> +++ b/builtin/checkout.c
> @@ -639,10 +639,9 @@ static int reset_tree(struct tree *tree, const struct checkout_opts *o,
>  		      int worktree, int *writeout_error,
>  		      struct branch_info *info)
>  {
> -	struct unpack_trees_options opts;
> +	struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT;
>  	struct tree_desc tree_desc;
>  
> -	memset(&opts, 0, sizeof(opts));
>  	opts.head_idx = -1;
>  	opts.update = worktree;
>  	opts.skip_unmerged = !worktree;

which can be initialized with designated initializers in place, and having an _INIT macro would not help us very much.

Show 11 quoted lines
> @@ -719,9 +718,8 @@ static int merge_working_tree(const struct checkout_opts *opts,
>  	} else {
>  		struct tree_desc trees[2];
>  		struct tree *tree;
> -		struct unpack_trees_options topts;
> +		struct unpack_trees_options topts = UNPACK_TREES_OPTIONS_INIT;
>  
> -		memset(&topts, 0, sizeof(topts));
>  		topts.head_idx = -1;
>  		topts.src_index = &the_index;
>  		topts.dst_index = &the_index;
Likewise here.
So...
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 27 of 33 in “Non-trivial designated initializer conversion”
  1. 0/6 Non-trivial designated initializer conversionÆvar Arnfjörð Bjarmason, Sep 27, 2021
  2. 1/6 daemon.c: refactor hostinfo_init() to HOSTINFO_INIT macroÆvar Arnfjörð Bjarmason, Sep 27, 2021
  3. 2/6 builtin/blame.c: refactor commit_info_init() to COMMIT_INFO_INIT macroÆvar Arnfjörð Bjarmason, Sep 27, 2021
  4. 3/6 shortlog: use designated initializer for "struct shortlog"Ævar Arnfjörð Bjarmason, Sep 27, 2021
  5. Phillip WoodSep 27, 2021
  6. Ævar Arnfjörð BjarmasonSep 27, 2021
  7. 4/6 urlmatch.[ch]: add and use URLMATCH_CONFIG_INITÆvar Arnfjörð Bjarmason, Sep 27, 2021
  8. 5/6 builtin/remote.c: add and use a REF_STATES_INITÆvar Arnfjörð Bjarmason, Sep 27, 2021
  9. 6/6 builtin/remote.c: add and use SHOW_INFO_INITÆvar Arnfjörð Bjarmason, Sep 27, 2021
  10. 0/5 Non-trivial designated initializer conversionÆvar Arnfjörð Bjarmason, Sep 27, 2021
  11. 1/5 daemon.c: refactor hostinfo_init() to HOSTINFO_INIT macroÆvar Arnfjörð Bjarmason, Sep 27, 2021
  12. 2/5 builtin/blame.c: refactor commit_info_init() to COMMIT_INFO_INIT macroÆvar Arnfjörð Bjarmason, Sep 27, 2021
  13. 3/5 urlmatch.[ch]: add and use URLMATCH_CONFIG_INITÆvar Arnfjörð Bjarmason, Sep 27, 2021
  14. Junio C HamanoSep 27, 2021
  15. 4/5 builtin/remote.c: add and use a REF_STATES_INITÆvar Arnfjörð Bjarmason, Sep 27, 2021
  16. Junio C HamanoSep 27, 2021
  17. Ævar Arnfjörð BjarmasonSep 27, 2021
  18. Junio C HamanoSep 27, 2021
  19. 5/5 builtin/remote.c: add and use SHOW_INFO_INITÆvar Arnfjörð Bjarmason, Sep 27, 2021
  20. 0/6 Non-trivial designated initializer conversionÆvar Arnfjörð Bjarmason, Oct 1, 2021
  21. 1/6 daemon.c: refactor hostinfo_init() to HOSTINFO_INIT macroÆvar Arnfjörð Bjarmason, Oct 1, 2021
  22. 2/6 builtin/blame.c: refactor commit_info_init() to COMMIT_INFO_INIT macroÆvar Arnfjörð Bjarmason, Oct 1, 2021
  23. 3/6 urlmatch.[ch]: add and use URLMATCH_CONFIG_INITÆvar Arnfjörð Bjarmason, Oct 1, 2021
  24. 4/6 builtin/remote.c: add and use a REF_STATES_INITÆvar Arnfjörð Bjarmason, Oct 1, 2021
  25. 5/6 builtin/remote.c: add and use SHOW_INFO_INITÆvar Arnfjörð Bjarmason, Oct 1, 2021
  26. 6/6 unpack-trees.[ch]: define and use a UNPACK_TREES_OPTIONS_INITÆvar Arnfjörð Bjarmason, Oct 1, 2021
  27. Junio C HamanoOct 1, 2021
  28. 0/5 Non-trivial designated initializer conversionÆvar Arnfjörð Bjarmason, Oct 2, 2021
  29. 2/5 builtin/blame.c: refactor commit_info_init() to COMMIT_INFO_INIT macroÆvar Arnfjörð Bjarmason, Oct 2, 2021
  30. 1/5 daemon.c: refactor hostinfo_init() to HOSTINFO_INIT macroÆvar Arnfjörð Bjarmason, Oct 2, 2021
  31. 4/5 builtin/remote.c: add and use a REF_STATES_INITÆvar Arnfjörð Bjarmason, Oct 2, 2021
  32. 5/5 builtin/remote.c: add and use SHOW_INFO_INITÆvar Arnfjörð Bjarmason, Oct 2, 2021
  33. 3/5 urlmatch.[ch]: add and use URLMATCH_CONFIG_INITÆvar Arnfjörð Bjarmason, Oct 2, 2021

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.