]> git.ipfire.org Git - thirdparty/git.git/blobdiff - path.c
path: add repo_worktree_path and strbuf_repo_worktree_path
[thirdparty/git.git] / path.c
diff --git a/path.c b/path.c
index 76a872297e507f7580c0c24a8b307f4d8188e21e..e485f9f93181778de3acfe3cd4b6811ec82c7940 100644 (file)
--- a/path.c
+++ b/path.c
@@ -411,10 +411,32 @@ static void do_git_path(const struct repository *repo,
                strbuf_addch(buf, '/');
        gitdir_len = buf->len;
        strbuf_vaddf(buf, fmt, args);
-       adjust_git_path(repo, buf, gitdir_len);
+       if (!wt)
+               adjust_git_path(repo, buf, gitdir_len);
        strbuf_cleanup_path(buf);
 }
 
+char *repo_git_path(const struct repository *repo,
+                   const char *fmt, ...)
+{
+       struct strbuf path = STRBUF_INIT;
+       va_list args;
+       va_start(args, fmt);
+       do_git_path(repo, NULL, &path, fmt, args);
+       va_end(args);
+       return strbuf_detach(&path, NULL);
+}
+
+void strbuf_repo_git_path(struct strbuf *sb,
+                         const struct repository *repo,
+                         const char *fmt, ...)
+{
+       va_list args;
+       va_start(args, fmt);
+       do_git_path(repo, NULL, sb, fmt, args);
+       va_end(args);
+}
+
 char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
 {
        va_list args;
@@ -484,6 +506,47 @@ const char *worktree_git_path(const struct worktree *wt, const char *fmt, ...)
        return pathname->buf;
 }
 
+static void do_worktree_path(const struct repository *repo,
+                            struct strbuf *buf,
+                            const char *fmt, va_list args)
+{
+       strbuf_addstr(buf, repo->worktree);
+       if(buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
+               strbuf_addch(buf, '/');
+
+       strbuf_vaddf(buf, fmt, args);
+       strbuf_cleanup_path(buf);
+}
+
+char *repo_worktree_path(const struct repository *repo, const char *fmt, ...)
+{
+       struct strbuf path = STRBUF_INIT;
+       va_list args;
+
+       if (!repo->worktree)
+               return NULL;
+
+       va_start(args, fmt);
+       do_worktree_path(repo, &path, fmt, args);
+       va_end(args);
+
+       return strbuf_detach(&path, NULL);
+}
+
+void strbuf_repo_worktree_path(struct strbuf *sb,
+                              const struct repository *repo,
+                              const char *fmt, ...)
+{
+       va_list args;
+
+       if (!repo->worktree)
+               return;
+
+       va_start(args, fmt);
+       do_worktree_path(repo, sb, fmt, args);
+       va_end(args);
+}
+
 /* Returns 0 on success, negative on failure. */
 static int do_submodule_path(struct strbuf *buf, const char *path,
                             const char *fmt, va_list args)