Merge branch 'pw/worktree-reduce-the-repository' into next

Reduce the reference to the_repository in the worktree subsystem.

* pw/worktree-reduce-the-repository:
  worktree: reject NULL worktree in get_worktree_git_dir()
  worktree add: stop reading ".git/HEAD"
  worktree: remove "the_repository" from is_current_worktree()
This commit is contained in:
Junio C Hamano
2026-03-30 14:04:21 -07:00
4 changed files with 20 additions and 42 deletions

View File

@@ -692,25 +692,8 @@ static int can_use_local_refs(const struct add_opts *opts)
if (refs_head_ref(get_main_ref_store(the_repository), first_valid_ref, NULL)) {
return 1;
} else if (refs_for_each_branch_ref(get_main_ref_store(the_repository), first_valid_ref, NULL)) {
if (!opts->quiet) {
struct strbuf path = STRBUF_INIT;
struct strbuf contents = STRBUF_INIT;
char *wt_gitdir = get_worktree_git_dir(NULL);
strbuf_add_real_path(&path, wt_gitdir);
strbuf_addstr(&path, "/HEAD");
strbuf_read_file(&contents, path.buf, 64);
strbuf_stripspace(&contents, NULL);
strbuf_strip_suffix(&contents, "\n");
warning(_("HEAD points to an invalid (or orphaned) reference.\n"
"HEAD path: '%s'\n"
"HEAD contents: '%s'"),
path.buf, contents.buf);
strbuf_release(&path);
strbuf_release(&contents);
free(wt_gitdir);
}
if (!opts->quiet)
warning(_("HEAD points to an invalid (or orphaned) reference.\n"));
return 1;
}
return 0;

View File

@@ -987,7 +987,7 @@ test_dwim_orphan () {
then
test_must_be_empty actual
else
grep "$info_text" actual
test_grep "$info_text" actual
fi
elif [ "$outcome" = "no_infer" ]
then
@@ -996,39 +996,35 @@ test_dwim_orphan () {
then
test_must_be_empty actual
else
! grep "$info_text" actual
test_grep ! "$info_text" actual
fi
elif [ "$outcome" = "fetch_error" ]
then
test_must_fail git $dashc_args worktree add $args 2>actual &&
grep "$fetch_error_text" actual
test_grep "$fetch_error_text" actual
elif [ "$outcome" = "fatal_orphan_bad_combo" ]
then
test_must_fail git $dashc_args worktree add $args 2>actual &&
if [ $use_quiet -eq 1 ]
then
! grep "$info_text" actual
test_grep ! "$info_text" actual
else
grep "$info_text" actual
test_grep "$info_text" actual
fi &&
grep "$bad_combo_regex" actual
test_grep "$bad_combo_regex" actual
elif [ "$outcome" = "warn_bad_head" ]
then
test_must_fail git $dashc_args worktree add $args 2>actual &&
if [ $use_quiet -eq 1 ]
then
grep "$invalid_ref_regex" actual &&
! grep "$orphan_hint" actual
test_grep "$invalid_ref_regex" actual &&
test_grep ! "$orphan_hint" actual
else
headpath=$(git $dashc_args rev-parse --path-format=absolute --git-path HEAD) &&
headcontents=$(cat "$headpath") &&
grep "HEAD points to an invalid (or orphaned) reference" actual &&
grep "HEAD path: .$headpath." actual &&
grep "HEAD contents: .$headcontents." actual &&
grep "$orphan_hint" actual &&
! grep "$info_text" actual
test_grep "HEAD points to an invalid (or orphaned) reference" actual &&
test_grep "$orphan_hint" actual &&
test_grep ! "$info_text" actual
fi &&
grep "$invalid_ref_regex" actual
test_grep "$invalid_ref_regex" actual
else
# Unreachable
false

View File

@@ -58,7 +58,7 @@ 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));
char *git_dir = absolute_pathdup(repo_get_git_dir(wt->repo));
char *wt_git_dir = get_worktree_git_dir(wt);
int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir));
free(wt_git_dir);
@@ -78,7 +78,7 @@ struct worktree *get_worktree_from_repository(struct repository *repo)
wt->is_bare = !repo->worktree;
if (fspathcmp(gitdir, commondir))
wt->id = xstrdup(find_last_dir_sep(gitdir) + 1);
wt->is_current = is_current_worktree(wt);
wt->is_current = true;
add_head_info(wt);
free(gitdir);
@@ -227,11 +227,11 @@ struct worktree **get_worktrees_without_reading_head(void)
char *get_worktree_git_dir(const struct worktree *wt)
{
if (!wt)
return xstrdup(repo_get_git_dir(the_repository));
BUG("%s() called with NULL worktree", __func__);
else if (!wt->id)
return xstrdup(repo_get_common_dir(the_repository));
return xstrdup(repo_get_common_dir(wt->repo));
else
return repo_common_path(the_repository, "worktrees/%s", wt->id);
return repo_common_path(wt->repo, "worktrees/%s", wt->id);
}
static struct worktree *find_worktree_by_suffix(struct worktree **list,

View File

@@ -16,7 +16,7 @@ struct worktree {
struct object_id head_oid;
int is_detached;
int is_bare;
int is_current;
int is_current; /* does `path` match `repo->worktree` */
int lock_reason_valid; /* private */
int prune_reason_valid; /* private */
};
@@ -51,7 +51,6 @@ int submodule_uses_worktrees(const char *path);
/*
* Return git dir of the worktree. Note that the path may be relative.
* If wt is NULL, git dir of current worktree is returned.
*/
char *get_worktree_git_dir(const struct worktree *wt);