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

[PATCH v2 03/16] path: refactor `repo_worktree_path()` family of functions

From
Patrick Steinhardt <ps@pks.im>
Date
Feb 7, 2025, 11:03 UTC
Message-ID
<20250207-b4-pks-path-drop-the-repository-v2-3-13cad3c11b8a@pks.im>
In-Reply-To
<20250207-b4-pks-path-drop-the-repository-v2-0-13cad3c11b8a@pks.im>

As explained in an earlier commit, we're refactoring path-related functions to provide a consistent interface for computing paths into the commondir, gitdir and worktree. Refactor the "worktree" family of functions accordingly.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 path.c       | 28 ++++++++++++++++++++++------
 path.h       | 20 +++++++++-----------
 repository.c |  4 ++--
 3 files changed, 33 insertions(+), 19 deletions(-)
diff --git a/path.c b/path.c
index 779aa94b56..499116dd1e 100644
--- a/path.c
+++ b/path.c
@@ -519,28 +519,44 @@ char *repo_worktree_path(const struct repository *repo, const char *fmt, ...)
 	struct strbuf path = STRBUF_INIT;
 	va_list args;
 
+	va_start(args, fmt);
+	do_worktree_path(repo, &path, fmt, args);
+	va_end(args);
+
+	return strbuf_detach(&path, NULL);
+}
+
+const char *repo_worktree_path_append(const struct repository *repo,
+				      struct strbuf *sb,
+				      const char *fmt, ...)
+{
+	va_list args;
+
 	if (!repo->worktree)
 		return NULL;
 
 	va_start(args, fmt);
-	do_worktree_path(repo, &path, fmt, args);
+	do_worktree_path(repo, sb, fmt, args);
 	va_end(args);
 
-	return strbuf_detach(&path, NULL);
+	return sb->buf;
 }
 
-void strbuf_repo_worktree_path(struct strbuf *sb,
-			       const struct repository *repo,
-			       const char *fmt, ...)
+const char *repo_worktree_path_replace(const struct repository *repo,
+				       struct strbuf *sb,
+				       const char *fmt, ...)
 {
 	va_list args;
 
+	strbuf_reset(sb);
 	if (!repo->worktree)
-		return;
+		return NULL;
 
 	va_start(args, fmt);
 	do_worktree_path(repo, sb, fmt, args);
 	va_end(args);
+
+	return sb->buf;
 }
 
 /* Returns 0 on success, negative on failure. */
diff --git a/path.h b/path.h
index c45311b0a8..d3f85f0676 100644
--- a/path.h
+++ b/path.h
@@ -75,24 +75,22 @@ const char *worktree_git_path(struct repository *r,
 	__attribute__((format (printf, 3, 4)));
 
 /*
- * Return a path into the worktree of repository `repo`.
+ * The `repo_worktree_path` family of functions will construct a path into a
+ * repository's worktree.
  *
- * If the repository doesn't have a worktree NULL is returned.
+ * Returns a `NULL` pointer in case the repository has no worktree.
  */
 char *repo_worktree_path(const struct repository *repo,
 				const char *fmt, ...)
 	__attribute__((format (printf, 2, 3)));
-
-/*
- * Construct a path into the worktree of repository `repo` and append it
- * to the provided buffer `sb`.
- *
- * If the repository doesn't have a worktree nothing will be appended to `sb`.
- */
-void strbuf_repo_worktree_path(struct strbuf *sb,
-				      const struct repository *repo,
+const char *repo_worktree_path_append(const struct repository *repo,
+				      struct strbuf *sb,
 				      const char *fmt, ...)
 	__attribute__((format (printf, 3, 4)));
+const char *repo_worktree_path_replace(const struct repository *repo,
+				       struct strbuf *sb,
+				       const char *fmt, ...)
+	__attribute__((format (printf, 3, 4)));
 
 /*
  * Return a path into a submodule's git directory located at `path`.  `path`
diff --git a/repository.c b/repository.c
index 1a6a62bbd0..648cd88474 100644
--- a/repository.c
+++ b/repository.c
@@ -312,8 +312,8 @@ int repo_submodule_init(struct repository *subrepo,
 	struct strbuf worktree = STRBUF_INIT;
 	int ret = 0;
 
-	strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
-	strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
+	repo_worktree_path_append(superproject, &gitdir, "%s/.git", path);
+	repo_worktree_path_append(superproject, &worktree, "%s", path);
 
 	if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
 		/*
-- 
2.48.1.538.gc4cfc42d60.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 4 of 28 in “path: remove dependency on `the_repository`”
  1. 00/16 path: remove dependency on `the_repository`Patrick Steinhardt, Feb 7, 2025
  2. 01/16 path: refactor `repo_common_path()` family of functionsPatrick Steinhardt, Feb 7, 2025
  3. 02/16 path: refactor `repo_git_path()` family of functionsPatrick Steinhardt, Feb 7, 2025
  4. 03/16 path: refactor `repo_worktree_path()` family of functionsPatrick Steinhardt, Feb 7, 2025
  5. 04/16 submodule: refactor `submodule_to_gitdir()` to accept a repoPatrick Steinhardt, Feb 7, 2025
  6. 05/16 path: refactor `repo_submodule_path()` family of functionsPatrick Steinhardt, Feb 7, 2025
  7. 06/16 path: drop unused `strbuf_git_path()` functionPatrick Steinhardt, Feb 7, 2025
  8. 07/16 path: drop `git_pathdup()` in favor of `repo_git_path()`Patrick Steinhardt, Feb 7, 2025
  9. 08/16 path: drop `git_path_buf()` in favor of `repo_git_path_replace()`Patrick Steinhardt, Feb 7, 2025
  10. 09/16 worktree: return allocated string from `get_worktree_git_dir()`Patrick Steinhardt, Feb 7, 2025
  11. 10/16 path: drop `git_common_path()` in favor of `repo_common_path()`Patrick Steinhardt, Feb 7, 2025
  12. 11/16 rerere: let `rerere_path()` write paths into a caller-provided bufferPatrick Steinhardt, Feb 7, 2025
  13. Jeff KingFeb 22, 2025
  14. Junio C HamanoFeb 24, 2025
  15. Jeff KingFeb 24, 2025
  16. Junio C HamanoFeb 24, 2025
  17. Jeff KingFeb 24, 2025
  18. Junio C HamanoFeb 24, 2025
  19. Patrick SteinhardtFeb 25, 2025
  20. 12/16 path: drop `git_path()` in favor of `repo_git_path()`Patrick Steinhardt, Feb 7, 2025
  21. 13/16 repo-settings: introduce function to clear structPatrick Steinhardt, Feb 7, 2025
  22. 14/16 environment: move access to "core.hooksPath" into repo settingsPatrick Steinhardt, Feb 7, 2025
  23. 15/16 environment: move access to "core.sharedRepository" into repo settingsPatrick Steinhardt, Feb 7, 2025
  24. 16/16 path: adjust last remaining users of `the_repository`Patrick Steinhardt, Feb 7, 2025
  25. Karthik NayakFeb 7, 2025
  26. shejialuoFeb 8, 2025
  27. Junio C HamanoFeb 10, 2025
  28. shejialuoFeb 11, 2025

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.