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