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

Re: [PATCH 02/18] setup: stop using `the_repository` in `is_inside_worktree()`

From
Karthik Nayak <karthik.188@gmail.com>
Date
Apr 9, 2026, 11:55 UTC
Message-ID
<CAOLa=ZT=OZ3ymxef=mRo_EJeNvisJoR9o=T9Ab59acw9ui2LXg@mail.gmail.com>
In-Reply-To
<20260330-pks-setup-wo-the-repository-v1-2-0d2e822837aa@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 22 quoted lines
> The function `is_inside_worktree()` verifies whether or not the current
> working directory is located inside the worktree of `the_repository`.
> This is done by taking the worktree path and verifying that it's a
> prefix of the current working directory.
>
> This information is cached so that we don't have to re-do this change
> multiple times. Furthermore, we proactively set the value in multiple
> locations so that we don't even have to perform the check when we have
> discovered the repository.
>
> While we could simply move the caching variable into the repository, the
> current layout doesn't really feel sensible in the first place:
>
>   - It can easily lead to false positives or negatives if at any point
>     in time we may switch the current working directory.
>
>   - We don't call the function in a hot loop, and neither is it overly
>     expensive to compute.
>
> Drop the caching infrastructure and instead compute the property ad-hoc
> via an injected repository.
>

I was thinking we could cache the variable as 'static' within the function, but that too would have the staleness issue and require invalidation. So this looks sensible.

Show 7 quoted lines
> Note that there is one small gotcha: we sometimes may end up with
> relative directory paths, and if so `is_inside_dir()` might fail. This
> wasn't an issue before because of how we proactively set the cached
> value during repository discovery. Now that we stop doing that it
> becomes a problem though, but it is worked around by resolving the
> repository directory via `realpath()`.
>
Ah!
[snip]
Show 12 quoted lines
> -int is_inside_work_tree(void)
> +int is_inside_work_tree(struct repository *repo)
>  {
> -	if (inside_work_tree < 0)
> -		inside_work_tree = is_inside_dir(repo_get_work_tree(the_repository));
> -	return inside_work_tree;
> +	static struct strbuf buf = STRBUF_INIT;
> +	const char *worktree = repo_get_work_tree(repo);
> +	if (!worktree)
> +		return 0;
> +	return is_inside_dir(strbuf_realpath(&buf, worktree, 1));
>  }
We statically allocate the buf to avoid reallocation. Okay.
[snip]
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 4 of 31 in “setup: drop uses of `the_repository`”
  1. 00/18 setup: drop uses of `the_repository`Patrick Steinhardt, Mar 30, 2026
  2. 01/18 setup: replace use of `the_repository` in static functionsPatrick Steinhardt, Mar 30, 2026
  3. 02/18 setup: stop using `the_repository` in `is_inside_worktree()`Patrick Steinhardt, Mar 30, 2026
  4. Karthik NayakApr 9, 2026
  5. 03/18 setup: stop using `the_repository` in `is_inside_git_dir()`Patrick Steinhardt, Mar 30, 2026
  6. Karthik NayakApr 9, 2026
  7. Patrick SteinhardtApr 13, 2026
  8. Junio C HamanoApr 13, 2026
  9. 04/18 setup: stop using `the_repository` in `prefix_path()`Patrick Steinhardt, Mar 30, 2026
  10. 05/18 setup: stop using `the_repository` in `path_inside_repo()`Patrick Steinhardt, Mar 30, 2026
  11. 06/18 setup: stop using `the_repository` in `verify_filename()`Patrick Steinhardt, Mar 30, 2026
  12. 07/18 setup: stop using `the_repository` in `verify_non_filename()`Patrick Steinhardt, Mar 30, 2026
  13. 08/18 setup: stop using `the_repository` in `enter_repo()`Patrick Steinhardt, Mar 30, 2026
  14. 09/18 setup: stop using `the_repository` in `setup_work_tree()`Patrick Steinhardt, Mar 30, 2026
  15. Elijah NewrenApr 18, 2026
  16. Patrick SteinhardtApr 20, 2026
  17. 10/18 setup: stop using `the_repository` in `set_git_work_tree()`Patrick Steinhardt, Mar 30, 2026
  18. Elijah NewrenApr 18, 2026
  19. Patrick SteinhardtApr 20, 2026
  20. 11/18 setup: stop using `the_repository` in `setup_git_env()`Patrick Steinhardt, Mar 30, 2026
  21. 12/18 setup: stop using `the_repository` in `setup_git_directory_gently()`Patrick Steinhardt, Mar 30, 2026
  22. 13/18 setup: stop using `the_repository` in `setup_git_directory()`Patrick Steinhardt, Mar 30, 2026
  23. 14/18 setup: stop using `the_repository` in `upgrade_repository_format()`Patrick Steinhardt, Mar 30, 2026
  24. 15/18 setup: stop using `the_repository` in `check_repository_format()`Patrick Steinhardt, Mar 30, 2026
  25. Elijah NewrenApr 18, 2026
  26. Patrick SteinhardtApr 20, 2026
  27. 16/18 setup: stop using `the_repository` in `initialize_repository_version()`Patrick Steinhardt, Mar 30, 2026
  28. 17/18 setup: stop using `the_repository` in `create_reference_database()`Patrick Steinhardt, Mar 30, 2026
  29. 18/18 setup: stop using `the_repository` in `init_db()`Patrick Steinhardt, Mar 30, 2026
  30. Karthik NayakApr 9, 2026
  31. Patrick SteinhardtApr 13, 2026

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.