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