Commit 72094582 authored by Duy Nguyen's avatar Duy Nguyen

refs.c: remove the_repo from get_worktree_ref_store()

This will allow us access to a specific worktree of any
repository (i.e. a submodule, most likely). Also one less global variable to
kill.
parent d6a25a10
......@@ -377,7 +377,7 @@ int replace_each_worktree_head_symref(const char *oldref, const char *newref,
if (strcmp(oldref, worktrees[i]->head_ref))
continue;
refs = get_worktree_ref_store(worktrees[i]);
refs = get_worktree_ref_store(the_repository, worktrees[i]);
if (refs_create_symref(refs, "HEAD", newref, logmsg))
ret = error(_("HEAD of working tree %s is not updated"),
worktrees[i]->path);
......
......@@ -598,7 +598,7 @@ static void get_default_heads(void)
strbuf_release(&ref);
if (include_reflogs)
refs_for_each_reflog(get_worktree_ref_store(wt),
refs_for_each_reflog(get_worktree_ref_store(the_repository, wt),
fsck_handle_reflog, wt);
}
free_worktrees(worktrees);
......
......@@ -619,7 +619,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
if (!all_worktrees && !(*p)->is_current)
continue;
collected.wt = *p;
refs_for_each_reflog(get_worktree_ref_store(*p),
refs_for_each_reflog(get_worktree_ref_store(the_repository, *p),
collect_reflog, &collected);
}
free_worktrees(worktrees);
......
......@@ -1703,9 +1703,6 @@ static struct ref_store_hash_entry *alloc_ref_store_hash_entry(
/* A hashmap of ref_stores, stored by submodule name: */
static struct hashmap submodule_ref_stores;
/* A hashmap of ref_stores, stored by worktree id: */
static struct hashmap worktree_ref_stores;
/*
* Look up a ref store by name. If that ref_store hasn't been
* registered yet, return NULL.
......@@ -1813,28 +1810,32 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
return refs;
}
struct ref_store *get_worktree_ref_store(const struct worktree *wt)
struct ref_store *get_worktree_ref_store(struct repository *r,
const struct worktree *wt)
{
struct ref_store *refs;
const char *id;
if (wt->is_current)
return get_main_ref_store(the_repository);
return get_main_ref_store(r);
if (!r->worktree_ref_stores)
r->worktree_ref_stores = xcalloc(1, sizeof(*r->worktree_ref_stores));
id = wt->id ? wt->id : "/";
refs = lookup_ref_store_map(&worktree_ref_stores, id);
refs = lookup_ref_store_map(r->worktree_ref_stores, id);
if (refs)
return refs;
if (wt->id)
refs = ref_store_init(git_common_path("worktrees/%s", wt->id),
refs = ref_store_init(repo_git_common_path(r, "worktrees/%s", wt->id),
REF_STORE_ALL_CAPS);
else
refs = ref_store_init(get_git_common_dir(),
refs = ref_store_init(r->commondir,
REF_STORE_ALL_CAPS);
if (refs)
register_ref_store_map(&worktree_ref_stores, "worktree",
register_ref_store_map(r->worktree_ref_stores, "worktree",
refs, id);
return refs;
}
......
......@@ -791,6 +791,7 @@ struct ref_store *get_main_ref_store(struct repository *r);
* submodule==NULL.
*/
struct ref_store *get_submodule_ref_store(const char *submodule);
struct ref_store *get_worktree_ref_store(const struct worktree *wt);
struct ref_store *get_worktree_ref_store(struct repository *r,
const struct worktree *wt);
#endif /* REFS_H */
......@@ -252,6 +252,11 @@ void repo_clear(struct repository *repo)
repo->submodule_cache = NULL;
}
if (repo->worktree_ref_stores) {
hashmap_free(repo->worktree_ref_stores, 0);
FREE_AND_NULL(repo->worktree_ref_stores);
}
if (repo->index) {
discard_index(repo->index);
if (repo->index != &the_index)
......
......@@ -5,6 +5,7 @@
struct config_set;
struct git_hash_algo;
struct hashmap;
struct index_state;
struct lock_file;
struct pathspec;
......@@ -89,6 +90,9 @@ struct repository {
*/
struct index_state *index;
/* A hashmap of ref_stores, stored by worktree id */
struct hashmap *worktree_ref_stores;
/* Repository's current hash algorithm, as serialized on disk. */
const struct git_hash_algo *hash_algo;
......
......@@ -1450,12 +1450,14 @@ static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
worktrees = get_worktrees(0);
for (p = worktrees; *p; p++) {
struct worktree *wt = *p;
struct ref_store *refs;
if (wt->is_current)
continue;
refs = get_worktree_ref_store(the_repository, wt);
cb->wt = wt;
refs_for_each_reflog(get_worktree_ref_store(wt),
refs_for_each_reflog(refs,
handle_one_reflog,
cb);
}
......
......@@ -52,7 +52,7 @@ static const char **get_store(const char **argv, struct ref_store **refs)
if (!*p)
die("no such worktree: %s", gitdir);
*refs = get_worktree_ref_store(*p);
*refs = get_worktree_ref_store(the_repository, *p);
} else
die("unknown backend %s", argv[0]);
......
......@@ -28,7 +28,7 @@ static void add_head_info(struct worktree *wt)
int flags;
const char *target;
target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
target = refs_resolve_ref_unsafe(get_worktree_ref_store(the_repository, wt),
"HEAD",
0,
&wt->head_oid, &flags);
......@@ -424,7 +424,7 @@ const struct worktree *find_shared_symref(const char *symref,
}
}
refs = get_worktree_ref_store(wt);
refs = get_worktree_ref_store(the_repository, wt);
symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
NULL, &flags);
if ((flags & REF_ISSYMREF) &&
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment