Git fork

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>

authored by

Patrick Steinhardt and committed by
Junio C Hamano
bdfc07bf 70a16ff8

+32 -32
+2 -4
editor.c
··· 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 - } 149 150 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666); 151 if (fd < 0)
··· 142 struct strbuf sb = STRBUF_INIT; 143 int fd, res = 0; 144 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)
+1 -2
hook.c
··· 16 17 int found_hook; 18 19 - strbuf_reset(&path); 20 - strbuf_repo_git_path(&path, r, "hooks/%s", name); 21 found_hook = access(path.buf, X_OK) >= 0; 22 #ifdef STRIP_EXTENSION 23 if (!found_hook) {
··· 16 17 int found_hook; 18 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) {
+16 -3
path.c
··· 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, ...) 449 { 450 va_list args; 451 va_start(args, fmt); 452 repo_git_pathv(repo, NULL, sb, fmt, args); 453 va_end(args); 454 } 455 456 char *mkpathdup(const char *fmt, ...)
··· 443 return strbuf_detach(&path, NULL); 444 } 445 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, ...)
+11 -21
path.h
··· 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 - */ 59 char *repo_git_path(const struct repository *repo, 60 const char *fmt, ...) 61 __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, ...) 78 __attribute__((format (printf, 3, 4))); 79 80 /* ··· 241 # include "strbuf.h" 242 # include "repository.h" 243 244 - /* Internal implementation detail that should not be used. */ 245 void repo_common_pathv(const struct repository *repo, 246 struct strbuf *buf, 247 const char *fmt, 248 va_list args); 249 250 /* 251 * Return a statically allocated path into the main repository's
··· 52 * For an exhaustive list of the adjustments made look at `common_list` and 53 * `adjust_git_path` in path.c. 54 */ 55 char *repo_git_path(const struct repository *repo, 56 const char *fmt, ...) 57 __attribute__((format (printf, 2, 3))); 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 /* ··· 228 # include "strbuf.h" 229 # include "repository.h" 230 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
+2 -2
submodule.c
··· 1315 int ret; 1316 struct strbuf buf = STRBUF_INIT; 1317 1318 - strbuf_repo_git_path(&buf, r, "modules/"); 1319 ret = file_exists(buf.buf) && !is_empty_dir(buf.buf); 1320 strbuf_release(&buf); 1321 return ret; ··· 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/"); 2633 strbuf_addstr(buf, submodule_name); 2634 }
··· 1315 int ret; 1316 struct strbuf buf = STRBUF_INIT; 1317 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 * administrators can explicitly set. Nothing has been decided, 2630 * so for now, just append the name at the end of the path. 2631 */ 2632 + repo_git_path_append(r, buf, "modules/"); 2633 strbuf_addstr(buf, submodule_name); 2634 }