Merge branch 'ps/path-sans-the-repository'
The path.[ch] API takes an explicit repository parameter passed throughout the callchain, instead of relying on the_repository singleton instance. * ps/path-sans-the-repository: path: adjust last remaining users of `the_repository` environment: move access to "core.sharedRepository" into repo settings environment: move access to "core.hooksPath" into repo settings repo-settings: introduce function to clear struct path: drop `git_path()` in favor of `repo_git_path()` rerere: let `rerere_path()` write paths into a caller-provided buffer path: drop `git_common_path()` in favor of `repo_common_path()` worktree: return allocated string from `get_worktree_git_dir()` path: drop `git_path_buf()` in favor of `repo_git_path_replace()` path: drop `git_pathdup()` in favor of `repo_git_path()` path: drop unused `strbuf_git_path()` function path: refactor `repo_submodule_path()` family of functions submodule: refactor `submodule_to_gitdir()` to accept a repo path: refactor `repo_worktree_path()` family of functions path: refactor `repo_git_path()` family of functions path: refactor `repo_common_path()` family of functions
This commit is contained in:
49
worktree.c
49
worktree.c
@ -59,8 +59,9 @@ static void add_head_info(struct worktree *wt)
|
||||
static int is_current_worktree(struct worktree *wt)
|
||||
{
|
||||
char *git_dir = absolute_pathdup(repo_get_git_dir(the_repository));
|
||||
const char *wt_git_dir = get_worktree_git_dir(wt);
|
||||
char *wt_git_dir = get_worktree_git_dir(wt);
|
||||
int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir));
|
||||
free(wt_git_dir);
|
||||
free(git_dir);
|
||||
return is_current;
|
||||
}
|
||||
@ -127,7 +128,7 @@ struct worktree *get_linked_worktree(const char *id,
|
||||
if (!id)
|
||||
die("Missing linked worktree name");
|
||||
|
||||
strbuf_git_common_path(&path, the_repository, "worktrees/%s/gitdir", id);
|
||||
repo_common_path_append(the_repository, &path, "worktrees/%s/gitdir", id);
|
||||
if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
|
||||
/* invalid gitdir file */
|
||||
goto done;
|
||||
@ -198,14 +199,14 @@ struct worktree **get_worktrees(void)
|
||||
return get_worktrees_internal(0);
|
||||
}
|
||||
|
||||
const char *get_worktree_git_dir(const struct worktree *wt)
|
||||
char *get_worktree_git_dir(const struct worktree *wt)
|
||||
{
|
||||
if (!wt)
|
||||
return repo_get_git_dir(the_repository);
|
||||
return xstrdup(repo_get_git_dir(the_repository));
|
||||
else if (!wt->id)
|
||||
return repo_get_common_dir(the_repository);
|
||||
return xstrdup(repo_get_common_dir(the_repository));
|
||||
else
|
||||
return git_common_path("worktrees/%s", wt->id);
|
||||
return repo_common_path(the_repository, "worktrees/%s", wt->id);
|
||||
}
|
||||
|
||||
static struct worktree *find_worktree_by_suffix(struct worktree **list,
|
||||
@ -336,6 +337,7 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
|
||||
{
|
||||
struct strbuf wt_path = STRBUF_INIT;
|
||||
struct strbuf realpath = STRBUF_INIT;
|
||||
struct strbuf buf = STRBUF_INIT;
|
||||
char *path = NULL;
|
||||
int err, ret = -1;
|
||||
|
||||
@ -365,7 +367,7 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
|
||||
if (!is_absolute_path(wt->path)) {
|
||||
strbuf_addf_gently(errmsg,
|
||||
_("'%s' file does not contain absolute path to the working tree location"),
|
||||
git_common_path("worktrees/%s/gitdir", wt->id));
|
||||
repo_common_path_replace(the_repository, &buf, "worktrees/%s/gitdir", wt->id));
|
||||
goto done;
|
||||
}
|
||||
|
||||
@ -387,14 +389,16 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
|
||||
goto done;
|
||||
}
|
||||
|
||||
strbuf_realpath(&realpath, git_common_path("worktrees/%s", wt->id), 1);
|
||||
strbuf_realpath(&realpath, repo_common_path_replace(the_repository, &buf, "worktrees/%s", wt->id), 1);
|
||||
ret = fspathcmp(path, realpath.buf);
|
||||
|
||||
if (ret)
|
||||
strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"),
|
||||
wt->path, git_common_path("worktrees/%s", wt->id));
|
||||
wt->path, repo_common_path_replace(the_repository, &buf,
|
||||
"worktrees/%s", wt->id));
|
||||
done:
|
||||
free(path);
|
||||
strbuf_release(&buf);
|
||||
strbuf_release(&wt_path);
|
||||
strbuf_release(&realpath);
|
||||
return ret;
|
||||
@ -406,11 +410,13 @@ void update_worktree_location(struct worktree *wt, const char *path_,
|
||||
struct strbuf path = STRBUF_INIT;
|
||||
struct strbuf dotgit = STRBUF_INIT;
|
||||
struct strbuf gitdir = STRBUF_INIT;
|
||||
char *wt_gitdir;
|
||||
|
||||
if (is_main_worktree(wt))
|
||||
BUG("can't relocate main worktree");
|
||||
|
||||
strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1);
|
||||
wt_gitdir = repo_common_path(the_repository, "worktrees/%s/gitdir", wt->id);
|
||||
strbuf_realpath(&gitdir, wt_gitdir, 1);
|
||||
strbuf_realpath(&path, path_, 1);
|
||||
strbuf_addf(&dotgit, "%s/.git", path.buf);
|
||||
if (fspathcmp(wt->path, path.buf)) {
|
||||
@ -422,6 +428,7 @@ void update_worktree_location(struct worktree *wt, const char *path_,
|
||||
strbuf_release(&path);
|
||||
strbuf_release(&dotgit);
|
||||
strbuf_release(&gitdir);
|
||||
free(wt_gitdir);
|
||||
}
|
||||
|
||||
int is_worktree_being_rebased(const struct worktree *wt,
|
||||
@ -510,7 +517,8 @@ int submodule_uses_worktrees(const char *path)
|
||||
int ret = 0;
|
||||
struct repository_format format = REPOSITORY_FORMAT_INIT;
|
||||
|
||||
submodule_gitdir = git_pathdup_submodule(path, "%s", "");
|
||||
submodule_gitdir = repo_submodule_path(the_repository,
|
||||
path, "%s", "");
|
||||
if (!submodule_gitdir)
|
||||
return 0;
|
||||
|
||||
@ -606,6 +614,7 @@ static void repair_gitfile(struct worktree *wt,
|
||||
struct strbuf backlink = STRBUF_INIT;
|
||||
char *dotgit_contents = NULL;
|
||||
const char *repair = NULL;
|
||||
char *path = NULL;
|
||||
int err;
|
||||
|
||||
/* missing worktree can't be repaired */
|
||||
@ -617,7 +626,8 @@ static void repair_gitfile(struct worktree *wt,
|
||||
goto done;
|
||||
}
|
||||
|
||||
strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
|
||||
path = repo_common_path(the_repository, "worktrees/%s", wt->id);
|
||||
strbuf_realpath(&repo, path, 1);
|
||||
strbuf_addf(&dotgit, "%s/.git", wt->path);
|
||||
strbuf_addf(&gitdir, "%s/gitdir", repo.buf);
|
||||
dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
|
||||
@ -647,6 +657,7 @@ static void repair_gitfile(struct worktree *wt,
|
||||
|
||||
done:
|
||||
free(dotgit_contents);
|
||||
free(path);
|
||||
strbuf_release(&repo);
|
||||
strbuf_release(&dotgit);
|
||||
strbuf_release(&gitdir);
|
||||
@ -678,11 +689,13 @@ void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path
|
||||
struct strbuf gitdir = STRBUF_INIT;
|
||||
struct strbuf dotgit = STRBUF_INIT;
|
||||
int is_relative_path;
|
||||
char *path = NULL;
|
||||
|
||||
if (is_main_worktree(wt))
|
||||
goto done;
|
||||
|
||||
strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1);
|
||||
path = repo_common_path(the_repository, "worktrees/%s/gitdir", wt->id);
|
||||
strbuf_realpath(&gitdir, path, 1);
|
||||
|
||||
if (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0)
|
||||
goto done;
|
||||
@ -701,6 +714,7 @@ void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path
|
||||
done:
|
||||
strbuf_release(&gitdir);
|
||||
strbuf_release(&dotgit);
|
||||
free(path);
|
||||
}
|
||||
|
||||
void repair_worktrees_after_gitdir_move(const char *old_path)
|
||||
@ -754,8 +768,7 @@ static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)
|
||||
id++; /* advance past '/' to point at <id> */
|
||||
if (!*id)
|
||||
goto error;
|
||||
strbuf_reset(inferred);
|
||||
strbuf_git_common_path(inferred, the_repository, "worktrees/%s", id);
|
||||
repo_common_path_replace(the_repository, inferred, "worktrees/%s", id);
|
||||
if (!is_directory(inferred->buf))
|
||||
goto error;
|
||||
|
||||
@ -893,7 +906,11 @@ int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath,
|
||||
ssize_t read_result;
|
||||
|
||||
*wtpath = NULL;
|
||||
strbuf_realpath(&repo, git_common_path("worktrees/%s", id), 1);
|
||||
|
||||
path = repo_common_path(the_repository, "worktrees/%s", id);
|
||||
strbuf_realpath(&repo, path, 1);
|
||||
FREE_AND_NULL(path);
|
||||
|
||||
strbuf_addf(&gitdir, "%s/gitdir", repo.buf);
|
||||
if (!is_directory(repo.buf)) {
|
||||
strbuf_addstr(reason, _("not a valid directory"));
|
||||
|
||||
Reference in New Issue
Block a user