]> git.ipfire.org Git - thirdparty/git.git/commitdiff
path: refactor `repo_worktree_path()` family of functions
authorPatrick Steinhardt <ps@pks.im>
Fri, 7 Feb 2025 11:03:28 +0000 (12:03 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 7 Feb 2025 17:59:21 +0000 (09:59 -0800)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
path.c
path.h
repository.c

diff --git a/path.c b/path.c
index 779aa94b567a9d7970b64b97981b3f6d1d871a35..499116dd1e0760b21658d97e69294eebaecc0ee4 100644 (file)
--- 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 c45311b0a839a2020a1a81dad19f37338469ca37..d3f85f06762c67683345f7785f18f3e181076f39 100644 (file)
--- 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`
index 1a6a62bbd03a5dc4fdade3eb45ea2696968abc23..648cd88474eda3405b4436c12bda6a47b5c9e102 100644 (file)
@@ -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)) {
                /*