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>
Nguyễn Thái Ngọc Duy committed
Apr 22, 2016 at 20:01 UTC
69dfe3b9420eb2a7f479a0a4cad663111af2b1f9
3 files changed
+27
-15
branch.c
+2
-1
@@ -357,7 +357,8 @@ int replace_each_worktree_head_symref(const char *oldref, const char *newref)
357
if (strcmp(oldref, worktrees[i]->head_ref))
358
continue;
359
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)) {
362
ret = -1;
363
error(_("HEAD of working tree %s is not updated"),
364
worktrees[i]->path);
worktree.c
+18
-13
@@ -9,7 +9,7 @@ void free_worktrees(struct worktree **worktrees)
9
10
for (i = 0; worktrees[i]; i++) {
11
free(worktrees[i]->path);
12
- free(worktrees[i]->git_dir);
12
+ free(worktrees[i]->id);
13
free(worktrees[i]->head_ref);
14
free(worktrees[i]);
15
}
@@ -74,13 +74,11 @@ static struct worktree *get_main_worktree(void)
74
struct worktree *worktree = NULL;
75
struct strbuf path = STRBUF_INIT;
76
struct strbuf worktree_path = STRBUF_INIT;
77
- struct strbuf gitdir = STRBUF_INIT;
77
struct strbuf head_ref = STRBUF_INIT;
78
int is_bare = 0;
79
int is_detached = 0;
80
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()));
82
is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
83
if (is_bare)
84
strbuf_strip_suffix(&worktree_path, "/.");
@@ -92,7 +90,7 @@ static struct worktree *get_main_worktree(void)
90
91
worktree = xmalloc(sizeof(struct worktree));
92
worktree->path = strbuf_detach(&worktree_path, NULL);
95
- worktree->git_dir = strbuf_detach(&gitdir, NULL);
93
+ worktree->id = NULL;
94
worktree->is_bare = is_bare;
95
worktree->head_ref = NULL;
96
worktree->is_detached = is_detached;
@@ -100,7 +98,6 @@ static struct worktree *get_main_worktree(void)
98
99
done:
100
strbuf_release(&path);
103
- strbuf_release(&gitdir);
101
strbuf_release(&worktree_path);
102
strbuf_release(&head_ref);
103
return worktree;
@@ -111,16 +108,13 @@ static struct worktree *get_linked_worktree(const char *id)
108
struct worktree *worktree = NULL;
109
struct strbuf path = STRBUF_INIT;
110
struct strbuf worktree_path = STRBUF_INIT;
114
- struct strbuf gitdir = STRBUF_INIT;
111
struct strbuf head_ref = STRBUF_INIT;
112
int is_detached = 0;
113
114
if (!id)
115
die("Missing linked worktree name");
116
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);
118
if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
119
/* invalid gitdir file */
120
goto done;
@@ -140,7 +134,7 @@ static struct worktree *get_linked_worktree(const char *id)
134
135
worktree = xmalloc(sizeof(struct worktree));
136
worktree->path = strbuf_detach(&worktree_path, NULL);
143
- worktree->git_dir = strbuf_detach(&gitdir, NULL);
137
+ worktree->id = xstrdup(id);
138
worktree->is_bare = 0;
139
worktree->head_ref = NULL;
140
worktree->is_detached = is_detached;
@@ -148,7 +142,6 @@ static struct worktree *get_linked_worktree(const char *id)
142
143
done:
144
strbuf_release(&path);
151
- strbuf_release(&gitdir);
145
strbuf_release(&worktree_path);
146
strbuf_release(&head_ref);
147
return worktree;
@@ -188,6 +181,16 @@ struct worktree **get_worktrees(void)
181
return list;
182
}
183
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
+
194
char *find_shared_symref(const char *symref, const char *target)
195
{
196
char *existing = NULL;
@@ -199,7 +202,9 @@ char *find_shared_symref(const char *symref, const char *target)
202
for (i = 0; worktrees[i]; i++) {
203
strbuf_reset(&path);
204
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);
208
209
if (parse_ref(path.buf, &sb, NULL)) {
210
continue;
worktree.h
+7
-1
@@ -3,7 +3,7 @@
3
4
struct worktree {
5
char *path;
6
- char *git_dir;
6
+ char *id;
7
char *head_ref;
8
unsigned char head_sha1[20];
9
int is_detached;
@@ -22,6 +22,12 @@ struct worktree {
22
*/
23
extern struct worktree **get_worktrees(void);
24
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
+
31
/*
32
* Free up the memory for worktree(s)
33
*/