Git fork

path: drop `git_common_path()` in favor of `repo_common_path()`

Remove `git_common_path()` in favor of the `repo_common_path()` family
of functions, which makes the implicit dependency on `the_repository` go
away.

Note that `git_common_path()` used to return a string allocated via
`get_pathname()`, which uses a rotating set of statically allocated
buffers. Consequently, callers didn't have to free the returned string.
The same isn't true for `repo_common_path()`, so we also have to add
logic to free the returned strings.

This refactoring also allows us to remove `repo_common_pathv()` 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
07242c2a 8e4710f0

+40 -35
+12 -4
builtin/worktree.c
··· 151 151 struct strbuf sb = STRBUF_INIT; 152 152 int ret; 153 153 154 - strbuf_addstr(&sb, git_common_path("worktrees/%s", id)); 154 + repo_common_path_append(the_repository, &sb, "worktrees/%s", id); 155 155 ret = remove_dir_recursively(&sb, 0); 156 156 if (ret < 0 && errno == ENOTDIR) 157 157 ret = unlink(sb.buf); ··· 1102 1102 OPT_END() 1103 1103 }; 1104 1104 struct worktree **worktrees, *wt; 1105 + char *path; 1105 1106 1106 1107 ac = parse_options(ac, av, prefix, options, git_worktree_lock_usage, 0); 1107 1108 if (ac != 1) ··· 1122 1123 die(_("'%s' is already locked"), av[0]); 1123 1124 } 1124 1125 1125 - write_file(git_common_path("worktrees/%s/locked", wt->id), 1126 - "%s", reason); 1126 + path = repo_common_path(the_repository, "worktrees/%s/locked", wt->id); 1127 + write_file(path, "%s", reason); 1128 + 1127 1129 free_worktrees(worktrees); 1130 + free(path); 1128 1131 return 0; 1129 1132 } 1130 1133 ··· 1135 1138 OPT_END() 1136 1139 }; 1137 1140 struct worktree **worktrees, *wt; 1141 + char *path; 1138 1142 int ret; 1139 1143 1140 1144 ac = parse_options(ac, av, prefix, options, git_worktree_unlock_usage, 0); ··· 1149 1153 die(_("The main working tree cannot be locked or unlocked")); 1150 1154 if (!worktree_lock_reason(wt)) 1151 1155 die(_("'%s' is not locked"), av[0]); 1152 - ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id)); 1156 + 1157 + path = repo_common_path(the_repository, "worktrees/%s/locked", wt->id); 1158 + ret = unlink_or_warn(path); 1159 + 1153 1160 free_worktrees(worktrees); 1161 + free(path); 1154 1162 return ret; 1155 1163 } 1156 1164
+4 -4
path.c
··· 634 634 return buf->buf; 635 635 } 636 636 637 - void repo_common_pathv(const struct repository *repo, 638 - struct strbuf *sb, 639 - const char *fmt, 640 - va_list args) 637 + static void repo_common_pathv(const struct repository *repo, 638 + struct strbuf *sb, 639 + const char *fmt, 640 + va_list args) 641 641 { 642 642 strbuf_addstr(sb, repo->commondir); 643 643 if (sb->len && !is_dir_sep(sb->buf[sb->len - 1]))
-19
path.h
··· 233 233 # include "repository.h" 234 234 235 235 /* Internal implementation details that should not be used. */ 236 - void repo_common_pathv(const struct repository *repo, 237 - struct strbuf *buf, 238 - const char *fmt, 239 - va_list args); 240 236 void repo_git_pathv(const struct repository *repo, 241 237 const struct worktree *wt, struct strbuf *buf, 242 238 const char *fmt, va_list args); 243 - 244 - /* 245 - * Return a statically allocated path into the main repository's 246 - * (the_repository) common git directory. 247 - */ 248 - __attribute__((format (printf, 1, 2))) 249 - static inline const char *git_common_path(const char *fmt, ...) 250 - { 251 - struct strbuf *pathname = get_pathname(); 252 - va_list args; 253 - va_start(args, fmt); 254 - repo_common_pathv(the_repository, pathname, fmt, args); 255 - va_end(args); 256 - return pathname->buf; 257 - } 258 239 259 240 /* 260 241 * Return a statically allocated path into the main repository's
+24 -8
worktree.c
··· 183 183 else if (!wt->id) 184 184 return xstrdup(repo_get_common_dir(the_repository)); 185 185 else 186 - return xstrdup(git_common_path("worktrees/%s", wt->id)); 186 + return repo_common_path(the_repository, "worktrees/%s", wt->id); 187 187 } 188 188 189 189 static struct worktree *find_worktree_by_suffix(struct worktree **list, ··· 314 314 { 315 315 struct strbuf wt_path = STRBUF_INIT; 316 316 struct strbuf realpath = STRBUF_INIT; 317 + struct strbuf buf = STRBUF_INIT; 317 318 char *path = NULL; 318 319 int err, ret = -1; 319 320 ··· 343 344 if (!is_absolute_path(wt->path)) { 344 345 strbuf_addf_gently(errmsg, 345 346 _("'%s' file does not contain absolute path to the working tree location"), 346 - git_common_path("worktrees/%s/gitdir", wt->id)); 347 + repo_common_path_replace(the_repository, &buf, "worktrees/%s/gitdir", wt->id)); 347 348 goto done; 348 349 } 349 350 ··· 365 366 goto done; 366 367 } 367 368 368 - strbuf_realpath(&realpath, git_common_path("worktrees/%s", wt->id), 1); 369 + strbuf_realpath(&realpath, repo_common_path_replace(the_repository, &buf, "worktrees/%s", wt->id), 1); 369 370 ret = fspathcmp(path, realpath.buf); 370 371 371 372 if (ret) 372 373 strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"), 373 - wt->path, git_common_path("worktrees/%s", wt->id)); 374 + wt->path, repo_common_path_replace(the_repository, &buf, 375 + "worktrees/%s", wt->id)); 374 376 done: 375 377 free(path); 378 + strbuf_release(&buf); 376 379 strbuf_release(&wt_path); 377 380 strbuf_release(&realpath); 378 381 return ret; ··· 384 387 struct strbuf path = STRBUF_INIT; 385 388 struct strbuf dotgit = STRBUF_INIT; 386 389 struct strbuf gitdir = STRBUF_INIT; 390 + char *wt_gitdir; 387 391 388 392 if (is_main_worktree(wt)) 389 393 BUG("can't relocate main worktree"); 390 394 391 - strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1); 395 + wt_gitdir = repo_common_path(the_repository, "worktrees/%s/gitdir", wt->id); 396 + strbuf_realpath(&gitdir, wt_gitdir, 1); 392 397 strbuf_realpath(&path, path_, 1); 393 398 strbuf_addf(&dotgit, "%s/.git", path.buf); 394 399 if (fspathcmp(wt->path, path.buf)) { ··· 400 405 strbuf_release(&path); 401 406 strbuf_release(&dotgit); 402 407 strbuf_release(&gitdir); 408 + free(wt_gitdir); 403 409 } 404 410 405 411 int is_worktree_being_rebased(const struct worktree *wt, ··· 585 591 struct strbuf backlink = STRBUF_INIT; 586 592 char *dotgit_contents = NULL; 587 593 const char *repair = NULL; 594 + char *path = NULL; 588 595 int err; 589 596 590 597 /* missing worktree can't be repaired */ ··· 596 603 goto done; 597 604 } 598 605 599 - strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1); 606 + path = repo_common_path(the_repository, "worktrees/%s", wt->id); 607 + strbuf_realpath(&repo, path, 1); 600 608 strbuf_addf(&dotgit, "%s/.git", wt->path); 601 609 strbuf_addf(&gitdir, "%s/gitdir", repo.buf); 602 610 dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err)); ··· 626 634 627 635 done: 628 636 free(dotgit_contents); 637 + free(path); 629 638 strbuf_release(&repo); 630 639 strbuf_release(&dotgit); 631 640 strbuf_release(&gitdir); ··· 657 666 struct strbuf gitdir = STRBUF_INIT; 658 667 struct strbuf dotgit = STRBUF_INIT; 659 668 int is_relative_path; 669 + char *path = NULL; 660 670 661 671 if (is_main_worktree(wt)) 662 672 goto done; 663 673 664 - strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1); 674 + path = repo_common_path(the_repository, "worktrees/%s/gitdir", wt->id); 675 + strbuf_realpath(&gitdir, path, 1); 665 676 666 677 if (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0) 667 678 goto done; ··· 680 691 done: 681 692 strbuf_release(&gitdir); 682 693 strbuf_release(&dotgit); 694 + free(path); 683 695 } 684 696 685 697 void repair_worktrees_after_gitdir_move(const char *old_path) ··· 871 883 ssize_t read_result; 872 884 873 885 *wtpath = NULL; 874 - strbuf_realpath(&repo, git_common_path("worktrees/%s", id), 1); 886 + 887 + path = repo_common_path(the_repository, "worktrees/%s", id); 888 + strbuf_realpath(&repo, path, 1); 889 + FREE_AND_NULL(path); 890 + 875 891 strbuf_addf(&gitdir, "%s/gitdir", repo.buf); 876 892 if (!is_directory(repo.buf)) { 877 893 strbuf_addstr(reason, _("not a valid directory"));