Skip to content

Commit ac4ded1

Browse files
committed
Merge branch 'pw/worktree-reduce-the-repository' into seen
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()
2 parents 966ef5f + 7d33b82 commit ac4ded1

File tree

4 files changed

+19
-41
lines changed

4 files changed

+19
-41
lines changed

builtin/worktree.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -692,25 +692,8 @@ static int can_use_local_refs(const struct add_opts *opts)
692692
if (refs_head_ref(get_main_ref_store(the_repository), first_valid_ref, NULL)) {
693693
return 1;
694694
} else if (refs_for_each_branch_ref(get_main_ref_store(the_repository), first_valid_ref, NULL)) {
695-
if (!opts->quiet) {
696-
struct strbuf path = STRBUF_INIT;
697-
struct strbuf contents = STRBUF_INIT;
698-
char *wt_gitdir = get_worktree_git_dir(NULL);
699-
700-
strbuf_add_real_path(&path, wt_gitdir);
701-
strbuf_addstr(&path, "/HEAD");
702-
strbuf_read_file(&contents, path.buf, 64);
703-
strbuf_stripspace(&contents, NULL);
704-
strbuf_strip_suffix(&contents, "\n");
705-
706-
warning(_("HEAD points to an invalid (or orphaned) reference.\n"
707-
"HEAD path: '%s'\n"
708-
"HEAD contents: '%s'"),
709-
path.buf, contents.buf);
710-
strbuf_release(&path);
711-
strbuf_release(&contents);
712-
free(wt_gitdir);
713-
}
695+
if (!opts->quiet)
696+
warning(_("HEAD points to an invalid (or orphaned) reference.\n"));
714697
return 1;
715698
}
716699
return 0;

t/t2400-worktree-add.sh

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ test_dwim_orphan () {
987987
then
988988
test_must_be_empty actual
989989
else
990-
grep "$info_text" actual
990+
test_grep "$info_text" actual
991991
fi
992992
elif [ "$outcome" = "no_infer" ]
993993
then
@@ -996,39 +996,35 @@ test_dwim_orphan () {
996996
then
997997
test_must_be_empty actual
998998
else
999-
! grep "$info_text" actual
999+
test_grep ! "$info_text" actual
10001000
fi
10011001
elif [ "$outcome" = "fetch_error" ]
10021002
then
10031003
test_must_fail git $dashc_args worktree add $args 2>actual &&
1004-
grep "$fetch_error_text" actual
1004+
test_grep "$fetch_error_text" actual
10051005
elif [ "$outcome" = "fatal_orphan_bad_combo" ]
10061006
then
10071007
test_must_fail git $dashc_args worktree add $args 2>actual &&
10081008
if [ $use_quiet -eq 1 ]
10091009
then
1010-
! grep "$info_text" actual
1010+
test_grep ! "$info_text" actual
10111011
else
1012-
grep "$info_text" actual
1012+
test_grep "$info_text" actual
10131013
fi &&
1014-
grep "$bad_combo_regex" actual
1014+
test_grep "$bad_combo_regex" actual
10151015
elif [ "$outcome" = "warn_bad_head" ]
10161016
then
10171017
test_must_fail git $dashc_args worktree add $args 2>actual &&
10181018
if [ $use_quiet -eq 1 ]
10191019
then
1020-
grep "$invalid_ref_regex" actual &&
1021-
! grep "$orphan_hint" actual
1020+
test_grep "$invalid_ref_regex" actual &&
1021+
test_grep ! "$orphan_hint" actual
10221022
else
1023-
headpath=$(git $dashc_args rev-parse --path-format=absolute --git-path HEAD) &&
1024-
headcontents=$(cat "$headpath") &&
1025-
grep "HEAD points to an invalid (or orphaned) reference" actual &&
1026-
grep "HEAD path: .$headpath." actual &&
1027-
grep "HEAD contents: .$headcontents." actual &&
1028-
grep "$orphan_hint" actual &&
1029-
! grep "$info_text" actual
1023+
test_grep "HEAD points to an invalid (or orphaned) reference" actual &&
1024+
test_grep "$orphan_hint" actual &&
1025+
test_grep ! "$info_text" actual
10301026
fi &&
1031-
grep "$invalid_ref_regex" actual
1027+
test_grep "$invalid_ref_regex" actual
10321028
else
10331029
# Unreachable
10341030
false

worktree.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static void add_head_info(struct worktree *wt)
5858

5959
static int is_current_worktree(struct worktree *wt)
6060
{
61-
char *git_dir = absolute_pathdup(repo_get_git_dir(the_repository));
61+
char *git_dir = absolute_pathdup(repo_get_git_dir(wt->repo));
6262
char *wt_git_dir = get_worktree_git_dir(wt);
6363
int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir));
6464
free(wt_git_dir);
@@ -78,7 +78,7 @@ struct worktree *get_worktree_from_repository(struct repository *repo)
7878
wt->is_bare = !repo->worktree;
7979
if (fspathcmp(gitdir, commondir))
8080
wt->id = xstrdup(find_last_dir_sep(gitdir) + 1);
81-
wt->is_current = is_current_worktree(wt);
81+
wt->is_current = true;
8282
add_head_info(wt);
8383

8484
free(gitdir);
@@ -227,11 +227,11 @@ struct worktree **get_worktrees_without_reading_head(void)
227227
char *get_worktree_git_dir(const struct worktree *wt)
228228
{
229229
if (!wt)
230-
return xstrdup(repo_get_git_dir(the_repository));
230+
BUG("%s() called with NULL worktree", __func__);
231231
else if (!wt->id)
232-
return xstrdup(repo_get_common_dir(the_repository));
232+
return xstrdup(repo_get_common_dir(wt->repo));
233233
else
234-
return repo_common_path(the_repository, "worktrees/%s", wt->id);
234+
return repo_common_path(wt->repo, "worktrees/%s", wt->id);
235235
}
236236

237237
static struct worktree *find_worktree_by_suffix(struct worktree **list,

worktree.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ int submodule_uses_worktrees(const char *path);
5151

5252
/*
5353
* Return git dir of the worktree. Note that the path may be relative.
54-
* If wt is NULL, git dir of current worktree is returned.
5554
*/
5655
char *get_worktree_git_dir(const struct worktree *wt);
5756

0 commit comments

Comments
 (0)