Skip to content

Commit 1339cb3

Browse files
pks-tgitster
authored andcommitted
worktree: don't store main worktree twice
In `get_worktree_ref_store()` we either return the repository's main ref store, or we look up the ref store via the map of worktree ref stores. Which of these worktrees gets picked depends on the `is_current` bit of the worktree, which indicates whether the worktree is the one that corresponds to `the_repository`. The bit is getting set in `get_worktrees()`, but only after we have computed the list of all worktrees. This is too late though, because at that time we have already called `get_worktree_ref_store()` on each of the worktrees via `add_head_info()`. The consequence is that the current worktree will not have been marked accordingly, which means that we did not use the main ref store, but instead created a new ref store. We thus have two separate ref stores now that map to the same ref database. Fix this by setting `is_current` before we call `add_head_info()`. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b5d7db9 commit 1339cb3

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

worktree.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ static void add_head_info(struct worktree *wt)
5353
wt->is_detached = 1;
5454
}
5555

56+
static int is_current_worktree(struct worktree *wt)
57+
{
58+
char *git_dir = absolute_pathdup(get_git_dir());
59+
const char *wt_git_dir = get_worktree_git_dir(wt);
60+
int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir));
61+
free(git_dir);
62+
return is_current;
63+
}
64+
5665
/**
5766
* get the main worktree
5867
*/
@@ -76,6 +85,7 @@ static struct worktree *get_main_worktree(int skip_reading_head)
7685
*/
7786
worktree->is_bare = (is_bare_repository_cfg == 1) ||
7887
is_bare_repository();
88+
worktree->is_current = is_current_worktree(worktree);
7989
if (!skip_reading_head)
8090
add_head_info(worktree);
8191
return worktree;
@@ -102,6 +112,7 @@ struct worktree *get_linked_worktree(const char *id,
102112
worktree->repo = the_repository;
103113
worktree->path = strbuf_detach(&worktree_path, NULL);
104114
worktree->id = xstrdup(id);
115+
worktree->is_current = is_current_worktree(worktree);
105116
if (!skip_reading_head)
106117
add_head_info(worktree);
107118

@@ -111,23 +122,6 @@ struct worktree *get_linked_worktree(const char *id,
111122
return worktree;
112123
}
113124

114-
static void mark_current_worktree(struct worktree **worktrees)
115-
{
116-
char *git_dir = absolute_pathdup(get_git_dir());
117-
int i;
118-
119-
for (i = 0; worktrees[i]; i++) {
120-
struct worktree *wt = worktrees[i];
121-
const char *wt_git_dir = get_worktree_git_dir(wt);
122-
123-
if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
124-
wt->is_current = 1;
125-
break;
126-
}
127-
}
128-
free(git_dir);
129-
}
130-
131125
/*
132126
* NEEDSWORK: This function exists so that we can look up metadata of a
133127
* worktree without trying to access any of its internals like the refdb. It
@@ -164,7 +158,6 @@ static struct worktree **get_worktrees_internal(int skip_reading_head)
164158
ALLOC_GROW(list, counter + 1, alloc);
165159
list[counter] = NULL;
166160

167-
mark_current_worktree(list);
168161
return list;
169162
}
170163

0 commit comments

Comments
 (0)