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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
e19488a60a
commit
dc7fb4f72c
27
refs.c
27
refs.c
@ -1960,9 +1960,6 @@ int repo_resolve_gitlink_ref(struct repository *r,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* A strmap of ref_stores, stored by worktree id: */
|
|
||||||
static struct strmap worktree_ref_stores;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Look up a ref store by name. If that ref_store hasn't been
|
* Look up a ref store by name. If that ref_store hasn't been
|
||||||
* registered yet, return NULL.
|
* registered yet, return NULL.
|
||||||
@ -2091,25 +2088,29 @@ struct ref_store *get_worktree_ref_store(const struct worktree *wt)
|
|||||||
const char *id;
|
const char *id;
|
||||||
|
|
||||||
if (wt->is_current)
|
if (wt->is_current)
|
||||||
return get_main_ref_store(the_repository);
|
return get_main_ref_store(wt->repo);
|
||||||
|
|
||||||
id = wt->id ? wt->id : "/";
|
id = wt->id ? wt->id : "/";
|
||||||
refs = lookup_ref_store_map(&worktree_ref_stores, id);
|
refs = lookup_ref_store_map(&wt->repo->worktree_ref_stores, id);
|
||||||
if (refs)
|
if (refs)
|
||||||
return refs;
|
return refs;
|
||||||
|
|
||||||
if (wt->id)
|
if (wt->id) {
|
||||||
refs = ref_store_init(the_repository,
|
struct strbuf common_path = STRBUF_INIT;
|
||||||
git_common_path("worktrees/%s", wt->id),
|
strbuf_git_common_path(&common_path, wt->repo,
|
||||||
|
"worktrees/%s", wt->id);
|
||||||
|
refs = ref_store_init(wt->repo, common_path.buf,
|
||||||
REF_STORE_ALL_CAPS);
|
REF_STORE_ALL_CAPS);
|
||||||
else
|
strbuf_release(&common_path);
|
||||||
refs = ref_store_init(the_repository,
|
} else {
|
||||||
get_git_common_dir(),
|
refs = ref_store_init(wt->repo, wt->repo->commondir,
|
||||||
REF_STORE_ALL_CAPS);
|
REF_STORE_ALL_CAPS);
|
||||||
|
}
|
||||||
|
|
||||||
if (refs)
|
if (refs)
|
||||||
register_ref_store_map(&worktree_ref_stores, "worktree",
|
register_ref_store_map(&wt->repo->worktree_ref_stores,
|
||||||
refs, id);
|
"worktree", refs, id);
|
||||||
|
|
||||||
return refs;
|
return refs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -337,6 +337,10 @@ void repo_clear(struct repository *repo)
|
|||||||
ref_store_release(e->value);
|
ref_store_release(e->value);
|
||||||
strmap_clear(&repo->submodule_ref_stores, 1);
|
strmap_clear(&repo->submodule_ref_stores, 1);
|
||||||
|
|
||||||
|
strmap_for_each_entry(&repo->worktree_ref_stores, &iter, e)
|
||||||
|
ref_store_release(e->value);
|
||||||
|
strmap_clear(&repo->worktree_ref_stores, 1);
|
||||||
|
|
||||||
repo_clear_path_cache(&repo->cached_paths);
|
repo_clear_path_cache(&repo->cached_paths);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,6 +116,12 @@ struct repository {
|
|||||||
*/
|
*/
|
||||||
struct strmap submodule_ref_stores;
|
struct strmap submodule_ref_stores;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* A strmap of ref_stores, stored by worktree id, accessible via
|
||||||
|
* `get_worktree_ref_store()`.
|
||||||
|
*/
|
||||||
|
struct strmap worktree_ref_stores;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Contains path to often used file names.
|
* Contains path to often used file names.
|
||||||
*/
|
*/
|
||||||
|
@ -65,6 +65,7 @@ static struct worktree *get_main_worktree(int skip_reading_head)
|
|||||||
strbuf_strip_suffix(&worktree_path, "/.git");
|
strbuf_strip_suffix(&worktree_path, "/.git");
|
||||||
|
|
||||||
CALLOC_ARRAY(worktree, 1);
|
CALLOC_ARRAY(worktree, 1);
|
||||||
|
worktree->repo = the_repository;
|
||||||
worktree->path = strbuf_detach(&worktree_path, NULL);
|
worktree->path = strbuf_detach(&worktree_path, NULL);
|
||||||
/*
|
/*
|
||||||
* NEEDSWORK: If this function is called from a secondary worktree and
|
* NEEDSWORK: If this function is called from a secondary worktree and
|
||||||
@ -98,6 +99,7 @@ struct worktree *get_linked_worktree(const char *id,
|
|||||||
strbuf_strip_suffix(&worktree_path, "/.git");
|
strbuf_strip_suffix(&worktree_path, "/.git");
|
||||||
|
|
||||||
CALLOC_ARRAY(worktree, 1);
|
CALLOC_ARRAY(worktree, 1);
|
||||||
|
worktree->repo = the_repository;
|
||||||
worktree->path = strbuf_detach(&worktree_path, NULL);
|
worktree->path = strbuf_detach(&worktree_path, NULL);
|
||||||
worktree->id = xstrdup(id);
|
worktree->id = xstrdup(id);
|
||||||
if (!skip_reading_head)
|
if (!skip_reading_head)
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
struct strbuf;
|
struct strbuf;
|
||||||
|
|
||||||
struct worktree {
|
struct worktree {
|
||||||
|
/* The repository this worktree belongs to. */
|
||||||
|
struct repository *repo;
|
||||||
char *path;
|
char *path;
|
||||||
char *id;
|
char *id;
|
||||||
char *head_ref; /* NULL if HEAD is broken or detached */
|
char *head_ref; /* NULL if HEAD is broken or detached */
|
||||||
|
Loading…
Reference in New Issue
Block a user