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

Re: [PATCH v2 1/4] git: pass in repo for RUN_SETUP_GENTLY

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 30, 2024, 19:40 UTC
Message-ID
<xmqqldz953qt.fsf@gitster.g>
In-Reply-To
<5d72c31c6f3b97b7f5f7d3b4fa9a8b1587597670.1727718030.git.gitgitgadget@gmail.com>
"John Cai via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 35 quoted lines
> From: John Cai <johncai86@gmail.com>
>
> commands that have RUN_SETUP_GENTLY potentially need a repository.
> Modify the logic in run_builtin() to pass the repository to the builtin
> if a builtin has the RUN_SETUP_GENTLY property.
>
> Signed-off-by: John Cai <johncai86@gmail.com>
> ---
>  git.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/git.c b/git.c
> index 2fbea24ec92..f58f169f3c7 100644
> --- a/git.c
> +++ b/git.c
> @@ -443,7 +443,7 @@ static int handle_alias(int *argcp, const char ***argv)
>  
>  static int run_builtin(struct cmd_struct *p, int argc, const char **argv, struct repository *repo)
>  {
> -	int status, help;
> +	int status, help, repo_exists;
>  	struct stat st;
>  	const char *prefix;
>  	int run_setup = (p->option & (RUN_SETUP | RUN_SETUP_GENTLY));
> @@ -455,9 +455,13 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv, struct
>  
>  	if (run_setup & RUN_SETUP) {
>  		prefix = setup_git_directory();
> +		repo_exists = 1;
>  	} else if (run_setup & RUN_SETUP_GENTLY) {
>  		int nongit_ok;
>  		prefix = setup_git_directory_gently(&nongit_ok);
> +
> +		if (!nongit_ok)
> +			repo_exists = 1;

Why not use the new variable and pass it directly to nongit_ok? The polarity of the new variable needs to be swapped, of course, but I think it makes reading the code to call p->fn() easier to grok

i.e., 
 - rename repo_exists to "no_repo", and initialize it to non-zero.
 - remove "int nongit_ok"; pass &no_repo instead.
 - update the calling of p->fn() to
	p->fn(argc, argv, prefix, no_repo ? NULL : repo);
Show 12 quoted lines
>  	} else {
>  		prefix = NULL;
>  	}
> @@ -480,7 +484,10 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv, struct
>  	trace2_cmd_name(p->cmd);
>  
>  	validate_cache_entries(repo->index);
> -	status = p->fn(argc, argv, prefix, (p->option & RUN_SETUP)? repo : NULL);
> +	status = p->fn(argc,
> +		       argv,
> +		       prefix,
> +		       repo_exists ? repo : NULL);

Keeping the call to a single line would be much better than spreding it across four lines.

Thanks
>  	validate_cache_entries(repo->index);
>  
>  	if (status)
Previous: John Cai via GitGitGadgetNext: shejialuo
Message 18 of 44 in “Remove the_repository global for am, annotate, apply, archive builtins”
  1. 0/4 Remove the_repository global for am, annotate, apply, archive builtinsJohn Cai via GitGitGadget, Sep 24, 2024
  2. 1/4 git: pass in repo for RUN_SETUP_GENTLYJohn Cai via GitGitGadget, Sep 24, 2024
  3. shejialuoSep 24, 2024
  4. Junio C HamanoSep 24, 2024
  5. Junio C HamanoSep 24, 2024
  6. Patrick SteinhardtSep 26, 2024
  7. Junio C HamanoSep 26, 2024
  8. 2/4 annotate: remove usage of the_repository globalJohn Cai via GitGitGadget, Sep 24, 2024
  9. 3/4 apply: remove the_repository global variableJohn Cai via GitGitGadget, Sep 24, 2024
  10. Junio C HamanoSep 24, 2024
  11. Junio C HamanoSep 24, 2024
  12. John CaiSep 26, 2024
  13. Junio C HamanoSep 26, 2024
  14. 4/4 archive: remove the_repository global variableJohn Cai via GitGitGadget, Sep 24, 2024
  15. Junio C HamanoSep 24, 2024
  16. 0/4 Remove the_repository global for am, annotate, apply, archive builtinsJohn Cai via GitGitGadget, Sep 30, 2024
  17. 1/4 git: pass in repo for RUN_SETUP_GENTLYJohn Cai via GitGitGadget, Sep 30, 2024
  18. Junio C HamanoSep 30, 2024
  19. shejialuoOct 1, 2024
  20. 2/4 annotate: remove usage of the_repository globalJohn Cai via GitGitGadget, Sep 30, 2024
  21. Junio C HamanoSep 30, 2024
  22. 4/4 archive: remove the_repository global variableJohn Cai via GitGitGadget, Sep 30, 2024
  23. Junio C HamanoSep 30, 2024
  24. johncai86@gmail.comOct 4, 2024
  25. 3/4 apply: remove the_repository global variableJohn Cai via GitGitGadget, Sep 30, 2024
  26. Junio C HamanoSep 30, 2024
  27. shejialuoOct 1, 2024
  28. Patrick SteinhardtOct 1, 2024
  29. shejialuoOct 1, 2024
  30. Patrick SteinhardtOct 1, 2024
  31. Junio C HamanoOct 1, 2024
  32. johncai86@gmail.comOct 3, 2024
  33. 0/3 Remove the_repository global for am, annotate, apply, archive builtinsJohn Cai via GitGitGadget, Oct 5, 2024
  34. 1/3 git: pass in repo to builtin based on setup_git_directory_gentlyJohn Cai via GitGitGadget, Oct 5, 2024
  35. shejialuoOct 5, 2024
  36. 2/3 annotate: remove usage of the_repository globalJohn Cai via GitGitGadget, Oct 5, 2024
  37. 3/3 archive: remove the_repository global variableJohn Cai via GitGitGadget, Oct 5, 2024
  38. shejialuoOct 5, 2024
  39. johncai86@gmail.comOct 10, 2024
  40. 0/3 Remove the_repository global for am, annotate, apply, archive builtinsJohn Cai via GitGitGadget, Oct 10, 2024
  41. 1/3 git: pass in repo to builtin based on setup_git_directory_gentlyJohn Cai via GitGitGadget, Oct 10, 2024
  42. 2/3 annotate: remove usage of the_repository globalJohn Cai via GitGitGadget, Oct 10, 2024
  43. 3/3 archive: remove the_repository global variableJohn Cai via GitGitGadget, Oct 10, 2024
  44. Junio C HamanoOct 11, 2024

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.