]>
Commit | Line | Data |
---|---|---|
ac6c561b MR |
1 | #include "cache.h" |
2 | #include "refs.h" | |
3 | #include "strbuf.h" | |
4 | #include "worktree.h" | |
750e8a60 | 5 | #include "dir.h" |
8d9fdd70 | 6 | #include "wt-status.h" |
ac6c561b | 7 | |
51934904 MR |
8 | void free_worktrees(struct worktree **worktrees) |
9 | { | |
10 | int i = 0; | |
11 | ||
12 | for (i = 0; worktrees[i]; i++) { | |
13 | free(worktrees[i]->path); | |
69dfe3b9 | 14 | free(worktrees[i]->id); |
92718b74 | 15 | free(worktrees[i]->head_ref); |
346ef530 | 16 | free(worktrees[i]->lock_reason); |
51934904 MR |
17 | free(worktrees[i]); |
18 | } | |
19 | free (worktrees); | |
20 | } | |
21 | ||
1ceb7f90 MR |
22 | /* |
23 | * read 'path_to_ref' into 'ref'. Also if is_detached is not NULL, | |
832c0e5e | 24 | * set is_detached to 1 (0) if the ref is detached (is not detached). |
1ceb7f90 MR |
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 | */ | |
32 | static 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 | ||
92718b74 MR |
57 | /** |
58 | * Add the head_sha1 and head_ref (if not detached) to the given worktree | |
59 | */ | |
60 | static 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 | ||
51934904 MR |
72 | /** |
73 | * get the main worktree | |
74 | */ | |
75 | static struct worktree *get_main_worktree(void) | |
1ceb7f90 | 76 | { |
51934904 | 77 | struct worktree *worktree = NULL; |
1ceb7f90 | 78 | struct strbuf path = STRBUF_INIT; |
51934904 | 79 | struct strbuf worktree_path = STRBUF_INIT; |
51934904 | 80 | struct strbuf head_ref = STRBUF_INIT; |
92718b74 MR |
81 | int is_bare = 0; |
82 | int is_detached = 0; | |
1ceb7f90 | 83 | |
fd2e7daf | 84 | strbuf_add_absolute_path(&worktree_path, get_git_common_dir()); |
92718b74 MR |
85 | is_bare = !strbuf_strip_suffix(&worktree_path, "/.git"); |
86 | if (is_bare) | |
51934904 MR |
87 | strbuf_strip_suffix(&worktree_path, "/."); |
88 | ||
89 | strbuf_addf(&path, "%s/HEAD", get_git_common_dir()); | |
90 | ||
92718b74 MR |
91 | if (parse_ref(path.buf, &head_ref, &is_detached) < 0) |
92 | goto done; | |
93 | ||
f054996d | 94 | worktree = xcalloc(1, sizeof(*worktree)); |
92718b74 | 95 | worktree->path = strbuf_detach(&worktree_path, NULL); |
92718b74 | 96 | worktree->is_bare = is_bare; |
92718b74 MR |
97 | worktree->is_detached = is_detached; |
98 | add_head_info(&head_ref, worktree); | |
99 | ||
100 | done: | |
1ceb7f90 | 101 | strbuf_release(&path); |
51934904 MR |
102 | strbuf_release(&worktree_path); |
103 | strbuf_release(&head_ref); | |
104 | return worktree; | |
1ceb7f90 MR |
105 | } |
106 | ||
51934904 | 107 | static struct worktree *get_linked_worktree(const char *id) |
ac6c561b | 108 | { |
51934904 | 109 | struct worktree *worktree = NULL; |
ac6c561b | 110 | struct strbuf path = STRBUF_INIT; |
51934904 | 111 | struct strbuf worktree_path = STRBUF_INIT; |
51934904 | 112 | struct strbuf head_ref = STRBUF_INIT; |
92718b74 | 113 | int is_detached = 0; |
ac6c561b | 114 | |
1ceb7f90 MR |
115 | if (!id) |
116 | die("Missing linked worktree name"); | |
ac6c561b | 117 | |
69dfe3b9 | 118 | strbuf_git_common_path(&path, "worktrees/%s/gitdir", id); |
51934904 MR |
119 | if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0) |
120 | /* invalid gitdir file */ | |
ac6c561b | 121 | goto done; |
51934904 MR |
122 | |
123 | strbuf_rtrim(&worktree_path); | |
124 | if (!strbuf_strip_suffix(&worktree_path, "/.git")) { | |
125 | strbuf_reset(&worktree_path); | |
fd2e7daf | 126 | strbuf_add_absolute_path(&worktree_path, "."); |
51934904 MR |
127 | strbuf_strip_suffix(&worktree_path, "/."); |
128 | } | |
129 | ||
1ceb7f90 | 130 | strbuf_reset(&path); |
51934904 MR |
131 | strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id); |
132 | ||
92718b74 MR |
133 | if (parse_ref(path.buf, &head_ref, &is_detached) < 0) |
134 | goto done; | |
135 | ||
f054996d | 136 | worktree = xcalloc(1, sizeof(*worktree)); |
92718b74 | 137 | worktree->path = strbuf_detach(&worktree_path, NULL); |
69dfe3b9 | 138 | worktree->id = xstrdup(id); |
92718b74 MR |
139 | worktree->is_detached = is_detached; |
140 | add_head_info(&head_ref, worktree); | |
ac6c561b | 141 | |
ac6c561b MR |
142 | done: |
143 | strbuf_release(&path); | |
51934904 MR |
144 | strbuf_release(&worktree_path); |
145 | strbuf_release(&head_ref); | |
146 | return worktree; | |
ac6c561b MR |
147 | } |
148 | ||
750e8a60 NTND |
149 | static void mark_current_worktree(struct worktree **worktrees) |
150 | { | |
360af2da | 151 | char *git_dir = xstrdup(absolute_path(get_git_dir())); |
750e8a60 NTND |
152 | int i; |
153 | ||
750e8a60 NTND |
154 | for (i = 0; worktrees[i]; i++) { |
155 | struct worktree *wt = worktrees[i]; | |
360af2da NTND |
156 | const char *wt_git_dir = get_worktree_git_dir(wt); |
157 | ||
158 | if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) { | |
159 | wt->is_current = 1; | |
750e8a60 | 160 | break; |
360af2da | 161 | } |
750e8a60 | 162 | } |
360af2da | 163 | free(git_dir); |
750e8a60 NTND |
164 | } |
165 | ||
51934904 | 166 | struct worktree **get_worktrees(void) |
ac6c561b | 167 | { |
51934904 | 168 | struct worktree **list = NULL; |
ac6c561b MR |
169 | struct strbuf path = STRBUF_INIT; |
170 | DIR *dir; | |
171 | struct dirent *d; | |
51934904 MR |
172 | int counter = 0, alloc = 2; |
173 | ||
174 | list = xmalloc(alloc * sizeof(struct worktree *)); | |
ac6c561b | 175 | |
51934904 MR |
176 | if ((list[counter] = get_main_worktree())) |
177 | counter++; | |
ac6c561b MR |
178 | |
179 | strbuf_addf(&path, "%s/worktrees", get_git_common_dir()); | |
180 | dir = opendir(path.buf); | |
181 | strbuf_release(&path); | |
51934904 MR |
182 | if (dir) { |
183 | while ((d = readdir(dir)) != NULL) { | |
184 | struct worktree *linked = NULL; | |
afb9e30b | 185 | if (is_dot_or_dotdot(d->d_name)) |
51934904 | 186 | continue; |
ac6c561b | 187 | |
d4cddd66 NTND |
188 | if ((linked = get_linked_worktree(d->d_name))) { |
189 | ALLOC_GROW(list, counter + 1, alloc); | |
190 | list[counter++] = linked; | |
191 | } | |
51934904 MR |
192 | } |
193 | closedir(dir); | |
194 | } | |
195 | ALLOC_GROW(list, counter + 1, alloc); | |
196 | list[counter] = NULL; | |
750e8a60 NTND |
197 | |
198 | mark_current_worktree(list); | |
51934904 MR |
199 | return list; |
200 | } | |
201 | ||
69dfe3b9 NTND |
202 | const char *get_worktree_git_dir(const struct worktree *wt) |
203 | { | |
204 | if (!wt) | |
205 | return get_git_dir(); | |
206 | else if (!wt->id) | |
207 | return get_git_common_dir(); | |
208 | else | |
209 | return git_common_path("worktrees/%s", wt->id); | |
210 | } | |
211 | ||
080739ba NTND |
212 | static struct worktree *find_worktree_by_suffix(struct worktree **list, |
213 | const char *suffix) | |
214 | { | |
215 | struct worktree *found = NULL; | |
216 | int nr_found = 0, suffixlen; | |
217 | ||
218 | suffixlen = strlen(suffix); | |
219 | if (!suffixlen) | |
220 | return NULL; | |
221 | ||
222 | for (; *list && nr_found < 2; list++) { | |
223 | const char *path = (*list)->path; | |
224 | int pathlen = strlen(path); | |
225 | int start = pathlen - suffixlen; | |
226 | ||
227 | /* suffix must start at directory boundary */ | |
228 | if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) && | |
229 | !fspathcmp(suffix, path + start)) { | |
230 | found = *list; | |
231 | nr_found++; | |
232 | } | |
233 | } | |
234 | return nr_found == 1 ? found : NULL; | |
235 | } | |
236 | ||
68353144 NTND |
237 | struct worktree *find_worktree(struct worktree **list, |
238 | const char *prefix, | |
239 | const char *arg) | |
240 | { | |
080739ba | 241 | struct worktree *wt; |
68353144 NTND |
242 | char *path; |
243 | ||
080739ba NTND |
244 | if ((wt = find_worktree_by_suffix(list, arg))) |
245 | return wt; | |
246 | ||
68353144 NTND |
247 | arg = prefix_filename(prefix, strlen(prefix), arg); |
248 | path = xstrdup(real_path(arg)); | |
249 | for (; *list; list++) | |
250 | if (!fspathcmp(path, real_path((*list)->path))) | |
251 | break; | |
252 | free(path); | |
253 | return *list; | |
254 | } | |
255 | ||
984ad9e5 NTND |
256 | int is_main_worktree(const struct worktree *wt) |
257 | { | |
258 | return !wt->id; | |
259 | } | |
260 | ||
346ef530 NTND |
261 | const char *is_worktree_locked(struct worktree *wt) |
262 | { | |
263 | assert(!is_main_worktree(wt)); | |
264 | ||
265 | if (!wt->lock_reason_valid) { | |
266 | struct strbuf path = STRBUF_INIT; | |
267 | ||
268 | strbuf_addstr(&path, worktree_git_path(wt, "locked")); | |
269 | if (file_exists(path.buf)) { | |
270 | struct strbuf lock_reason = STRBUF_INIT; | |
271 | if (strbuf_read_file(&lock_reason, path.buf, 0) < 0) | |
272 | die_errno(_("failed to read '%s'"), path.buf); | |
273 | strbuf_trim(&lock_reason); | |
274 | wt->lock_reason = strbuf_detach(&lock_reason, NULL); | |
275 | } else | |
276 | wt->lock_reason = NULL; | |
277 | wt->lock_reason_valid = 1; | |
278 | strbuf_release(&path); | |
279 | } | |
280 | ||
281 | return wt->lock_reason; | |
282 | } | |
283 | ||
14ace5b7 NTND |
284 | int is_worktree_being_rebased(const struct worktree *wt, |
285 | const char *target) | |
8d9fdd70 NTND |
286 | { |
287 | struct wt_status_state state; | |
288 | int found_rebase; | |
289 | ||
290 | memset(&state, 0, sizeof(state)); | |
291 | found_rebase = wt_status_check_rebase(wt, &state) && | |
292 | ((state.rebase_in_progress || | |
293 | state.rebase_interactive_in_progress) && | |
294 | state.branch && | |
295 | starts_with(target, "refs/heads/") && | |
296 | !strcmp(state.branch, target + strlen("refs/heads/"))); | |
297 | free(state.branch); | |
298 | free(state.onto); | |
299 | return found_rebase; | |
300 | } | |
301 | ||
14ace5b7 NTND |
302 | int is_worktree_being_bisected(const struct worktree *wt, |
303 | const char *target) | |
51934904 | 304 | { |
04a3dfb8 NTND |
305 | struct wt_status_state state; |
306 | int found_rebase; | |
307 | ||
308 | memset(&state, 0, sizeof(state)); | |
309 | found_rebase = wt_status_check_bisect(wt, &state) && | |
310 | state.branch && | |
311 | starts_with(target, "refs/heads/") && | |
312 | !strcmp(state.branch, target + strlen("refs/heads/")); | |
313 | free(state.branch); | |
314 | return found_rebase; | |
315 | } | |
316 | ||
8d9fdd70 NTND |
317 | /* |
318 | * note: this function should be able to detect shared symref even if | |
319 | * HEAD is temporarily detached (e.g. in the middle of rebase or | |
320 | * bisect). New commands that do similar things should update this | |
321 | * function as well. | |
322 | */ | |
d3b9ac07 NTND |
323 | const struct worktree *find_shared_symref(const char *symref, |
324 | const char *target) | |
51934904 | 325 | { |
d3b9ac07 | 326 | const struct worktree *existing = NULL; |
51934904 MR |
327 | struct strbuf path = STRBUF_INIT; |
328 | struct strbuf sb = STRBUF_INIT; | |
d3b9ac07 | 329 | static struct worktree **worktrees; |
51934904 MR |
330 | int i = 0; |
331 | ||
d3b9ac07 NTND |
332 | if (worktrees) |
333 | free_worktrees(worktrees); | |
334 | worktrees = get_worktrees(); | |
335 | ||
51934904 | 336 | for (i = 0; worktrees[i]; i++) { |
c8717148 | 337 | struct worktree *wt = worktrees[i]; |
171c646f DK |
338 | if (wt->is_bare) |
339 | continue; | |
c8717148 | 340 | |
8d9fdd70 NTND |
341 | if (wt->is_detached && !strcmp(symref, "HEAD")) { |
342 | if (is_worktree_being_rebased(wt, target)) { | |
343 | existing = wt; | |
344 | break; | |
345 | } | |
04a3dfb8 NTND |
346 | if (is_worktree_being_bisected(wt, target)) { |
347 | existing = wt; | |
348 | break; | |
349 | } | |
8d9fdd70 NTND |
350 | } |
351 | ||
51934904 MR |
352 | strbuf_reset(&path); |
353 | strbuf_reset(&sb); | |
69dfe3b9 | 354 | strbuf_addf(&path, "%s/%s", |
c8717148 | 355 | get_worktree_git_dir(wt), |
69dfe3b9 | 356 | symref); |
51934904 MR |
357 | |
358 | if (parse_ref(path.buf, &sb, NULL)) { | |
ac6c561b | 359 | continue; |
51934904 MR |
360 | } |
361 | ||
362 | if (!strcmp(sb.buf, target)) { | |
c8717148 | 363 | existing = wt; |
51934904 MR |
364 | break; |
365 | } | |
ac6c561b | 366 | } |
51934904 MR |
367 | |
368 | strbuf_release(&path); | |
369 | strbuf_release(&sb); | |
ac6c561b MR |
370 | |
371 | return existing; | |
372 | } |