worktree.c: store "id" instead of "git_dir"
We can reconstruct git_dir from id quite easily. It's a bit hackier to do the reverse. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
15cdfea734
commit
69dfe3b942
3
branch.c
3
branch.c
@ -357,7 +357,8 @@ int replace_each_worktree_head_symref(const char *oldref, const char *newref)
|
|||||||
if (strcmp(oldref, worktrees[i]->head_ref))
|
if (strcmp(oldref, worktrees[i]->head_ref))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (set_worktree_head_symref(worktrees[i]->git_dir, newref)) {
|
if (set_worktree_head_symref(get_worktree_git_dir(worktrees[i]),
|
||||||
|
newref)) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
error(_("HEAD of working tree %s is not updated"),
|
error(_("HEAD of working tree %s is not updated"),
|
||||||
worktrees[i]->path);
|
worktrees[i]->path);
|
||||||
|
31
worktree.c
31
worktree.c
@ -9,7 +9,7 @@ void free_worktrees(struct worktree **worktrees)
|
|||||||
|
|
||||||
for (i = 0; worktrees[i]; i++) {
|
for (i = 0; worktrees[i]; i++) {
|
||||||
free(worktrees[i]->path);
|
free(worktrees[i]->path);
|
||||||
free(worktrees[i]->git_dir);
|
free(worktrees[i]->id);
|
||||||
free(worktrees[i]->head_ref);
|
free(worktrees[i]->head_ref);
|
||||||
free(worktrees[i]);
|
free(worktrees[i]);
|
||||||
}
|
}
|
||||||
@ -74,13 +74,11 @@ static struct worktree *get_main_worktree(void)
|
|||||||
struct worktree *worktree = NULL;
|
struct worktree *worktree = NULL;
|
||||||
struct strbuf path = STRBUF_INIT;
|
struct strbuf path = STRBUF_INIT;
|
||||||
struct strbuf worktree_path = STRBUF_INIT;
|
struct strbuf worktree_path = STRBUF_INIT;
|
||||||
struct strbuf gitdir = STRBUF_INIT;
|
|
||||||
struct strbuf head_ref = STRBUF_INIT;
|
struct strbuf head_ref = STRBUF_INIT;
|
||||||
int is_bare = 0;
|
int is_bare = 0;
|
||||||
int is_detached = 0;
|
int is_detached = 0;
|
||||||
|
|
||||||
strbuf_addf(&gitdir, "%s", absolute_path(get_git_common_dir()));
|
strbuf_addstr(&worktree_path, absolute_path(get_git_common_dir()));
|
||||||
strbuf_addbuf(&worktree_path, &gitdir);
|
|
||||||
is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
|
is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
|
||||||
if (is_bare)
|
if (is_bare)
|
||||||
strbuf_strip_suffix(&worktree_path, "/.");
|
strbuf_strip_suffix(&worktree_path, "/.");
|
||||||
@ -92,7 +90,7 @@ static struct worktree *get_main_worktree(void)
|
|||||||
|
|
||||||
worktree = xmalloc(sizeof(struct worktree));
|
worktree = xmalloc(sizeof(struct worktree));
|
||||||
worktree->path = strbuf_detach(&worktree_path, NULL);
|
worktree->path = strbuf_detach(&worktree_path, NULL);
|
||||||
worktree->git_dir = strbuf_detach(&gitdir, NULL);
|
worktree->id = NULL;
|
||||||
worktree->is_bare = is_bare;
|
worktree->is_bare = is_bare;
|
||||||
worktree->head_ref = NULL;
|
worktree->head_ref = NULL;
|
||||||
worktree->is_detached = is_detached;
|
worktree->is_detached = is_detached;
|
||||||
@ -100,7 +98,6 @@ static struct worktree *get_main_worktree(void)
|
|||||||
|
|
||||||
done:
|
done:
|
||||||
strbuf_release(&path);
|
strbuf_release(&path);
|
||||||
strbuf_release(&gitdir);
|
|
||||||
strbuf_release(&worktree_path);
|
strbuf_release(&worktree_path);
|
||||||
strbuf_release(&head_ref);
|
strbuf_release(&head_ref);
|
||||||
return worktree;
|
return worktree;
|
||||||
@ -111,16 +108,13 @@ static struct worktree *get_linked_worktree(const char *id)
|
|||||||
struct worktree *worktree = NULL;
|
struct worktree *worktree = NULL;
|
||||||
struct strbuf path = STRBUF_INIT;
|
struct strbuf path = STRBUF_INIT;
|
||||||
struct strbuf worktree_path = STRBUF_INIT;
|
struct strbuf worktree_path = STRBUF_INIT;
|
||||||
struct strbuf gitdir = STRBUF_INIT;
|
|
||||||
struct strbuf head_ref = STRBUF_INIT;
|
struct strbuf head_ref = STRBUF_INIT;
|
||||||
int is_detached = 0;
|
int is_detached = 0;
|
||||||
|
|
||||||
if (!id)
|
if (!id)
|
||||||
die("Missing linked worktree name");
|
die("Missing linked worktree name");
|
||||||
|
|
||||||
strbuf_addf(&gitdir, "%s/worktrees/%s",
|
strbuf_git_common_path(&path, "worktrees/%s/gitdir", id);
|
||||||
absolute_path(get_git_common_dir()), id);
|
|
||||||
strbuf_addf(&path, "%s/gitdir", gitdir.buf);
|
|
||||||
if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
|
if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
|
||||||
/* invalid gitdir file */
|
/* invalid gitdir file */
|
||||||
goto done;
|
goto done;
|
||||||
@ -140,7 +134,7 @@ static struct worktree *get_linked_worktree(const char *id)
|
|||||||
|
|
||||||
worktree = xmalloc(sizeof(struct worktree));
|
worktree = xmalloc(sizeof(struct worktree));
|
||||||
worktree->path = strbuf_detach(&worktree_path, NULL);
|
worktree->path = strbuf_detach(&worktree_path, NULL);
|
||||||
worktree->git_dir = strbuf_detach(&gitdir, NULL);
|
worktree->id = xstrdup(id);
|
||||||
worktree->is_bare = 0;
|
worktree->is_bare = 0;
|
||||||
worktree->head_ref = NULL;
|
worktree->head_ref = NULL;
|
||||||
worktree->is_detached = is_detached;
|
worktree->is_detached = is_detached;
|
||||||
@ -148,7 +142,6 @@ static struct worktree *get_linked_worktree(const char *id)
|
|||||||
|
|
||||||
done:
|
done:
|
||||||
strbuf_release(&path);
|
strbuf_release(&path);
|
||||||
strbuf_release(&gitdir);
|
|
||||||
strbuf_release(&worktree_path);
|
strbuf_release(&worktree_path);
|
||||||
strbuf_release(&head_ref);
|
strbuf_release(&head_ref);
|
||||||
return worktree;
|
return worktree;
|
||||||
@ -188,6 +181,16 @@ struct worktree **get_worktrees(void)
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *get_worktree_git_dir(const struct worktree *wt)
|
||||||
|
{
|
||||||
|
if (!wt)
|
||||||
|
return get_git_dir();
|
||||||
|
else if (!wt->id)
|
||||||
|
return get_git_common_dir();
|
||||||
|
else
|
||||||
|
return git_common_path("worktrees/%s", wt->id);
|
||||||
|
}
|
||||||
|
|
||||||
char *find_shared_symref(const char *symref, const char *target)
|
char *find_shared_symref(const char *symref, const char *target)
|
||||||
{
|
{
|
||||||
char *existing = NULL;
|
char *existing = NULL;
|
||||||
@ -199,7 +202,9 @@ char *find_shared_symref(const char *symref, const char *target)
|
|||||||
for (i = 0; worktrees[i]; i++) {
|
for (i = 0; worktrees[i]; i++) {
|
||||||
strbuf_reset(&path);
|
strbuf_reset(&path);
|
||||||
strbuf_reset(&sb);
|
strbuf_reset(&sb);
|
||||||
strbuf_addf(&path, "%s/%s", worktrees[i]->git_dir, symref);
|
strbuf_addf(&path, "%s/%s",
|
||||||
|
get_worktree_git_dir(worktrees[i]),
|
||||||
|
symref);
|
||||||
|
|
||||||
if (parse_ref(path.buf, &sb, NULL)) {
|
if (parse_ref(path.buf, &sb, NULL)) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
struct worktree {
|
struct worktree {
|
||||||
char *path;
|
char *path;
|
||||||
char *git_dir;
|
char *id;
|
||||||
char *head_ref;
|
char *head_ref;
|
||||||
unsigned char head_sha1[20];
|
unsigned char head_sha1[20];
|
||||||
int is_detached;
|
int is_detached;
|
||||||
@ -22,6 +22,12 @@ struct worktree {
|
|||||||
*/
|
*/
|
||||||
extern struct worktree **get_worktrees(void);
|
extern struct worktree **get_worktrees(void);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return git dir of the worktree. Note that the path may be relative.
|
||||||
|
* If wt is NULL, git dir of current worktree is returned.
|
||||||
|
*/
|
||||||
|
extern const char *get_worktree_git_dir(const struct worktree *wt);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Free up the memory for worktree(s)
|
* Free up the memory for worktree(s)
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user