Commit 9be095a6 authored by Duy Nguyen's avatar Duy Nguyen

worktree.c: add repo_get_worktrees()

parent dd70695f
......@@ -23,12 +23,12 @@ void free_worktrees(struct worktree **worktrees)
/**
* Update head_sha1, head_ref and is_detached of the given worktree
*/
static void add_head_info(struct worktree *wt)
static void add_head_info(struct repository *r, struct worktree *wt)
{
int flags;
const char *target;
target = refs_resolve_ref_unsafe(get_worktree_ref_store(the_repository, wt),
target = refs_resolve_ref_unsafe(get_worktree_ref_store(r, wt),
"HEAD",
0,
&wt->head_oid, &flags);
......@@ -44,31 +44,32 @@ static void add_head_info(struct worktree *wt)
/**
* get the main worktree
*/
static struct worktree *get_main_worktree(void)
static struct worktree *get_main_worktree(struct repository *r)
{
struct worktree *worktree = NULL;
struct strbuf path = STRBUF_INIT;
struct strbuf worktree_path = STRBUF_INIT;
int is_bare = 0;
strbuf_add_absolute_path(&worktree_path, get_git_common_dir());
strbuf_add_absolute_path(&worktree_path, r->commondir);
is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
if (is_bare)
strbuf_strip_suffix(&worktree_path, "/.");
strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
strbuf_addf(&path, "%s/HEAD", r->commondir);
worktree = xcalloc(1, sizeof(*worktree));
worktree->path = strbuf_detach(&worktree_path, NULL);
worktree->is_bare = is_bare;
add_head_info(worktree);
add_head_info(r, worktree);
strbuf_release(&path);
strbuf_release(&worktree_path);
return worktree;
}
static struct worktree *get_linked_worktree(const char *id)
static struct worktree *get_linked_worktree(struct repository *r,
const char *id)
{
struct worktree *worktree = NULL;
struct strbuf path = STRBUF_INIT;
......@@ -77,7 +78,7 @@ static struct worktree *get_linked_worktree(const char *id)
if (!id)
die("Missing linked worktree name");
strbuf_git_common_path(&path, the_repository, "worktrees/%s/gitdir", id);
strbuf_git_common_path(&path, r, "worktrees/%s/gitdir", id);
if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
/* invalid gitdir file */
goto done;
......@@ -90,12 +91,12 @@ static struct worktree *get_linked_worktree(const char *id)
}
strbuf_reset(&path);
strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id);
strbuf_addf(&path, "%s/worktrees/%s/HEAD", r->commondir, id);
worktree = xcalloc(1, sizeof(*worktree));
worktree->path = strbuf_detach(&worktree_path, NULL);
worktree->id = xstrdup(id);
add_head_info(worktree);
add_head_info(r, worktree);
done:
strbuf_release(&path);
......@@ -103,14 +104,15 @@ static struct worktree *get_linked_worktree(const char *id)
return worktree;
}
static void mark_current_worktree(struct worktree **worktrees)
static void mark_current_worktree(struct repository *r,
struct worktree **worktrees)
{
char *git_dir = absolute_pathdup(get_git_dir());
char *git_dir = absolute_pathdup(r->gitdir);
int i;
for (i = 0; worktrees[i]; i++) {
struct worktree *wt = worktrees[i];
const char *wt_git_dir = get_worktree_git_dir(the_repository, wt);
const char *wt_git_dir = get_worktree_git_dir(r, wt);
if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
wt->is_current = 1;
......@@ -127,7 +129,7 @@ static int compare_worktree(const void *a_, const void *b_)
return fspathcmp((*a)->path, (*b)->path);
}
struct worktree **get_worktrees(unsigned flags)
struct worktree **repo_get_worktrees(struct repository *r, unsigned flags)
{
struct worktree **list = NULL;
struct strbuf path = STRBUF_INIT;
......@@ -137,9 +139,9 @@ struct worktree **get_worktrees(unsigned flags)
ALLOC_ARRAY(list, alloc);
list[counter++] = get_main_worktree();
list[counter++] = get_main_worktree(r);
strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
strbuf_addf(&path, "%s/worktrees", r->commondir);
dir = opendir(path.buf);
strbuf_release(&path);
if (dir) {
......@@ -148,7 +150,7 @@ struct worktree **get_worktrees(unsigned flags)
if (is_dot_or_dotdot(d->d_name))
continue;
if ((linked = get_linked_worktree(d->d_name))) {
if ((linked = get_linked_worktree(r, d->d_name))) {
ALLOC_GROW(list, counter + 1, alloc);
list[counter++] = linked;
}
......@@ -165,7 +167,7 @@ struct worktree **get_worktrees(unsigned flags)
*/
QSORT(list + 1, counter - 1, compare_worktree);
mark_current_worktree(list);
mark_current_worktree(r, list);
return list;
}
......
......@@ -31,7 +31,8 @@ struct worktree {
* The caller is responsible for freeing the memory from the returned
* worktree(s).
*/
extern struct worktree **get_worktrees(unsigned flags);
struct worktree **repo_get_worktrees(struct repository *r, unsigned flags);
#define get_worktrees(flags) repo_get_worktrees(the_repository, flags)
/*
* Returns 1 if linked worktrees exist, 0 otherwise.
......
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