1#include "cache.h"
2#include "refs.h"
3#include "strbuf.h"
4#include "worktree.h"
5#include "dir.h"
6#include "wt-status.h"
7
8void free_worktrees(struct worktree **worktrees)
9{
10 int i = 0;
11
12 for (i = 0; worktrees[i]; i++) {
13 free(worktrees[i]->path);
14 free(worktrees[i]->id);
15 free(worktrees[i]->head_ref);
16 free(worktrees[i]);
17 }
18 free (worktrees);
19}
20
21/*
22 * read 'path_to_ref' into 'ref'. Also if is_detached is not NULL,
23 * set is_detached to 1 (0) if the ref is detached (is not detached).
24 *
25 * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside $GIT_DIR so
26 * for linked worktrees, `resolve_ref_unsafe()` won't work (it uses
27 * git_path). Parse the ref ourselves.
28 *
29 * return -1 if the ref is not a proper ref, 0 otherwise (success)
30 */
31static int parse_ref(char *path_to_ref, struct strbuf *ref, int *is_detached)
32{
33 if (is_detached)
34 *is_detached = 0;
35 if (!strbuf_readlink(ref, path_to_ref, 0)) {
36 /* HEAD is symbolic link */
37 if (!starts_with(ref->buf, "refs/") ||
38 check_refname_format(ref->buf, 0))
39 return -1;
40 } else if (strbuf_read_file(ref, path_to_ref, 0) >= 0) {
41 /* textual symref or detached */
42 if (!starts_with(ref->buf, "ref:")) {
43 if (is_detached)
44 *is_detached = 1;
45 } else {
46 strbuf_remove(ref, 0, strlen("ref:"));
47 strbuf_trim(ref);
48 if (check_refname_format(ref->buf, 0))
49 return -1;
50 }
51 } else
52 return -1;
53 return 0;
54}
55
56/**
57 * Add the head_sha1 and head_ref (if not detached) to the given worktree
58 */
59static void add_head_info(struct strbuf *head_ref, struct worktree *worktree)
60{
61 if (head_ref->len) {
62 if (worktree->is_detached) {
63 get_sha1_hex(head_ref->buf, worktree->head_sha1);
64 } else {
65 resolve_ref_unsafe(head_ref->buf, 0, worktree->head_sha1, NULL);
66 worktree->head_ref = strbuf_detach(head_ref, NULL);
67 }
68 }
69}
70
71/**
72 * get the main worktree
73 */
74static struct worktree *get_main_worktree(void)
75{
76 struct worktree *worktree = NULL;
77 struct strbuf path = STRBUF_INIT;
78 struct strbuf worktree_path = STRBUF_INIT;
79 struct strbuf head_ref = STRBUF_INIT;
80 int is_bare = 0;
81 int is_detached = 0;
82
83 strbuf_addstr(&worktree_path, absolute_path(get_git_common_dir()));
84 is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
85 if (is_bare)
86 strbuf_strip_suffix(&worktree_path, "/.");
87
88 strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
89
90 if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
91 goto done;
92
93 worktree = xmalloc(sizeof(struct worktree));
94 worktree->path = strbuf_detach(&worktree_path, NULL);
95 worktree->id = NULL;
96 worktree->is_bare = is_bare;
97 worktree->head_ref = NULL;
98 worktree->is_detached = is_detached;
99 worktree->is_current = 0;
100 add_head_info(&head_ref, worktree);
101
102done:
103 strbuf_release(&path);
104 strbuf_release(&worktree_path);
105 strbuf_release(&head_ref);
106 return worktree;
107}
108
109static struct worktree *get_linked_worktree(const char *id)
110{
111 struct worktree *worktree = NULL;
112 struct strbuf path = STRBUF_INIT;
113 struct strbuf worktree_path = STRBUF_INIT;
114 struct strbuf head_ref = STRBUF_INIT;
115 int is_detached = 0;
116
117 if (!id)
118 die("Missing linked worktree name");
119
120 strbuf_git_common_path(&path, "worktrees/%s/gitdir", id);
121 if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
122 /* invalid gitdir file */
123 goto done;
124
125 strbuf_rtrim(&worktree_path);
126 if (!strbuf_strip_suffix(&worktree_path, "/.git")) {
127 strbuf_reset(&worktree_path);
128 strbuf_addstr(&worktree_path, absolute_path("."));
129 strbuf_strip_suffix(&worktree_path, "/.");
130 }
131
132 strbuf_reset(&path);
133 strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id);
134
135 if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
136 goto done;
137
138 worktree = xmalloc(sizeof(struct worktree));
139 worktree->path = strbuf_detach(&worktree_path, NULL);
140 worktree->id = xstrdup(id);
141 worktree->is_bare = 0;
142 worktree->head_ref = NULL;
143 worktree->is_detached = is_detached;
144 worktree->is_current = 0;
145 add_head_info(&head_ref, worktree);
146
147done:
148 strbuf_release(&path);
149 strbuf_release(&worktree_path);
150 strbuf_release(&head_ref);
151 return worktree;
152}
153
154static void mark_current_worktree(struct worktree **worktrees)
155{
156 char *git_dir = xstrdup(absolute_path(get_git_dir()));
157 int i;
158
159 for (i = 0; worktrees[i]; i++) {
160 struct worktree *wt = worktrees[i];
161 const char *wt_git_dir = get_worktree_git_dir(wt);
162
163 if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
164 wt->is_current = 1;
165 break;
166 }
167 }
168 free(git_dir);
169}
170
171struct worktree **get_worktrees(void)
172{
173 struct worktree **list = NULL;
174 struct strbuf path = STRBUF_INIT;
175 DIR *dir;
176 struct dirent *d;
177 int counter = 0, alloc = 2;
178
179 list = xmalloc(alloc * sizeof(struct worktree *));
180
181 if ((list[counter] = get_main_worktree()))
182 counter++;
183
184 strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
185 dir = opendir(path.buf);
186 strbuf_release(&path);
187 if (dir) {
188 while ((d = readdir(dir)) != NULL) {
189 struct worktree *linked = NULL;
190 if (is_dot_or_dotdot(d->d_name))
191 continue;
192
193 if ((linked = get_linked_worktree(d->d_name))) {
194 ALLOC_GROW(list, counter + 1, alloc);
195 list[counter++] = linked;
196 }
197 }
198 closedir(dir);
199 }
200 ALLOC_GROW(list, counter + 1, alloc);
201 list[counter] = NULL;
202
203 mark_current_worktree(list);
204 return list;
205}
206
207const char *get_worktree_git_dir(const struct worktree *wt)
208{
209 if (!wt)
210 return get_git_dir();
211 else if (!wt->id)
212 return get_git_common_dir();
213 else
214 return git_common_path("worktrees/%s", wt->id);
215}
216
217int is_worktree_being_rebased(const struct worktree *wt,
218 const char *target)
219{
220 struct wt_status_state state;
221 int found_rebase;
222
223 memset(&state, 0, sizeof(state));
224 found_rebase = wt_status_check_rebase(wt, &state) &&
225 ((state.rebase_in_progress ||
226 state.rebase_interactive_in_progress) &&
227 state.branch &&
228 starts_with(target, "refs/heads/") &&
229 !strcmp(state.branch, target + strlen("refs/heads/")));
230 free(state.branch);
231 free(state.onto);
232 return found_rebase;
233}
234
235int is_worktree_being_bisected(const struct worktree *wt,
236 const char *target)
237{
238 struct wt_status_state state;
239 int found_rebase;
240
241 memset(&state, 0, sizeof(state));
242 found_rebase = wt_status_check_bisect(wt, &state) &&
243 state.branch &&
244 starts_with(target, "refs/heads/") &&
245 !strcmp(state.branch, target + strlen("refs/heads/"));
246 free(state.branch);
247 return found_rebase;
248}
249
250/*
251 * note: this function should be able to detect shared symref even if
252 * HEAD is temporarily detached (e.g. in the middle of rebase or
253 * bisect). New commands that do similar things should update this
254 * function as well.
255 */
256const struct worktree *find_shared_symref(const char *symref,
257 const char *target)
258{
259 const struct worktree *existing = NULL;
260 struct strbuf path = STRBUF_INIT;
261 struct strbuf sb = STRBUF_INIT;
262 static struct worktree **worktrees;
263 int i = 0;
264
265 if (worktrees)
266 free_worktrees(worktrees);
267 worktrees = get_worktrees();
268
269 for (i = 0; worktrees[i]; i++) {
270 struct worktree *wt = worktrees[i];
271
272 if (wt->is_detached && !strcmp(symref, "HEAD")) {
273 if (is_worktree_being_rebased(wt, target)) {
274 existing = wt;
275 break;
276 }
277 if (is_worktree_being_bisected(wt, target)) {
278 existing = wt;
279 break;
280 }
281 }
282
283 strbuf_reset(&path);
284 strbuf_reset(&sb);
285 strbuf_addf(&path, "%s/%s",
286 get_worktree_git_dir(wt),
287 symref);
288
289 if (parse_ref(path.buf, &sb, NULL)) {
290 continue;
291 }
292
293 if (!strcmp(sb.buf, target)) {
294 existing = wt;
295 break;
296 }
297 }
298
299 strbuf_release(&path);
300 strbuf_release(&sb);
301
302 return existing;
303}