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

[PATCH 2/2] path: remove repository argument from worktree_git_path()

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Feb 16, 2026, 16:18 UTC
Message-ID
<23b8a355b414da2b6216a50006bf2276dd3ea6ae.1771258688.git.phillip.wood@dunelm.org.uk>
In-Reply-To
<cover.1771258688.git.phillip.wood@dunelm.org.uk>
From: Phillip Wood <phillip.wood@dunelm.org.uk>

worktree_git_path() takes a struct repository and a struct worktree which also contains a struct repository. The repository argument was added by a973f60dc7c (path: stop relying on `the_repository` in `worktree_git_path()`, 2024-08-13) and exists because the worktree argument is optional. Having two ways of passing a repository is a potential foot-gun as if the the worktree argument is present the repository argument must match the worktree's repository member. Since the last commit there are no callers that pass a NULL worktree so lets remove the repository argument. This removes the potential confusion and lets us delete a number of uses of "the_repository".

worktree_git_path() has the following callers:
 - builtin/worktree.c:validate_no_submodules() which is called from
   check_clean_worktree() and move_worktree(), both of which supply
   a non-NULL worktree.
 - builtin/fsck.c:cmd_fsck() which loops over all worktrees.
 - revision.c:add_index_objects_to_pending() which loops over all
   worktrees.
 - worktree.c:worktree_lock_reason() which dereferences wt before
   calling worktree_git_path().
 - wt-status.c:wt_status_check_bisect() and wt_status_check_rebase()
   which are always called with a non-NULL worktree after the last
   commit.
 - wt-status.c:git_branch() which is only called by
   wt_status_check_bisect() and wt_status_check_rebase().
Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
 builtin/fsck.c     |  2 +-
 builtin/worktree.c |  4 ++--
 path.c             |  9 ++++-----
 path.h             |  8 +++-----
 revision.c         |  2 +-
 worktree.c         |  2 +-
 wt-status.c        | 14 +++++++-------
 7 files changed, 19 insertions(+), 22 deletions(-)
diff --git a/builtin/fsck.c b/builtin/fsck.c
index 0512f78a87f..42ba0afb91a 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -1137,7 +1137,7 @@ int cmd_fsck(int argc,
 			 * and may get overwritten by other calls
 			 * while we're examining the index.
 			 */
-			path = xstrdup(worktree_git_path(the_repository, wt, "index"));
+			path = xstrdup(worktree_git_path(wt, "index"));
 			wt_gitdir = get_worktree_git_dir(wt);
 
 			read_index_from(&istate, path, wt_gitdir);
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 3d6547c23b4..62fd4642e5d 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -1191,14 +1191,14 @@ static void validate_no_submodules(const struct worktree *wt)
 
 	wt_gitdir = get_worktree_git_dir(wt);
 
-	if (is_directory(worktree_git_path(the_repository, wt, "modules"))) {
+	if (is_directory(worktree_git_path(wt, "modules"))) {
 		/*
 		 * There could be false positives, e.g. the "modules"
 		 * directory exists but is empty. But it's a rare case and
 		 * this simpler check is probably good enough for now.
 		 */
 		found_submodules = 1;
-	} else if (read_index_from(&istate, worktree_git_path(the_repository, wt, "index"),
+	} else if (read_index_from(&istate, worktree_git_path(wt, "index"),
 				   wt_gitdir) > 0) {
 		for (i = 0; i < istate.cache_nr; i++) {
 			struct cache_entry *ce = istate.cache[i];
diff --git a/path.c b/path.c
index d726537622c..073f631b914 100644
--- a/path.c
+++ b/path.c
@@ -486,17 +486,16 @@ const char *mkpath(const char *fmt, ...)
 	return cleanup_path(pathname->buf);
 }
 
-const char *worktree_git_path(struct repository *r,
-			      const struct worktree *wt, const char *fmt, ...)
+const char *worktree_git_path(const struct worktree *wt, const char *fmt, ...)
 {
 	struct strbuf *pathname = get_pathname();
 	va_list args;
 
-	if (wt && wt->repo != r)
-		BUG("worktree not connected to expected repository");
+	if (!wt)
+		BUG("%s() called with NULL worktree", __func__);
 
 	va_start(args, fmt);
-	repo_git_pathv(r, wt, pathname, fmt, args);
+	repo_git_pathv(wt->repo, wt, pathname, fmt, args);
 	va_end(args);
 	return pathname->buf;
 }
diff --git a/path.h b/path.h
index 0ec95a0b079..cbcad254a0a 100644
--- a/path.h
+++ b/path.h
@@ -66,13 +66,11 @@ const char *repo_git_path_replace(struct repository *repo,
 
 /*
  * Similar to repo_git_path() but can produce paths for a specified
- * worktree instead of current one. When no worktree is given, then the path is
- * computed relative to main worktree of the given repository.
+ * worktree instead of current one.
  */
-const char *worktree_git_path(struct repository *r,
-			      const struct worktree *wt,
+const char *worktree_git_path(const struct worktree *wt,
 			      const char *fmt, ...)
-	__attribute__((format (printf, 3, 4)));
+	__attribute__((format (printf, 2, 3)));
 
 /*
  * The `repo_worktree_path` family of functions will construct a path into a
diff --git a/revision.c b/revision.c
index 29972c3a198..ca3481c1902 100644
--- a/revision.c
+++ b/revision.c
@@ -1847,7 +1847,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
 		wt_gitdir = get_worktree_git_dir(wt);
 
 		if (read_index_from(&istate,
-				    worktree_git_path(the_repository, wt, "index"),
+				    worktree_git_path(wt, "index"),
 				    wt_gitdir) > 0)
 			do_add_index_objects_to_pending(revs, &istate, flags);
 
diff --git a/worktree.c b/worktree.c
index fd182c319b7..efd2b75608d 100644
--- a/worktree.c
+++ b/worktree.c
@@ -308,7 +308,7 @@ const char *worktree_lock_reason(struct worktree *wt)
 	if (!wt->lock_reason_valid) {
 		struct strbuf path = STRBUF_INIT;
 
-		strbuf_addstr(&path, worktree_git_path(the_repository, wt, "locked"));
+		strbuf_addstr(&path, worktree_git_path(wt, "locked"));
 		if (file_exists(path.buf)) {
 			struct strbuf lock_reason = STRBUF_INIT;
 			if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
diff --git a/wt-status.c b/wt-status.c
index 2debda534c1..68257d6dfd2 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1648,7 +1648,7 @@ static char *get_branch(const struct worktree *wt, const char *path)
 	struct object_id oid;
 	const char *branch_name;
 
-	if (strbuf_read_file(&sb, worktree_git_path(the_repository, wt, "%s", path), 0) <= 0)
+	if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
 		goto got_nothing;
 
 	while (sb.len && sb.buf[sb.len - 1] == '\n')
@@ -1750,18 +1750,18 @@ int wt_status_check_rebase(const struct worktree *wt,
 	if (!wt)
 		BUG("wt_status_check_rebase() called with NULL worktree");
 
-	if (!stat(worktree_git_path(the_repository, wt, "rebase-apply"), &st)) {
-		if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/applying"), &st)) {
+	if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
+		if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
 			state->am_in_progress = 1;
-			if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/patch"), &st) && !st.st_size)
+			if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
 				state->am_empty_patch = 1;
 		} else {
 			state->rebase_in_progress = 1;
 			state->branch = get_branch(wt, "rebase-apply/head-name");
 			state->onto = get_branch(wt, "rebase-apply/onto");
 		}
-	} else if (!stat(worktree_git_path(the_repository, wt, "rebase-merge"), &st)) {
-		if (!stat(worktree_git_path(the_repository, wt, "rebase-merge/interactive"), &st))
+	} else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
+		if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
 			state->rebase_interactive_in_progress = 1;
 		else
 			state->rebase_in_progress = 1;
@@ -1780,7 +1780,7 @@ int wt_status_check_bisect(const struct worktree *wt,
 	if (!wt)
 		BUG("wt_status_check_bisect() called with NULL worktree");
 
-	if (!stat(worktree_git_path(the_repository, wt, "BISECT_LOG"), &st)) {
+	if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
 		state->bisect_in_progress = 1;
 		state->bisecting_from = get_branch(wt, "BISECT_START");
 		return 1;
-- 
2.52.0.362.g884e03848a9
Previous: Phillip WoodNext: Karthik Nayak
Message 25 of 39 in “worktree: change representation and usage of primary worktree”
  1. Shreyansh PaliwalFeb 13, 2026
  2. [RFC][PATCH 1/2] worktree: represent the primary worktree with '/' instead of NULLShreyansh Paliwal, Feb 13, 2026
  3. Junio C HamanoFeb 13, 2026
  4. Shreyansh PaliwalFeb 14, 2026
  5. [RFC][PATCH 2/2] worktree: stop passing NULL as primary worktreeShreyansh Paliwal, Feb 13, 2026
  6. Junio C HamanoFeb 13, 2026
  7. Shreyansh PaliwalFeb 14, 2026
  8. Phillip WoodFeb 14, 2026
  9. Junio C HamanoFeb 14, 2026
  10. Shreyansh PaliwalFeb 15, 2026
  11. Phillip WoodFeb 16, 2026
  12. Junio C HamanoFeb 17, 2026
  13. Shreyansh PaliwalFeb 17, 2026
  14. 0/2 worktree_git_path(): remove repository argumentPhillip Wood, Feb 16, 2026
  15. 1/2 wt-status: avoid passing NULL worktreePhillip Wood, Feb 16, 2026
  16. Phillip WoodFeb 17, 2026
  17. Shreyansh PaliwalFeb 17, 2026
  18. Phillip WoodFeb 17, 2026
  19. Shreyansh PaliwalFeb 17, 2026
  20. Junio C HamanoFeb 17, 2026
  21. Karthik NayakFeb 17, 2026
  22. Phillip WoodFeb 18, 2026
  23. Junio C HamanoFeb 17, 2026
  24. Phillip WoodFeb 18, 2026
  25. 2/2 path: remove repository argument from worktree_git_path()Phillip Wood, Feb 16, 2026
  26. Karthik NayakFeb 17, 2026
  27. Shreyansh PaliwalFeb 17, 2026
  28. Phillip WoodFeb 17, 2026
  29. Shreyansh PaliwalFeb 17, 2026
  30. 0/2 worktree_git_path(): remove repository argumentPhillip Wood, Feb 19, 2026
  31. 1/2 wt-status: avoid passing NULL worktreePhillip Wood, Feb 19, 2026
  32. Junio C HamanoFeb 19, 2026
  33. Junio C HamanoFeb 19, 2026
  34. Phillip WoodFeb 25, 2026
  35. Junio C HamanoFeb 25, 2026
  36. Phillip WoodFeb 26, 2026
  37. Junio C HamanoFeb 26, 2026
  38. 2/2 path: remove repository argument from worktree_git_path()Phillip Wood, Feb 19, 2026
  39. Junio C HamanoFeb 19, 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.