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
Junio C Hamano <gitster@pobox.com>
Date
May 19, 2026, 01:22 UTC
Message-ID
<xmqqqzn89os3.fsf@gitster.g>
In-Reply-To
<agrD4p1AIPtwa5gW@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 14 quoted lines
>> I do not offhand know if other code paths that are called from this
>> function are thread-safe, but yeah, this use of file-scope static is
>> not a safe thing to do.
>
> In the current status quo this is a safe conversion to do, as we still
> have the assumption ingrained that this only works for a single repo.
> We were using static variables before, and we're still using static
> variables now.
>
> That being said, I wouldn't mind dropping the static variable if this is
> something we'd rather want to get rid of. It's a smell that I'm not
> particularly happy about myself.
>
> I'll send a revised version in a bit, thanks!

OK. In the v2 (perhaps v3?) series, I can see the removal of two static strbuf based "optimizations" is the only change since the previous round (after merging the previous one to the base of the new iteration and comparing the result with the new iteration).

Looking very good.  Shall we mark the topic for 'next' now?
Thanks.
diff --git w/setup.c c/setup.c
index 1847111616..6aee839d8c 100644
--- w/setup.c
+++ c/setup.c
@@ -471,17 +471,26 @@ int is_nonbare_repository_dir(struct strbuf *path)
 
 int is_inside_git_dir(struct repository *repo)
 {
-	static struct strbuf buf = STRBUF_INIT;
-	return is_inside_dir(strbuf_realpath(&buf, repo_get_git_dir(repo), 1));
+	struct strbuf buf = STRBUF_INIT;
+	int ret = is_inside_dir(strbuf_realpath(&buf, repo_get_git_dir(repo), 1));
+	strbuf_release(&buf);
+	return ret;
 }
 
 int is_inside_work_tree(struct repository *repo)
 {
-	static struct strbuf buf = STRBUF_INIT;
-	const char *worktree = repo_get_work_tree(repo);
+	struct strbuf buf = STRBUF_INIT;
+	const char *worktree;
+	int ret;
+
+	worktree = repo_get_work_tree(repo);
 	if (!worktree)
 		return 0;
-	return is_inside_dir(strbuf_realpath(&buf, worktree, 1));
+
+	ret = is_inside_dir(strbuf_realpath(&buf, worktree, 1));
+
+	strbuf_release(&buf);
+	return ret;
 }
 
 void setup_work_tree(struct repository *repo)
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 7 of 73 in “setup: drop uses of `the_repository`”
  1. 00/18 setup: drop uses of `the_repository`Patrick Steinhardt, Apr 20, 2026
  2. 01/18 setup: replace use of `the_repository` in static functionsPatrick Steinhardt, Apr 20, 2026
  3. 02/18 setup: stop using `the_repository` in `is_inside_worktree()`Patrick Steinhardt, Apr 20, 2026
  4. Tian YuchenMay 17, 2026
  5. Junio C HamanoMay 17, 2026
  6. Patrick SteinhardtMay 18, 2026
  7. Junio C HamanoMay 19, 2026
  8. Patrick SteinhardtMay 19, 2026
  9. 03/18 setup: stop using `the_repository` in `is_inside_git_dir()`Patrick Steinhardt, Apr 20, 2026
  10. 04/18 setup: stop using `the_repository` in `prefix_path()`Patrick Steinhardt, Apr 20, 2026
  11. 05/18 setup: stop using `the_repository` in `path_inside_repo()`Patrick Steinhardt, Apr 20, 2026
  12. 06/18 setup: stop using `the_repository` in `verify_filename()`Patrick Steinhardt, Apr 20, 2026
  13. 07/18 setup: stop using `the_repository` in `verify_non_filename()`Patrick Steinhardt, Apr 20, 2026
  14. 08/18 setup: stop using `the_repository` in `enter_repo()`Patrick Steinhardt, Apr 20, 2026
  15. 09/18 setup: stop using `the_repository` in `setup_work_tree()`Patrick Steinhardt, Apr 20, 2026
  16. 10/18 setup: stop using `the_repository` in `set_git_work_tree()`Patrick Steinhardt, Apr 20, 2026
  17. 11/18 setup: stop using `the_repository` in `setup_git_env()`Patrick Steinhardt, Apr 20, 2026
  18. 12/18 setup: stop using `the_repository` in `setup_git_directory_gently()`Patrick Steinhardt, Apr 20, 2026
  19. 13/18 setup: stop using `the_repository` in `setup_git_directory()`Patrick Steinhardt, Apr 20, 2026
  20. 14/18 setup: stop using `the_repository` in `upgrade_repository_format()`Patrick Steinhardt, Apr 20, 2026
  21. 15/18 setup: stop using `the_repository` in `check_repository_format()`Patrick Steinhardt, Apr 20, 2026
  22. 16/18 setup: stop using `the_repository` in `initialize_repository_version()`Patrick Steinhardt, Apr 20, 2026
  23. 17/18 setup: stop using `the_repository` in `create_reference_database()`Patrick Steinhardt, Apr 20, 2026
  24. 18/18 setup: stop using `the_repository` in `init_db()`Patrick Steinhardt, Apr 20, 2026
  25. Elijah NewrenApr 20, 2026
  26. Patrick SteinhardtApr 21, 2026
  27. Junio C HamanoApr 20, 2026
  28. Patrick SteinhardtApr 21, 2026
  29. 00/18 setup: drop uses of `the_repository`Patrick Steinhardt, May 18, 2026
  30. 01/18 setup: replace use of `the_repository` in static functionsPatrick Steinhardt, May 18, 2026
  31. Karthik NayakMay 19, 2026
  32. 02/18 setup: stop using `the_repository` in `is_inside_worktree()`Patrick Steinhardt, May 18, 2026
  33. Karthik NayakMay 19, 2026
  34. Patrick SteinhardtMay 19, 2026
  35. 03/18 setup: stop using `the_repository` in `is_inside_git_dir()`Patrick Steinhardt, May 18, 2026
  36. Karthik NayakMay 19, 2026
  37. 04/18 setup: stop using `the_repository` in `prefix_path()`Patrick Steinhardt, May 18, 2026
  38. 05/18 setup: stop using `the_repository` in `path_inside_repo()`Patrick Steinhardt, May 18, 2026
  39. 06/18 setup: stop using `the_repository` in `verify_filename()`Patrick Steinhardt, May 18, 2026
  40. 07/18 setup: stop using `the_repository` in `verify_non_filename()`Patrick Steinhardt, May 18, 2026
  41. 08/18 setup: stop using `the_repository` in `enter_repo()`Patrick Steinhardt, May 18, 2026
  42. 09/18 setup: stop using `the_repository` in `setup_work_tree()`Patrick Steinhardt, May 18, 2026
  43. 10/18 setup: stop using `the_repository` in `set_git_work_tree()`Patrick Steinhardt, May 18, 2026
  44. 11/18 setup: stop using `the_repository` in `setup_git_env()`Patrick Steinhardt, May 18, 2026
  45. 12/18 setup: stop using `the_repository` in `setup_git_directory_gently()`Patrick Steinhardt, May 18, 2026
  46. 13/18 setup: stop using `the_repository` in `setup_git_directory()`Patrick Steinhardt, May 18, 2026
  47. 14/18 setup: stop using `the_repository` in `upgrade_repository_format()`Patrick Steinhardt, May 18, 2026
  48. 15/18 setup: stop using `the_repository` in `check_repository_format()`Patrick Steinhardt, May 18, 2026
  49. 16/18 setup: stop using `the_repository` in `initialize_repository_version()`Patrick Steinhardt, May 18, 2026
  50. 17/18 setup: stop using `the_repository` in `create_reference_database()`Patrick Steinhardt, May 18, 2026
  51. 18/18 setup: stop using `the_repository` in `init_db()`Patrick Steinhardt, May 18, 2026
  52. Karthik NayakMay 19, 2026
  53. Patrick SteinhardtMay 19, 2026
  54. 00/18 setup: drop uses of `the_repository`Patrick Steinhardt, May 19, 2026
  55. 01/18 setup: replace use of `the_repository` in static functionsPatrick Steinhardt, May 19, 2026
  56. 02/18 setup: stop using `the_repository` in `is_inside_git_dir()`Patrick Steinhardt, May 19, 2026
  57. 03/18 setup: stop using `the_repository` in `is_inside_work_tree()`Patrick Steinhardt, May 19, 2026
  58. 04/18 setup: stop using `the_repository` in `prefix_path()`Patrick Steinhardt, May 19, 2026
  59. 05/18 setup: stop using `the_repository` in `path_inside_repo()`Patrick Steinhardt, May 19, 2026
  60. 06/18 setup: stop using `the_repository` in `verify_filename()`Patrick Steinhardt, May 19, 2026
  61. 07/18 setup: stop using `the_repository` in `verify_non_filename()`Patrick Steinhardt, May 19, 2026
  62. 08/18 setup: stop using `the_repository` in `enter_repo()`Patrick Steinhardt, May 19, 2026
  63. 09/18 setup: stop using `the_repository` in `setup_work_tree()`Patrick Steinhardt, May 19, 2026
  64. 10/18 setup: stop using `the_repository` in `set_git_work_tree()`Patrick Steinhardt, May 19, 2026
  65. 11/18 setup: stop using `the_repository` in `setup_git_env()`Patrick Steinhardt, May 19, 2026
  66. 12/18 setup: stop using `the_repository` in `setup_git_directory_gently()`Patrick Steinhardt, May 19, 2026
  67. 14/18 setup: stop using `the_repository` in `upgrade_repository_format()`Patrick Steinhardt, May 19, 2026
  68. 13/18 setup: stop using `the_repository` in `setup_git_directory()`Patrick Steinhardt, May 19, 2026
  69. 15/18 setup: stop using `the_repository` in `check_repository_format()`Patrick Steinhardt, May 19, 2026
  70. 16/18 setup: stop using `the_repository` in `initialize_repository_version()`Patrick Steinhardt, May 19, 2026
  71. 17/18 setup: stop using `the_repository` in `create_reference_database()`Patrick Steinhardt, May 19, 2026
  72. 18/18 setup: stop using `the_repository` in `init_db()`Patrick Steinhardt, May 19, 2026
  73. Junio C HamanoMay 20, 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.