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

Re: [PATCH] Simplify handling of setup_git_directory_gently() failure cases.

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Dec 14, 2018, 10:32 UTC
Message-ID
<nycvar.QRO.7.76.6.1812141130180.43@tvgsbejvaqbjf.bet>
In-Reply-To
<1544722211-13370-1-git-send-email-eedahlgren@gmail.com>
Hi Erin,
On Thu, 13 Dec 2018, Erin Dahlgren wrote:
Show 37 quoted lines
> setup_git_directory_gently() expects two types of failures to
> discover a git directory (e.g. .git/):
> 
>   - GIT_DIR_HIT_CEILING: could not find a git directory in any
> 	parent directories of the cwd.
>   - GIT_DIR_HIT_MOUNT_POINT: could not find a git directory in
> 	any parent directories up to the mount point of the cwd.
> 
> Both cases are handled in a similar way, but there are misleading
> and unimportant differences. In both cases, setup_git_directory_gently()
> should:
> 
>   - Die if we are not in a git repository. Otherwise:
>   - Set nongit_ok = 1, indicating that we are not in a git repository
> 	but this is ok.
>   - Call strbuf_release() on any non-static struct strbufs that we
> 	allocated.
> 
> Before this change are two misleading additional behaviors:
> 
>   - GIT_DIR_HIT_CEILING: setup_nongit() changes to the cwd for no
> 	apparent reason. We never had the chance to change directories
> 	up to this point so chdir(current cwd) is pointless.
>   - GIT_DIR_HIT_MOUNT_POINT: strbuf_release() frees the buffer
> 	of a static struct strbuf (cwd). This is unnecessary because the
> 	struct is static so its buffer is always reachable. This is also
> 	misleading because nowhere else in the function is this buffer
> 	released.
> 
> This change eliminates these two misleading additional behaviors and
> deletes setup_nogit() because the code is clearer without it. The
> result is that we can see clearly that GIT_DIR_HIT_CEILING and
> GIT_DIR_HIT_MOUNT_POINT lead to the same behavior (ignoring the
> different help messages).
> 
> Thanks-to: Johannes Schindelin <johannes.schindelin@gmx.de>
> Signed-off-by: Erin Dahlgren <eedahlgren@gmail.com>
Thank you for working on this!
> ---
>  setup.c | 34 +++++++++++++---------------------
>  1 file changed, 13 insertions(+), 21 deletions(-)
Nice! It's always good to see a code reduction with such a cleanup.
Just one thing:
Show 47 quoted lines
> diff --git a/setup.c b/setup.c
> index 1be5037..b441e39 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -831,16 +831,6 @@ static const char *setup_bare_git_dir(struct strbuf *cwd, int offset,
>  	return NULL;
>  }
>  
> -static const char *setup_nongit(const char *cwd, int *nongit_ok)
> -{
> -	if (!nongit_ok)
> -		die(_("not a git repository (or any of the parent directories): %s"), DEFAULT_GIT_DIR_ENVIRONMENT);
> -	if (chdir(cwd))
> -		die_errno(_("cannot come back to cwd"));
> -	*nongit_ok = 1;
> -	return NULL;
> -}
> -
>  static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len)
>  {
>  	struct stat buf;
> @@ -1097,18 +1087,20 @@ const char *setup_git_directory_gently(int *nongit_ok)
>  		prefix = setup_bare_git_dir(&cwd, dir.len, &repo_fmt, nongit_ok);
>  		break;
>  	case GIT_DIR_HIT_CEILING:
> -		prefix = setup_nongit(cwd.buf, nongit_ok);
> -		break;
> +		if (!nongit_ok)
> +			die(_("not a git repository (or any of the parent directories): %s"),
> +					DEFAULT_GIT_DIR_ENVIRONMENT);
> +		*nongit_ok = 1;
> +		strbuf_release(&dir);
> +		return NULL;
>  	case GIT_DIR_HIT_MOUNT_POINT:
> -		if (nongit_ok) {
> -			*nongit_ok = 1;
> -			strbuf_release(&cwd);
> -			strbuf_release(&dir);
> -			return NULL;
> -		}
> -		die(_("not a git repository (or any parent up to mount point %s)\n"
> -		      "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."),
> -		    dir.buf);
> +		if (!nongit_ok)
> +			die(_("not a git repository (or any parent up to mount point %s)\n"
> +						"Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."),
> +					dir.buf);

This indentation would probably be better aligned with the first double-quote on the `die` line.

Otherwise: ACK!

Thank you, Johannes

Show 10 quoted lines
> +		*nongit_ok = 1;
> +		strbuf_release(&dir);
> +		return NULL;
>  	default:
>  		BUG("unhandled setup_git_directory_1() result");
>  	}
> -- 
> 2.7.4
> 
> 
Previous: Erin DahlgrenNext: Erin Dahlgren
Message 2 of 19 in “Simplify handling of setup_git_directory_gently() failure cases.”
  1. Simplify handling of setup_git_directory_gently() failure cases.Erin Dahlgren, Dec 13, 2018
  2. Johannes SchindelinDec 14, 2018
  3. Erin DahlgrenDec 16, 2018
  4. Simplify handling of setup_git_directory_gently() failure cases.Erin Dahlgren, Dec 16, 2018
  5. Johannes SchindelinDec 18, 2018
  6. Erin DahlgrenDec 18, 2018
  7. Jeff KingDec 18, 2018
  8. Erin DahlgrenDec 18, 2018
  9. Jeff KingDec 19, 2018
  10. Junio C HamanoDec 26, 2018
  11. Jeff KingDec 27, 2018
  12. Erin DahlgrenDec 27, 2018
  13. Jeff KingJan 3, 2019
  14. Simplify handling of setup_git_directory_gently() failure cases.Erin Dahlgren, Dec 27, 2018
  15. Jeff KingJan 3, 2019
  16. Junio C HamanoJan 3, 2019
  17. Jeff KingJan 4, 2019
  18. Erin DahlgrenJan 5, 2019
  19. Jeff KingJan 6, 2019

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.