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

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()
maint
Junio C Hamano 2026-04-03 13:01:09 -07:00
commit 05ddb9ee8a
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)) { if (refs_head_ref(get_main_ref_store(the_repository), first_valid_ref, NULL)) {
return 1; return 1;
} else if (refs_for_each_branch_ref(get_main_ref_store(the_repository), first_valid_ref, NULL)) { } else if (refs_for_each_branch_ref(get_main_ref_store(the_repository), first_valid_ref, NULL)) {
if (!opts->quiet) { if (!opts->quiet)
struct strbuf path = STRBUF_INIT; warning(_("HEAD points to an invalid (or orphaned) reference.\n"));
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);
}
return 1; return 1;
} }
return 0; return 0;

View File

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

View File

@ -58,7 +58,7 @@ static void add_head_info(struct worktree *wt)


static int is_current_worktree(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); char *wt_git_dir = get_worktree_git_dir(wt);
int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir)); int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir));
free(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; wt->is_bare = !repo->worktree;
if (fspathcmp(gitdir, commondir)) if (fspathcmp(gitdir, commondir))
wt->id = xstrdup(find_last_dir_sep(gitdir) + 1); wt->id = xstrdup(find_last_dir_sep(gitdir) + 1);
wt->is_current = is_current_worktree(wt); wt->is_current = true;
add_head_info(wt); add_head_info(wt);


free(gitdir); free(gitdir);
@ -227,11 +227,11 @@ struct worktree **get_worktrees_without_reading_head(void)
char *get_worktree_git_dir(const struct worktree *wt) char *get_worktree_git_dir(const struct worktree *wt)
{ {
if (!wt) if (!wt)
return xstrdup(repo_get_git_dir(the_repository)); BUG("%s() called with NULL worktree", __func__);
else if (!wt->id) else if (!wt->id)
return xstrdup(repo_get_common_dir(the_repository)); return xstrdup(repo_get_common_dir(wt->repo));
else 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, static struct worktree *find_worktree_by_suffix(struct worktree **list,

View File

@ -16,7 +16,7 @@ struct worktree {
struct object_id head_oid; struct object_id head_oid;
int is_detached; int is_detached;
int is_bare; int is_bare;
int is_current; int is_current; /* does `path` match `repo->worktree` */
int lock_reason_valid; /* private */ int lock_reason_valid; /* private */
int prune_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. * 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); char *get_worktree_git_dir(const struct worktree *wt);