path: refactor `repo_worktree_path()` family of functions

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>

Patrick Steinhardt committed Feb 7, 2025 at 12:03 UTC 93a8cfaf3c24f8c1f999b2ca5532ff8f46e0808d
3 files changed +33 -19
path.c
+22 -6
@@ -519,28 +519,44 @@ char *repo_worktree_path(const struct repository *repo, const char *fmt, ...)
519 struct strbuf path = STRBUF_INIT;
520 va_list args;
521
522 + va_start(args, fmt);
523 + do_worktree_path(repo, &path, fmt, args);
524 + va_end(args);
525 +
526 + return strbuf_detach(&path, NULL);
527 +}
528 +
529 +const char *repo_worktree_path_append(const struct repository *repo,
530 + struct strbuf *sb,
531 + const char *fmt, ...)
532 +{
533 + va_list args;
534 +
535 if (!repo->worktree)
536 return NULL;
537
538 va_start(args, fmt);
526 - do_worktree_path(repo, &path, fmt, args);
539 + do_worktree_path(repo, sb, fmt, args);
540 va_end(args);
541
529 - return strbuf_detach(&path, NULL);
542 + return sb->buf;
543 }
544
532 -void strbuf_repo_worktree_path(struct strbuf *sb,
533 - const struct repository *repo,
534 - const char *fmt, ...)
545 +const char *repo_worktree_path_replace(const struct repository *repo,
546 + struct strbuf *sb,
547 + const char *fmt, ...)
548 {
549 va_list args;
550
551 + strbuf_reset(sb);
552 if (!repo->worktree)
539 - return;
553 + return NULL;
554
555 va_start(args, fmt);
556 do_worktree_path(repo, sb, fmt, args);
557 va_end(args);
558 +
559 + return sb->buf;
560 }
561
562 /* Returns 0 on success, negative on failure. */
path.h
+9 -11
@@ -75,24 +75,22 @@ const char *worktree_git_path(struct repository *r,
75 __attribute__((format (printf, 3, 4)));
76
77 /*
78 - * Return a path into the worktree of repository `repo`.
78 + * The `repo_worktree_path` family of functions will construct a path into a
79 + * repository's worktree.
80 *
80 - * If the repository doesn't have a worktree NULL is returned.
81 + * Returns a `NULL` pointer in case the repository has no worktree.
82 */
83 char *repo_worktree_path(const struct repository *repo,
84 const char *fmt, ...)
85 __attribute__((format (printf, 2, 3)));
85 -
86 -/*
87 - * Construct a path into the worktree of repository `repo` and append it
88 - * to the provided buffer `sb`.
89 - *
90 - * If the repository doesn't have a worktree nothing will be appended to `sb`.
91 - */
92 -void strbuf_repo_worktree_path(struct strbuf *sb,
93 - const struct repository *repo,
86 +const char *repo_worktree_path_append(const struct repository *repo,
87 + struct strbuf *sb,
88 const char *fmt, ...)
89 __attribute__((format (printf, 3, 4)));
90 +const char *repo_worktree_path_replace(const struct repository *repo,
91 + struct strbuf *sb,
92 + const char *fmt, ...)
93 + __attribute__((format (printf, 3, 4)));
94
95 /*
96 * Return a path into a submodule's git directory located at `path`. `path`
repository.c
+2 -2
@@ -312,8 +312,8 @@ int repo_submodule_init(struct repository *subrepo,
312 struct strbuf worktree = STRBUF_INIT;
313 int ret = 0;
314
315 - strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
316 - strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
315 + repo_worktree_path_append(superproject, &gitdir, "%s/.git", path);
316 + repo_worktree_path_append(superproject, &worktree, "%s", path);
317
318 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
319 /*