path: refactor `repo_git_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 "gitdir" family of functions accordingly. Note that the `repo_git_pathv()` function is converted into an internal implementation detail. It is only used to implement `the_repository` compatibility shims and will eventually be removed from the public interface. 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 bdfc07bfdf3f4f4ef94580c0cb46eef5977bb810
5 files changed +32 -32
editor.c
+2 -4
@@ -142,10 +142,8 @@ int strbuf_edit_interactively(struct repository *r,
142 struct strbuf sb = STRBUF_INIT;
143 int fd, res = 0;
144
145 - if (!is_absolute_path(path)) {
146 - strbuf_repo_git_path(&sb, r, "%s", path);
147 - path = sb.buf;
148 - }
145 + if (!is_absolute_path(path))
146 + path = repo_git_path_append(r, &sb, "%s", path);
147
148 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
149 if (fd < 0)
hook.c
+1 -2
@@ -16,8 +16,7 @@ const char *find_hook(struct repository *r, const char *name)
16
17 int found_hook;
18
19 - strbuf_reset(&path);
20 - strbuf_repo_git_path(&path, r, "hooks/%s", name);
19 + repo_git_path_replace(r, &path, "hooks/%s", name);
20 found_hook = access(path.buf, X_OK) >= 0;
21 #ifdef STRIP_EXTENSION
22 if (!found_hook) {
path.c
+16 -3
@@ -443,14 +443,27 @@ char *repo_git_path(const struct repository *repo,
443 return strbuf_detach(&path, NULL);
444 }
445
446 -void strbuf_repo_git_path(struct strbuf *sb,
447 - const struct repository *repo,
448 - const char *fmt, ...)
446 +const char *repo_git_path_append(const struct repository *repo,
447 + struct strbuf *sb,
448 + const char *fmt, ...)
449 {
450 va_list args;
451 va_start(args, fmt);
452 repo_git_pathv(repo, NULL, sb, fmt, args);
453 va_end(args);
454 + return sb->buf;
455 +}
456 +
457 +const char *repo_git_path_replace(const struct repository *repo,
458 + struct strbuf *sb,
459 + const char *fmt, ...)
460 +{
461 + va_list args;
462 + strbuf_reset(sb);
463 + va_start(args, fmt);
464 + repo_git_pathv(repo, NULL, sb, fmt, args);
465 + va_end(args);
466 + return sb->buf;
467 }
468
469 char *mkpathdup(const char *fmt, ...)
path.h
+11 -21
@@ -52,29 +52,16 @@ const char *repo_common_path_replace(const struct repository *repo,
52 * For an exhaustive list of the adjustments made look at `common_list` and
53 * `adjust_git_path` in path.c.
54 */
55 -
56 -/*
57 - * Return a path into the git directory of repository `repo`.
58 - */
55 char *repo_git_path(const struct repository *repo,
56 const char *fmt, ...)
57 __attribute__((format (printf, 2, 3)));
62 -
63 -/*
64 - * Print a path into the git directory of repository `repo` into the provided
65 - * buffer.
66 - */
67 -void repo_git_pathv(const struct repository *repo,
68 - const struct worktree *wt, struct strbuf *buf,
69 - const char *fmt, va_list args);
70 -
71 -/*
72 - * Construct a path into the git directory of repository `repo` and append it
73 - * to the provided buffer `sb`.
74 - */
75 -void strbuf_repo_git_path(struct strbuf *sb,
76 - const struct repository *repo,
77 - const char *fmt, ...)
58 +const char *repo_git_path_append(const struct repository *repo,
59 + struct strbuf *sb,
60 + const char *fmt, ...)
61 + __attribute__((format (printf, 3, 4)));
62 +const char *repo_git_path_replace(const struct repository *repo,
63 + struct strbuf *sb,
64 + const char *fmt, ...)
65 __attribute__((format (printf, 3, 4)));
66
67 /*
@@ -241,11 +228,14 @@ struct strbuf *get_pathname(void);
228 # include "strbuf.h"
229 # include "repository.h"
230
244 -/* Internal implementation detail that should not be used. */
231 +/* Internal implementation details that should not be used. */
232 void repo_common_pathv(const struct repository *repo,
233 struct strbuf *buf,
234 const char *fmt,
235 va_list args);
236 +void repo_git_pathv(const struct repository *repo,
237 + const struct worktree *wt, struct strbuf *buf,
238 + const char *fmt, va_list args);
239
240 /*
241 * Return a statically allocated path into the main repository's
submodule.c
+2 -2
@@ -1315,7 +1315,7 @@ static int repo_has_absorbed_submodules(struct repository *r)
1315 int ret;
1316 struct strbuf buf = STRBUF_INIT;
1317
1318 - strbuf_repo_git_path(&buf, r, "modules/");
1318 + repo_git_path_append(r, &buf, "modules/");
1319 ret = file_exists(buf.buf) && !is_empty_dir(buf.buf);
1320 strbuf_release(&buf);
1321 return ret;
@@ -2629,6 +2629,6 @@ void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
2629 * administrators can explicitly set. Nothing has been decided,
2630 * so for now, just append the name at the end of the path.
2631 */
2632 - strbuf_repo_git_path(buf, r, "modules/");
2632 + repo_git_path_append(r, buf, "modules/");
2633 strbuf_addstr(buf, submodule_name);
2634 }