Skip to content

Commit dc7fb4f

Browse files
pks-tgitster
authored andcommitted
refs: retrieve worktree ref stores via associated repository
Similar as with the preceding commit, the worktree ref stores are always looked up via `the_repository`. Also, again, those ref stores are stored in a global map. Refactor the code so that worktrees have a pointer to their repository. Like this, we can move the global map into `struct repository` and stop using `the_repository`. With this change, we can now in theory look up worktree ref stores for repositories other than `the_repository`. In practice, the worktree code will need further changes to look up arbitrary worktrees. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e19488a commit dc7fb4f

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

refs.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,9 +1960,6 @@ int repo_resolve_gitlink_ref(struct repository *r,
19601960
return 0;
19611961
}
19621962

1963-
/* A strmap of ref_stores, stored by worktree id: */
1964-
static struct strmap worktree_ref_stores;
1965-
19661963
/*
19671964
* Look up a ref store by name. If that ref_store hasn't been
19681965
* registered yet, return NULL.
@@ -2091,25 +2088,29 @@ struct ref_store *get_worktree_ref_store(const struct worktree *wt)
20912088
const char *id;
20922089

20932090
if (wt->is_current)
2094-
return get_main_ref_store(the_repository);
2091+
return get_main_ref_store(wt->repo);
20952092

20962093
id = wt->id ? wt->id : "/";
2097-
refs = lookup_ref_store_map(&worktree_ref_stores, id);
2094+
refs = lookup_ref_store_map(&wt->repo->worktree_ref_stores, id);
20982095
if (refs)
20992096
return refs;
21002097

2101-
if (wt->id)
2102-
refs = ref_store_init(the_repository,
2103-
git_common_path("worktrees/%s", wt->id),
2098+
if (wt->id) {
2099+
struct strbuf common_path = STRBUF_INIT;
2100+
strbuf_git_common_path(&common_path, wt->repo,
2101+
"worktrees/%s", wt->id);
2102+
refs = ref_store_init(wt->repo, common_path.buf,
21042103
REF_STORE_ALL_CAPS);
2105-
else
2106-
refs = ref_store_init(the_repository,
2107-
get_git_common_dir(),
2104+
strbuf_release(&common_path);
2105+
} else {
2106+
refs = ref_store_init(wt->repo, wt->repo->commondir,
21082107
REF_STORE_ALL_CAPS);
2108+
}
21092109

21102110
if (refs)
2111-
register_ref_store_map(&worktree_ref_stores, "worktree",
2112-
refs, id);
2111+
register_ref_store_map(&wt->repo->worktree_ref_stores,
2112+
"worktree", refs, id);
2113+
21132114
return refs;
21142115
}
21152116

repository.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ void repo_clear(struct repository *repo)
337337
ref_store_release(e->value);
338338
strmap_clear(&repo->submodule_ref_stores, 1);
339339

340+
strmap_for_each_entry(&repo->worktree_ref_stores, &iter, e)
341+
ref_store_release(e->value);
342+
strmap_clear(&repo->worktree_ref_stores, 1);
343+
340344
repo_clear_path_cache(&repo->cached_paths);
341345
}
342346

repository.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ struct repository {
116116
*/
117117
struct strmap submodule_ref_stores;
118118

119+
/*
120+
* A strmap of ref_stores, stored by worktree id, accessible via
121+
* `get_worktree_ref_store()`.
122+
*/
123+
struct strmap worktree_ref_stores;
124+
119125
/*
120126
* Contains path to often used file names.
121127
*/

worktree.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ static struct worktree *get_main_worktree(int skip_reading_head)
6565
strbuf_strip_suffix(&worktree_path, "/.git");
6666

6767
CALLOC_ARRAY(worktree, 1);
68+
worktree->repo = the_repository;
6869
worktree->path = strbuf_detach(&worktree_path, NULL);
6970
/*
7071
* NEEDSWORK: If this function is called from a secondary worktree and
@@ -98,6 +99,7 @@ struct worktree *get_linked_worktree(const char *id,
9899
strbuf_strip_suffix(&worktree_path, "/.git");
99100

100101
CALLOC_ARRAY(worktree, 1);
102+
worktree->repo = the_repository;
101103
worktree->path = strbuf_detach(&worktree_path, NULL);
102104
worktree->id = xstrdup(id);
103105
if (!skip_reading_head)

worktree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
struct strbuf;
77

88
struct worktree {
9+
/* The repository this worktree belongs to. */
10+
struct repository *repo;
911
char *path;
1012
char *id;
1113
char *head_ref; /* NULL if HEAD is broken or detached */

0 commit comments

Comments
 (0)