refs: introduce get_worktree_ref_store()

files-backend at this point is still aware of the per-repo/worktree separation in refs, so it can handle a linked worktree. Some refs operations are known not working when current files-backend is used in a linked worktree (e.g. reflog). Tests will be written when refs_* functions start to be called with worktree backend to verify that they work as expected. Note: accessing a worktree of a submodule remains unaddressed. Perhaps after get_worktrees() can access submodule (or rather a new function get_submodule_worktrees(), that lists worktrees of a submodule), we can update this function to work with submodules as well. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 24, 2017 at 17:01 UTC 17eff96b83be5c4c25e33a40e632d1b55c506d33
2 files changed +32
refs.c
+30
@@ -10,6 +10,7 @@
10 #include "object.h"
11 #include "tag.h"
12 #include "submodule.h"
13 +#include "worktree.h"
14
15 /*
16 * List of all available backends
@@ -1486,6 +1487,9 @@ static struct ref_store *main_ref_store;
1487 /* A hashmap of ref_stores, stored by submodule name: */
1488 static struct hashmap submodule_ref_stores;
1489
1490 +/* A hashmap of ref_stores, stored by worktree id: */
1491 +static struct hashmap worktree_ref_stores;
1492 +
1493 /*
1494 * Look up a ref store by name. If that ref_store hasn't been
1495 * registered yet, return NULL.
@@ -1586,6 +1590,32 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
1590 return refs;
1591 }
1592
1593 +struct ref_store *get_worktree_ref_store(const struct worktree *wt)
1594 +{
1595 + struct ref_store *refs;
1596 + const char *id;
1597 +
1598 + if (wt->is_current)
1599 + return get_main_ref_store();
1600 +
1601 + id = wt->id ? wt->id : "/";
1602 + refs = lookup_ref_store_map(&worktree_ref_stores, id);
1603 + if (refs)
1604 + return refs;
1605 +
1606 + if (wt->id)
1607 + refs = ref_store_init(git_common_path("worktrees/%s", wt->id),
1608 + REF_STORE_ALL_CAPS);
1609 + else
1610 + refs = ref_store_init(get_git_common_dir(),
1611 + REF_STORE_ALL_CAPS);
1612 +
1613 + if (refs)
1614 + register_ref_store_map(&worktree_ref_stores, "worktree",
1615 + refs, id);
1616 + return refs;
1617 +}
1618 +
1619 void base_ref_store_init(struct ref_store *refs,
1620 const struct ref_storage_be *be)
1621 {
refs.h
+2
@@ -5,6 +5,7 @@ struct object_id;
5 struct ref_store;
6 struct strbuf;
7 struct string_list;
8 +struct worktree;
9
10 /*
11 * Resolve a reference, recursively following symbolic refererences.
@@ -655,5 +656,6 @@ struct ref_store *get_main_ref_store(void);
656 * submodule==NULL.
657 */
658 struct ref_store *get_submodule_ref_store(const char *submodule);
659 +struct ref_store *get_worktree_ref_store(const struct worktree *wt);
660
661 #endif /* REFS_H */