Skip to content

Commit 69dfe3b

Browse files
pcloudsgitster
authored andcommitted
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>
1 parent 15cdfea commit 69dfe3b

3 files changed

Lines changed: 27 additions & 15 deletions

File tree

branch.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@ int replace_each_worktree_head_symref(const char *oldref, const char *newref)
357357
if (strcmp(oldref, worktrees[i]->head_ref))
358358
continue;
359359

360-
if (set_worktree_head_symref(worktrees[i]->git_dir, newref)) {
360+
if (set_worktree_head_symref(get_worktree_git_dir(worktrees[i]),
361+
newref)) {
361362
ret = -1;
362363
error(_("HEAD of working tree %s is not updated"),
363364
worktrees[i]->path);

worktree.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void free_worktrees(struct worktree **worktrees)
99

1010
for (i = 0; worktrees[i]; i++) {
1111
free(worktrees[i]->path);
12-
free(worktrees[i]->git_dir);
12+
free(worktrees[i]->id);
1313
free(worktrees[i]->head_ref);
1414
free(worktrees[i]);
1515
}
@@ -74,13 +74,11 @@ static struct worktree *get_main_worktree(void)
7474
struct worktree *worktree = NULL;
7575
struct strbuf path = STRBUF_INIT;
7676
struct strbuf worktree_path = STRBUF_INIT;
77-
struct strbuf gitdir = STRBUF_INIT;
7877
struct strbuf head_ref = STRBUF_INIT;
7978
int is_bare = 0;
8079
int is_detached = 0;
8180

82-
strbuf_addf(&gitdir, "%s", absolute_path(get_git_common_dir()));
83-
strbuf_addbuf(&worktree_path, &gitdir);
81+
strbuf_addstr(&worktree_path, absolute_path(get_git_common_dir()));
8482
is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
8583
if (is_bare)
8684
strbuf_strip_suffix(&worktree_path, "/.");
@@ -92,15 +90,14 @@ static struct worktree *get_main_worktree(void)
9290

9391
worktree = xmalloc(sizeof(struct worktree));
9492
worktree->path = strbuf_detach(&worktree_path, NULL);
95-
worktree->git_dir = strbuf_detach(&gitdir, NULL);
93+
worktree->id = NULL;
9694
worktree->is_bare = is_bare;
9795
worktree->head_ref = NULL;
9896
worktree->is_detached = is_detached;
9997
add_head_info(&head_ref, worktree);
10098

10199
done:
102100
strbuf_release(&path);
103-
strbuf_release(&gitdir);
104101
strbuf_release(&worktree_path);
105102
strbuf_release(&head_ref);
106103
return worktree;
@@ -111,16 +108,13 @@ static struct worktree *get_linked_worktree(const char *id)
111108
struct worktree *worktree = NULL;
112109
struct strbuf path = STRBUF_INIT;
113110
struct strbuf worktree_path = STRBUF_INIT;
114-
struct strbuf gitdir = STRBUF_INIT;
115111
struct strbuf head_ref = STRBUF_INIT;
116112
int is_detached = 0;
117113

118114
if (!id)
119115
die("Missing linked worktree name");
120116

121-
strbuf_addf(&gitdir, "%s/worktrees/%s",
122-
absolute_path(get_git_common_dir()), id);
123-
strbuf_addf(&path, "%s/gitdir", gitdir.buf);
117+
strbuf_git_common_path(&path, "worktrees/%s/gitdir", id);
124118
if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
125119
/* invalid gitdir file */
126120
goto done;
@@ -140,15 +134,14 @@ static struct worktree *get_linked_worktree(const char *id)
140134

141135
worktree = xmalloc(sizeof(struct worktree));
142136
worktree->path = strbuf_detach(&worktree_path, NULL);
143-
worktree->git_dir = strbuf_detach(&gitdir, NULL);
137+
worktree->id = xstrdup(id);
144138
worktree->is_bare = 0;
145139
worktree->head_ref = NULL;
146140
worktree->is_detached = is_detached;
147141
add_head_info(&head_ref, worktree);
148142

149143
done:
150144
strbuf_release(&path);
151-
strbuf_release(&gitdir);
152145
strbuf_release(&worktree_path);
153146
strbuf_release(&head_ref);
154147
return worktree;
@@ -188,6 +181,16 @@ struct worktree **get_worktrees(void)
188181
return list;
189182
}
190183

184+
const char *get_worktree_git_dir(const struct worktree *wt)
185+
{
186+
if (!wt)
187+
return get_git_dir();
188+
else if (!wt->id)
189+
return get_git_common_dir();
190+
else
191+
return git_common_path("worktrees/%s", wt->id);
192+
}
193+
191194
char *find_shared_symref(const char *symref, const char *target)
192195
{
193196
char *existing = NULL;
@@ -199,7 +202,9 @@ char *find_shared_symref(const char *symref, const char *target)
199202
for (i = 0; worktrees[i]; i++) {
200203
strbuf_reset(&path);
201204
strbuf_reset(&sb);
202-
strbuf_addf(&path, "%s/%s", worktrees[i]->git_dir, symref);
205+
strbuf_addf(&path, "%s/%s",
206+
get_worktree_git_dir(worktrees[i]),
207+
symref);
203208

204209
if (parse_ref(path.buf, &sb, NULL)) {
205210
continue;

worktree.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
struct worktree {
55
char *path;
6-
char *git_dir;
6+
char *id;
77
char *head_ref;
88
unsigned char head_sha1[20];
99
int is_detached;
@@ -22,6 +22,12 @@ struct worktree {
2222
*/
2323
extern struct worktree **get_worktrees(void);
2424

25+
/*
26+
* Return git dir of the worktree. Note that the path may be relative.
27+
* If wt is NULL, git dir of current worktree is returned.
28+
*/
29+
extern const char *get_worktree_git_dir(const struct worktree *wt);
30+
2531
/*
2632
* Free up the memory for worktree(s)
2733
*/

0 commit comments

Comments
 (0)