Skip to content

Commit 17eff96

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0d8a814 commit 17eff96

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

refs.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "object.h"
1111
#include "tag.h"
1212
#include "submodule.h"
13+
#include "worktree.h"
1314

1415
/*
1516
* List of all available backends
@@ -1486,6 +1487,9 @@ static struct ref_store *main_ref_store;
14861487
/* A hashmap of ref_stores, stored by submodule name: */
14871488
static struct hashmap submodule_ref_stores;
14881489

1490+
/* A hashmap of ref_stores, stored by worktree id: */
1491+
static struct hashmap worktree_ref_stores;
1492+
14891493
/*
14901494
* Look up a ref store by name. If that ref_store hasn't been
14911495
* registered yet, return NULL.
@@ -1586,6 +1590,32 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
15861590
return refs;
15871591
}
15881592

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+
15891619
void base_ref_store_init(struct ref_store *refs,
15901620
const struct ref_storage_be *be)
15911621
{

refs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ struct object_id;
55
struct ref_store;
66
struct strbuf;
77
struct string_list;
8+
struct worktree;
89

910
/*
1011
* Resolve a reference, recursively following symbolic refererences.
@@ -655,5 +656,6 @@ struct ref_store *get_main_ref_store(void);
655656
* submodule==NULL.
656657
*/
657658
struct ref_store *get_submodule_ref_store(const char *submodule);
659+
struct ref_store *get_worktree_ref_store(const struct worktree *wt);
658660

659661
#endif /* REFS_H */

0 commit comments

Comments
 (0)