]>
Commit | Line | Data |
---|---|---|
ac6c561b | 1 | #include "cache.h" |
b337172c | 2 | #include "repository.h" |
ac6c561b MR |
3 | #include "refs.h" |
4 | #include "strbuf.h" | |
5 | #include "worktree.h" | |
750e8a60 | 6 | #include "dir.h" |
8d9fdd70 | 7 | #include "wt-status.h" |
ac6c561b | 8 | |
51934904 MR |
9 | void free_worktrees(struct worktree **worktrees) |
10 | { | |
11 | int i = 0; | |
12 | ||
13 | for (i = 0; worktrees[i]; i++) { | |
14 | free(worktrees[i]->path); | |
69dfe3b9 | 15 | free(worktrees[i]->id); |
92718b74 | 16 | free(worktrees[i]->head_ref); |
346ef530 | 17 | free(worktrees[i]->lock_reason); |
fc0c7d5e | 18 | free(worktrees[i]->prune_reason); |
51934904 MR |
19 | free(worktrees[i]); |
20 | } | |
21 | free (worktrees); | |
22 | } | |
23 | ||
92718b74 | 24 | /** |
cfaf9f05 | 25 | * Update head_oid, head_ref and is_detached of the given worktree |
92718b74 | 26 | */ |
fa099d23 | 27 | static void add_head_info(struct worktree *wt) |
92718b74 | 28 | { |
fa099d23 NTND |
29 | int flags; |
30 | const char *target; | |
31 | ||
32 | target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt), | |
33 | "HEAD", | |
31824d18 | 34 | 0, |
49e61479 | 35 | &wt->head_oid, &flags); |
fa099d23 NTND |
36 | if (!target) |
37 | return; | |
38 | ||
39 | if (flags & REF_ISSYMREF) | |
40 | wt->head_ref = xstrdup(target); | |
41 | else | |
42 | wt->is_detached = 1; | |
92718b74 MR |
43 | } |
44 | ||
51934904 MR |
45 | /** |
46 | * get the main worktree | |
47 | */ | |
48 | static struct worktree *get_main_worktree(void) | |
1ceb7f90 | 49 | { |
51934904 | 50 | struct worktree *worktree = NULL; |
51934904 | 51 | struct strbuf worktree_path = STRBUF_INIT; |
1ceb7f90 | 52 | |
918d8ff7 ES |
53 | strbuf_add_real_path(&worktree_path, get_git_common_dir()); |
54 | strbuf_strip_suffix(&worktree_path, "/.git"); | |
51934904 | 55 | |
ca56dadb | 56 | CALLOC_ARRAY(worktree, 1); |
92718b74 | 57 | worktree->path = strbuf_detach(&worktree_path, NULL); |
f3534c98 JT |
58 | /* |
59 | * NEEDSWORK: If this function is called from a secondary worktree and | |
60 | * config.worktree is present, is_bare_repository_cfg will reflect the | |
61 | * contents of config.worktree, not the contents of the main worktree. | |
62 | * This means that worktree->is_bare may be set to 0 even if the main | |
63 | * worktree is configured to be bare. | |
64 | */ | |
65 | worktree->is_bare = (is_bare_repository_cfg == 1) || | |
66 | is_bare_repository(); | |
fa099d23 | 67 | add_head_info(worktree); |
51934904 | 68 | return worktree; |
1ceb7f90 MR |
69 | } |
70 | ||
51934904 | 71 | static struct worktree *get_linked_worktree(const char *id) |
ac6c561b | 72 | { |
51934904 | 73 | struct worktree *worktree = NULL; |
ac6c561b | 74 | struct strbuf path = STRBUF_INIT; |
51934904 | 75 | struct strbuf worktree_path = STRBUF_INIT; |
ac6c561b | 76 | |
1ceb7f90 MR |
77 | if (!id) |
78 | die("Missing linked worktree name"); | |
ac6c561b | 79 | |
b337172c | 80 | strbuf_git_common_path(&path, the_repository, "worktrees/%s/gitdir", id); |
51934904 MR |
81 | if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0) |
82 | /* invalid gitdir file */ | |
ac6c561b | 83 | goto done; |
51934904 | 84 | strbuf_rtrim(&worktree_path); |
1c4854ec | 85 | strbuf_strip_suffix(&worktree_path, "/.git"); |
51934904 | 86 | |
ca56dadb | 87 | CALLOC_ARRAY(worktree, 1); |
92718b74 | 88 | worktree->path = strbuf_detach(&worktree_path, NULL); |
69dfe3b9 | 89 | worktree->id = xstrdup(id); |
fa099d23 | 90 | add_head_info(worktree); |
ac6c561b | 91 | |
ac6c561b MR |
92 | done: |
93 | strbuf_release(&path); | |
51934904 | 94 | strbuf_release(&worktree_path); |
51934904 | 95 | return worktree; |
ac6c561b MR |
96 | } |
97 | ||
750e8a60 NTND |
98 | static void mark_current_worktree(struct worktree **worktrees) |
99 | { | |
0aaad415 | 100 | char *git_dir = absolute_pathdup(get_git_dir()); |
750e8a60 NTND |
101 | int i; |
102 | ||
750e8a60 NTND |
103 | for (i = 0; worktrees[i]; i++) { |
104 | struct worktree *wt = worktrees[i]; | |
360af2da NTND |
105 | const char *wt_git_dir = get_worktree_git_dir(wt); |
106 | ||
107 | if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) { | |
108 | wt->is_current = 1; | |
750e8a60 | 109 | break; |
360af2da | 110 | } |
750e8a60 | 111 | } |
360af2da | 112 | free(git_dir); |
750e8a60 NTND |
113 | } |
114 | ||
03f2465b | 115 | struct worktree **get_worktrees(void) |
ac6c561b | 116 | { |
51934904 | 117 | struct worktree **list = NULL; |
ac6c561b MR |
118 | struct strbuf path = STRBUF_INIT; |
119 | DIR *dir; | |
120 | struct dirent *d; | |
51934904 MR |
121 | int counter = 0, alloc = 2; |
122 | ||
3f64699f | 123 | ALLOC_ARRAY(list, alloc); |
ac6c561b | 124 | |
a234563a | 125 | list[counter++] = get_main_worktree(); |
ac6c561b MR |
126 | |
127 | strbuf_addf(&path, "%s/worktrees", get_git_common_dir()); | |
128 | dir = opendir(path.buf); | |
129 | strbuf_release(&path); | |
51934904 MR |
130 | if (dir) { |
131 | while ((d = readdir(dir)) != NULL) { | |
132 | struct worktree *linked = NULL; | |
afb9e30b | 133 | if (is_dot_or_dotdot(d->d_name)) |
51934904 | 134 | continue; |
ac6c561b | 135 | |
d4cddd66 NTND |
136 | if ((linked = get_linked_worktree(d->d_name))) { |
137 | ALLOC_GROW(list, counter + 1, alloc); | |
138 | list[counter++] = linked; | |
139 | } | |
51934904 MR |
140 | } |
141 | closedir(dir); | |
142 | } | |
143 | ALLOC_GROW(list, counter + 1, alloc); | |
144 | list[counter] = NULL; | |
750e8a60 NTND |
145 | |
146 | mark_current_worktree(list); | |
51934904 MR |
147 | return list; |
148 | } | |
149 | ||
69dfe3b9 NTND |
150 | const char *get_worktree_git_dir(const struct worktree *wt) |
151 | { | |
152 | if (!wt) | |
153 | return get_git_dir(); | |
154 | else if (!wt->id) | |
155 | return get_git_common_dir(); | |
156 | else | |
157 | return git_common_path("worktrees/%s", wt->id); | |
158 | } | |
159 | ||
080739ba NTND |
160 | static struct worktree *find_worktree_by_suffix(struct worktree **list, |
161 | const char *suffix) | |
162 | { | |
163 | struct worktree *found = NULL; | |
164 | int nr_found = 0, suffixlen; | |
165 | ||
166 | suffixlen = strlen(suffix); | |
167 | if (!suffixlen) | |
168 | return NULL; | |
169 | ||
170 | for (; *list && nr_found < 2; list++) { | |
171 | const char *path = (*list)->path; | |
172 | int pathlen = strlen(path); | |
173 | int start = pathlen - suffixlen; | |
174 | ||
175 | /* suffix must start at directory boundary */ | |
176 | if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) && | |
177 | !fspathcmp(suffix, path + start)) { | |
178 | found = *list; | |
179 | nr_found++; | |
180 | } | |
181 | } | |
182 | return nr_found == 1 ? found : NULL; | |
183 | } | |
184 | ||
68353144 NTND |
185 | struct worktree *find_worktree(struct worktree **list, |
186 | const char *prefix, | |
187 | const char *arg) | |
188 | { | |
080739ba | 189 | struct worktree *wt; |
e4da43b1 | 190 | char *to_free = NULL; |
68353144 | 191 | |
080739ba NTND |
192 | if ((wt = find_worktree_by_suffix(list, arg))) |
193 | return wt; | |
194 | ||
e4da43b1 JK |
195 | if (prefix) |
196 | arg = to_free = prefix_filename(prefix, arg); | |
bb4995fc ES |
197 | wt = find_worktree_by_path(list, arg); |
198 | free(to_free); | |
199 | return wt; | |
200 | } | |
201 | ||
202 | struct worktree *find_worktree_by_path(struct worktree **list, const char *p) | |
203 | { | |
4530a85b | 204 | struct strbuf wt_path = STRBUF_INIT; |
bb4995fc ES |
205 | char *path = real_pathdup(p, 0); |
206 | ||
207 | if (!path) | |
4c5fa9e6 | 208 | return NULL; |
105df73e | 209 | for (; *list; list++) { |
4530a85b AM |
210 | if (!strbuf_realpath(&wt_path, (*list)->path, 0)) |
211 | continue; | |
105df73e | 212 | |
4530a85b | 213 | if (!fspathcmp(path, wt_path.buf)) |
68353144 | 214 | break; |
105df73e | 215 | } |
68353144 | 216 | free(path); |
4530a85b | 217 | strbuf_release(&wt_path); |
68353144 NTND |
218 | return *list; |
219 | } | |
220 | ||
984ad9e5 NTND |
221 | int is_main_worktree(const struct worktree *wt) |
222 | { | |
223 | return !wt->id; | |
224 | } | |
225 | ||
d236f12b | 226 | const char *worktree_lock_reason(struct worktree *wt) |
346ef530 | 227 | { |
eb36135a RS |
228 | if (is_main_worktree(wt)) |
229 | return NULL; | |
346ef530 NTND |
230 | |
231 | if (!wt->lock_reason_valid) { | |
232 | struct strbuf path = STRBUF_INIT; | |
233 | ||
234 | strbuf_addstr(&path, worktree_git_path(wt, "locked")); | |
235 | if (file_exists(path.buf)) { | |
236 | struct strbuf lock_reason = STRBUF_INIT; | |
237 | if (strbuf_read_file(&lock_reason, path.buf, 0) < 0) | |
238 | die_errno(_("failed to read '%s'"), path.buf); | |
239 | strbuf_trim(&lock_reason); | |
240 | wt->lock_reason = strbuf_detach(&lock_reason, NULL); | |
241 | } else | |
242 | wt->lock_reason = NULL; | |
243 | wt->lock_reason_valid = 1; | |
244 | strbuf_release(&path); | |
245 | } | |
246 | ||
247 | return wt->lock_reason; | |
248 | } | |
249 | ||
fc0c7d5e RS |
250 | const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire) |
251 | { | |
252 | struct strbuf reason = STRBUF_INIT; | |
253 | char *path = NULL; | |
254 | ||
255 | if (is_main_worktree(wt)) | |
256 | return NULL; | |
257 | if (wt->prune_reason_valid) | |
258 | return wt->prune_reason; | |
259 | ||
260 | if (should_prune_worktree(wt->id, &reason, &path, expire)) | |
261 | wt->prune_reason = strbuf_detach(&reason, NULL); | |
262 | wt->prune_reason_valid = 1; | |
263 | ||
264 | strbuf_release(&reason); | |
265 | free(path); | |
266 | return wt->prune_reason; | |
267 | } | |
268 | ||
4ddddc1f NTND |
269 | /* convenient wrapper to deal with NULL strbuf */ |
270 | static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...) | |
271 | { | |
272 | va_list params; | |
273 | ||
274 | if (!buf) | |
275 | return; | |
276 | ||
277 | va_start(params, fmt); | |
278 | strbuf_vaddf(buf, fmt, params); | |
279 | va_end(params); | |
280 | } | |
281 | ||
ee6763af NTND |
282 | int validate_worktree(const struct worktree *wt, struct strbuf *errmsg, |
283 | unsigned flags) | |
4ddddc1f NTND |
284 | { |
285 | struct strbuf wt_path = STRBUF_INIT; | |
3d7747e3 | 286 | struct strbuf realpath = STRBUF_INIT; |
4ddddc1f NTND |
287 | char *path = NULL; |
288 | int err, ret = -1; | |
289 | ||
290 | strbuf_addf(&wt_path, "%s/.git", wt->path); | |
291 | ||
292 | if (is_main_worktree(wt)) { | |
293 | if (is_directory(wt_path.buf)) { | |
294 | ret = 0; | |
295 | goto done; | |
296 | } | |
297 | /* | |
298 | * Main worktree using .git file to point to the | |
299 | * repository would make it impossible to know where | |
300 | * the actual worktree is if this function is executed | |
301 | * from another worktree. No .git file support for now. | |
302 | */ | |
303 | strbuf_addf_gently(errmsg, | |
304 | _("'%s' at main working tree is not the repository directory"), | |
305 | wt_path.buf); | |
306 | goto done; | |
307 | } | |
308 | ||
309 | /* | |
310 | * Make sure "gitdir" file points to a real .git file and that | |
311 | * file points back here. | |
312 | */ | |
313 | if (!is_absolute_path(wt->path)) { | |
314 | strbuf_addf_gently(errmsg, | |
315 | _("'%s' file does not contain absolute path to the working tree location"), | |
316 | git_common_path("worktrees/%s/gitdir", wt->id)); | |
317 | goto done; | |
318 | } | |
319 | ||
ee6763af NTND |
320 | if (flags & WT_VALIDATE_WORKTREE_MISSING_OK && |
321 | !file_exists(wt->path)) { | |
322 | ret = 0; | |
323 | goto done; | |
324 | } | |
325 | ||
4ddddc1f NTND |
326 | if (!file_exists(wt_path.buf)) { |
327 | strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf); | |
328 | goto done; | |
329 | } | |
330 | ||
331 | path = xstrdup_or_null(read_gitfile_gently(wt_path.buf, &err)); | |
332 | if (!path) { | |
333 | strbuf_addf_gently(errmsg, _("'%s' is not a .git file, error code %d"), | |
334 | wt_path.buf, err); | |
335 | goto done; | |
336 | } | |
337 | ||
3d7747e3 AM |
338 | strbuf_realpath(&realpath, git_common_path("worktrees/%s", wt->id), 1); |
339 | ret = fspathcmp(path, realpath.buf); | |
4ddddc1f NTND |
340 | |
341 | if (ret) | |
342 | strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"), | |
343 | wt->path, git_common_path("worktrees/%s", wt->id)); | |
344 | done: | |
345 | free(path); | |
346 | strbuf_release(&wt_path); | |
3d7747e3 | 347 | strbuf_release(&realpath); |
4ddddc1f NTND |
348 | return ret; |
349 | } | |
350 | ||
9c620fc7 NTND |
351 | void update_worktree_location(struct worktree *wt, const char *path_) |
352 | { | |
353 | struct strbuf path = STRBUF_INIT; | |
354 | ||
355 | if (is_main_worktree(wt)) | |
033abf97 | 356 | BUG("can't relocate main worktree"); |
9c620fc7 NTND |
357 | |
358 | strbuf_realpath(&path, path_, 1); | |
359 | if (fspathcmp(wt->path, path.buf)) { | |
360 | write_file(git_common_path("worktrees/%s/gitdir", wt->id), | |
361 | "%s/.git", path.buf); | |
362 | free(wt->path); | |
363 | wt->path = strbuf_detach(&path, NULL); | |
364 | } | |
365 | strbuf_release(&path); | |
366 | } | |
367 | ||
14ace5b7 NTND |
368 | int is_worktree_being_rebased(const struct worktree *wt, |
369 | const char *target) | |
8d9fdd70 NTND |
370 | { |
371 | struct wt_status_state state; | |
372 | int found_rebase; | |
373 | ||
374 | memset(&state, 0, sizeof(state)); | |
375 | found_rebase = wt_status_check_rebase(wt, &state) && | |
a46d1f73 MÅ |
376 | (state.rebase_in_progress || |
377 | state.rebase_interactive_in_progress) && | |
378 | state.branch && | |
379 | skip_prefix(target, "refs/heads/", &target) && | |
380 | !strcmp(state.branch, target); | |
962dd7eb | 381 | wt_status_state_free_buffers(&state); |
8d9fdd70 NTND |
382 | return found_rebase; |
383 | } | |
384 | ||
14ace5b7 NTND |
385 | int is_worktree_being_bisected(const struct worktree *wt, |
386 | const char *target) | |
51934904 | 387 | { |
04a3dfb8 | 388 | struct wt_status_state state; |
fb07bd42 | 389 | int found_bisect; |
04a3dfb8 NTND |
390 | |
391 | memset(&state, 0, sizeof(state)); | |
fb07bd42 MÅ |
392 | found_bisect = wt_status_check_bisect(wt, &state) && |
393 | state.branch && | |
a46d1f73 MÅ |
394 | skip_prefix(target, "refs/heads/", &target) && |
395 | !strcmp(state.branch, target); | |
962dd7eb | 396 | wt_status_state_free_buffers(&state); |
fb07bd42 | 397 | return found_bisect; |
04a3dfb8 NTND |
398 | } |
399 | ||
8d9fdd70 NTND |
400 | /* |
401 | * note: this function should be able to detect shared symref even if | |
402 | * HEAD is temporarily detached (e.g. in the middle of rebase or | |
403 | * bisect). New commands that do similar things should update this | |
404 | * function as well. | |
405 | */ | |
d3b9ac07 NTND |
406 | const struct worktree *find_shared_symref(const char *symref, |
407 | const char *target) | |
51934904 | 408 | { |
d3b9ac07 | 409 | const struct worktree *existing = NULL; |
d3b9ac07 | 410 | static struct worktree **worktrees; |
51934904 MR |
411 | int i = 0; |
412 | ||
d3b9ac07 NTND |
413 | if (worktrees) |
414 | free_worktrees(worktrees); | |
03f2465b | 415 | worktrees = get_worktrees(); |
d3b9ac07 | 416 | |
51934904 | 417 | for (i = 0; worktrees[i]; i++) { |
c8717148 | 418 | struct worktree *wt = worktrees[i]; |
fa099d23 | 419 | const char *symref_target; |
fa099d23 NTND |
420 | struct ref_store *refs; |
421 | int flags; | |
422 | ||
171c646f DK |
423 | if (wt->is_bare) |
424 | continue; | |
c8717148 | 425 | |
8d9fdd70 NTND |
426 | if (wt->is_detached && !strcmp(symref, "HEAD")) { |
427 | if (is_worktree_being_rebased(wt, target)) { | |
428 | existing = wt; | |
429 | break; | |
430 | } | |
04a3dfb8 NTND |
431 | if (is_worktree_being_bisected(wt, target)) { |
432 | existing = wt; | |
433 | break; | |
434 | } | |
8d9fdd70 NTND |
435 | } |
436 | ||
fa099d23 NTND |
437 | refs = get_worktree_ref_store(wt); |
438 | symref_target = refs_resolve_ref_unsafe(refs, symref, 0, | |
e691b027 | 439 | NULL, &flags); |
dbd2b55c JK |
440 | if ((flags & REF_ISSYMREF) && |
441 | symref_target && !strcmp(symref_target, target)) { | |
c8717148 | 442 | existing = wt; |
51934904 MR |
443 | break; |
444 | } | |
ac6c561b | 445 | } |
51934904 | 446 | |
ac6c561b MR |
447 | return existing; |
448 | } | |
1a248cf2 SB |
449 | |
450 | int submodule_uses_worktrees(const char *path) | |
451 | { | |
452 | char *submodule_gitdir; | |
e02a7141 | 453 | struct strbuf sb = STRBUF_INIT, err = STRBUF_INIT; |
1a248cf2 SB |
454 | DIR *dir; |
455 | struct dirent *d; | |
7c4be458 | 456 | int ret = 0; |
e8805af1 | 457 | struct repository_format format = REPOSITORY_FORMAT_INIT; |
1a248cf2 SB |
458 | |
459 | submodule_gitdir = git_pathdup_submodule(path, "%s", ""); | |
460 | if (!submodule_gitdir) | |
461 | return 0; | |
462 | ||
463 | /* The env would be set for the superproject. */ | |
464 | get_common_dir_noenv(&sb, submodule_gitdir); | |
d32de66a | 465 | free(submodule_gitdir); |
1a248cf2 | 466 | |
1a248cf2 SB |
467 | strbuf_addstr(&sb, "/config"); |
468 | read_repository_format(&format, sb.buf); | |
e02a7141 | 469 | if (verify_repository_format(&format, &err)) { |
470 | strbuf_release(&err); | |
1a248cf2 | 471 | strbuf_release(&sb); |
e8805af1 | 472 | clear_repository_format(&format); |
1a248cf2 SB |
473 | return 1; |
474 | } | |
e8805af1 | 475 | clear_repository_format(&format); |
e02a7141 | 476 | strbuf_release(&err); |
1a248cf2 SB |
477 | |
478 | /* Replace config by worktrees. */ | |
479 | strbuf_setlen(&sb, sb.len - strlen("config")); | |
480 | strbuf_addstr(&sb, "worktrees"); | |
481 | ||
482 | /* See if there is any file inside the worktrees directory. */ | |
483 | dir = opendir(sb.buf); | |
484 | strbuf_release(&sb); | |
1a248cf2 SB |
485 | |
486 | if (!dir) | |
487 | return 0; | |
488 | ||
489 | while ((d = readdir(dir)) != NULL) { | |
490 | if (is_dot_or_dotdot(d->d_name)) | |
491 | continue; | |
492 | ||
493 | ret = 1; | |
494 | break; | |
495 | } | |
496 | closedir(dir); | |
497 | return ret; | |
498 | } | |
d0c39a49 | 499 | |
3a3b9d8c NTND |
500 | int parse_worktree_ref(const char *worktree_ref, const char **name, |
501 | int *name_length, const char **ref) | |
502 | { | |
503 | if (skip_prefix(worktree_ref, "main-worktree/", &worktree_ref)) { | |
504 | if (!*worktree_ref) | |
505 | return -1; | |
506 | if (name) | |
507 | *name = NULL; | |
508 | if (name_length) | |
509 | *name_length = 0; | |
510 | if (ref) | |
511 | *ref = worktree_ref; | |
512 | return 0; | |
513 | } | |
514 | if (skip_prefix(worktree_ref, "worktrees/", &worktree_ref)) { | |
515 | const char *slash = strchr(worktree_ref, '/'); | |
516 | ||
517 | if (!slash || slash == worktree_ref || !slash[1]) | |
518 | return -1; | |
519 | if (name) | |
520 | *name = worktree_ref; | |
521 | if (name_length) | |
522 | *name_length = slash - worktree_ref; | |
523 | if (ref) | |
524 | *ref = slash + 1; | |
525 | return 0; | |
526 | } | |
527 | return -1; | |
528 | } | |
529 | ||
ab3e1f78 NTND |
530 | void strbuf_worktree_ref(const struct worktree *wt, |
531 | struct strbuf *sb, | |
532 | const char *refname) | |
533 | { | |
534 | switch (ref_type(refname)) { | |
535 | case REF_TYPE_PSEUDOREF: | |
536 | case REF_TYPE_PER_WORKTREE: | |
537 | if (wt && !wt->is_current) { | |
538 | if (is_main_worktree(wt)) | |
539 | strbuf_addstr(sb, "main-worktree/"); | |
540 | else | |
541 | strbuf_addf(sb, "worktrees/%s/", wt->id); | |
542 | } | |
543 | break; | |
544 | ||
545 | case REF_TYPE_MAIN_PSEUDOREF: | |
546 | case REF_TYPE_OTHER_PSEUDOREF: | |
547 | break; | |
548 | ||
549 | case REF_TYPE_NORMAL: | |
550 | /* | |
551 | * For shared refs, don't prefix worktrees/ or | |
552 | * main-worktree/. It's not necessary and | |
553 | * files-backend.c can't handle it anyway. | |
554 | */ | |
555 | break; | |
556 | } | |
557 | strbuf_addstr(sb, refname); | |
558 | } | |
559 | ||
d0c39a49 NTND |
560 | int other_head_refs(each_ref_fn fn, void *cb_data) |
561 | { | |
562 | struct worktree **worktrees, **p; | |
ef2d5547 | 563 | struct strbuf refname = STRBUF_INIT; |
d0c39a49 NTND |
564 | int ret = 0; |
565 | ||
03f2465b | 566 | worktrees = get_worktrees(); |
d0c39a49 NTND |
567 | for (p = worktrees; *p; p++) { |
568 | struct worktree *wt = *p; | |
ab3e1f78 NTND |
569 | struct object_id oid; |
570 | int flag; | |
d0c39a49 NTND |
571 | |
572 | if (wt->is_current) | |
573 | continue; | |
574 | ||
ef2d5547 MÅ |
575 | strbuf_reset(&refname); |
576 | strbuf_worktree_ref(wt, &refname, "HEAD"); | |
ab3e1f78 | 577 | if (!refs_read_ref_full(get_main_ref_store(the_repository), |
ef2d5547 | 578 | refname.buf, |
ab3e1f78 NTND |
579 | RESOLVE_REF_READING, |
580 | &oid, &flag)) | |
ef2d5547 | 581 | ret = fn(refname.buf, &oid, flag, cb_data); |
d0c39a49 NTND |
582 | if (ret) |
583 | break; | |
584 | } | |
585 | free_worktrees(worktrees); | |
ef2d5547 | 586 | strbuf_release(&refname); |
d0c39a49 NTND |
587 | return ret; |
588 | } | |
bdd1f3e4 ES |
589 | |
590 | /* | |
591 | * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not | |
592 | * pointing at <repo>/worktrees/<id>. | |
593 | */ | |
594 | static void repair_gitfile(struct worktree *wt, | |
595 | worktree_repair_fn fn, void *cb_data) | |
596 | { | |
597 | struct strbuf dotgit = STRBUF_INIT; | |
598 | struct strbuf repo = STRBUF_INIT; | |
599 | char *backlink; | |
600 | const char *repair = NULL; | |
601 | int err; | |
602 | ||
603 | /* missing worktree can't be repaired */ | |
604 | if (!file_exists(wt->path)) | |
605 | return; | |
606 | ||
607 | if (!is_directory(wt->path)) { | |
608 | fn(1, wt->path, _("not a directory"), cb_data); | |
609 | return; | |
610 | } | |
611 | ||
612 | strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1); | |
613 | strbuf_addf(&dotgit, "%s/.git", wt->path); | |
614 | backlink = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err)); | |
615 | ||
616 | if (err == READ_GITFILE_ERR_NOT_A_FILE) | |
617 | fn(1, wt->path, _(".git is not a file"), cb_data); | |
618 | else if (err) | |
619 | repair = _(".git file broken"); | |
620 | else if (fspathcmp(backlink, repo.buf)) | |
621 | repair = _(".git file incorrect"); | |
622 | ||
623 | if (repair) { | |
624 | fn(0, wt->path, repair, cb_data); | |
625 | write_file(dotgit.buf, "gitdir: %s", repo.buf); | |
626 | } | |
627 | ||
628 | free(backlink); | |
629 | strbuf_release(&repo); | |
630 | strbuf_release(&dotgit); | |
631 | } | |
632 | ||
633 | static void repair_noop(int iserr, const char *path, const char *msg, | |
634 | void *cb_data) | |
635 | { | |
636 | /* nothing */ | |
637 | } | |
638 | ||
639 | void repair_worktrees(worktree_repair_fn fn, void *cb_data) | |
640 | { | |
641 | struct worktree **worktrees = get_worktrees(); | |
642 | struct worktree **wt = worktrees + 1; /* +1 skips main worktree */ | |
643 | ||
644 | if (!fn) | |
645 | fn = repair_noop; | |
646 | for (; *wt; wt++) | |
647 | repair_gitfile(*wt, fn, cb_data); | |
648 | free_worktrees(worktrees); | |
649 | } | |
b214ab5a ES |
650 | |
651 | static int is_main_worktree_path(const char *path) | |
652 | { | |
653 | struct strbuf target = STRBUF_INIT; | |
654 | struct strbuf maindir = STRBUF_INIT; | |
655 | int cmp; | |
656 | ||
657 | strbuf_add_real_path(&target, path); | |
658 | strbuf_strip_suffix(&target, "/.git"); | |
659 | strbuf_add_real_path(&maindir, get_git_common_dir()); | |
660 | strbuf_strip_suffix(&maindir, "/.git"); | |
661 | cmp = fspathcmp(maindir.buf, target.buf); | |
662 | ||
663 | strbuf_release(&maindir); | |
664 | strbuf_release(&target); | |
665 | return !cmp; | |
666 | } | |
667 | ||
cf76baea ES |
668 | /* |
669 | * If both the main worktree and linked worktree have been moved, then the | |
670 | * gitfile /path/to/worktree/.git won't point into the repository, thus we | |
671 | * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may | |
672 | * be able to infer the gitdir by manually reading /path/to/worktree/.git, | |
673 | * extracting the <id>, and checking if <repo>/worktrees/<id> exists. | |
674 | */ | |
675 | static char *infer_backlink(const char *gitfile) | |
676 | { | |
677 | struct strbuf actual = STRBUF_INIT; | |
678 | struct strbuf inferred = STRBUF_INIT; | |
679 | const char *id; | |
680 | ||
681 | if (strbuf_read_file(&actual, gitfile, 0) < 0) | |
682 | goto error; | |
683 | if (!starts_with(actual.buf, "gitdir:")) | |
684 | goto error; | |
685 | if (!(id = find_last_dir_sep(actual.buf))) | |
686 | goto error; | |
687 | strbuf_trim(&actual); | |
688 | id++; /* advance past '/' to point at <id> */ | |
689 | if (!*id) | |
690 | goto error; | |
691 | strbuf_git_common_path(&inferred, the_repository, "worktrees/%s", id); | |
692 | if (!is_directory(inferred.buf)) | |
693 | goto error; | |
694 | ||
695 | strbuf_release(&actual); | |
696 | return strbuf_detach(&inferred, NULL); | |
697 | ||
698 | error: | |
699 | strbuf_release(&actual); | |
700 | strbuf_release(&inferred); | |
701 | return NULL; | |
702 | } | |
703 | ||
b214ab5a ES |
704 | /* |
705 | * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at | |
706 | * the worktree's path. | |
707 | */ | |
708 | void repair_worktree_at_path(const char *path, | |
709 | worktree_repair_fn fn, void *cb_data) | |
710 | { | |
711 | struct strbuf dotgit = STRBUF_INIT; | |
712 | struct strbuf realdotgit = STRBUF_INIT; | |
713 | struct strbuf gitdir = STRBUF_INIT; | |
714 | struct strbuf olddotgit = STRBUF_INIT; | |
715 | char *backlink = NULL; | |
716 | const char *repair = NULL; | |
717 | int err; | |
718 | ||
719 | if (!fn) | |
720 | fn = repair_noop; | |
721 | ||
722 | if (is_main_worktree_path(path)) | |
723 | goto done; | |
724 | ||
725 | strbuf_addf(&dotgit, "%s/.git", path); | |
726 | if (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) { | |
727 | fn(1, path, _("not a valid path"), cb_data); | |
728 | goto done; | |
729 | } | |
730 | ||
731 | backlink = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err)); | |
732 | if (err == READ_GITFILE_ERR_NOT_A_FILE) { | |
733 | fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data); | |
734 | goto done; | |
cf76baea ES |
735 | } else if (err == READ_GITFILE_ERR_NOT_A_REPO) { |
736 | if (!(backlink = infer_backlink(realdotgit.buf))) { | |
737 | fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data); | |
738 | goto done; | |
739 | } | |
b214ab5a ES |
740 | } else if (err) { |
741 | fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data); | |
742 | goto done; | |
743 | } | |
744 | ||
745 | strbuf_addf(&gitdir, "%s/gitdir", backlink); | |
746 | if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0) | |
747 | repair = _("gitdir unreadable"); | |
748 | else { | |
749 | strbuf_rtrim(&olddotgit); | |
750 | if (fspathcmp(olddotgit.buf, realdotgit.buf)) | |
751 | repair = _("gitdir incorrect"); | |
752 | } | |
753 | ||
754 | if (repair) { | |
755 | fn(0, gitdir.buf, repair, cb_data); | |
756 | write_file(gitdir.buf, "%s", realdotgit.buf); | |
757 | } | |
758 | done: | |
759 | free(backlink); | |
760 | strbuf_release(&olddotgit); | |
761 | strbuf_release(&gitdir); | |
762 | strbuf_release(&realdotgit); | |
763 | strbuf_release(&dotgit); | |
764 | } | |
a29a8b75 RS |
765 | |
766 | int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire) | |
767 | { | |
768 | struct stat st; | |
769 | char *path; | |
770 | int fd; | |
771 | size_t len; | |
772 | ssize_t read_result; | |
773 | ||
774 | *wtpath = NULL; | |
775 | if (!is_directory(git_path("worktrees/%s", id))) { | |
776 | strbuf_addstr(reason, _("not a valid directory")); | |
777 | return 1; | |
778 | } | |
779 | if (file_exists(git_path("worktrees/%s/locked", id))) | |
780 | return 0; | |
781 | if (stat(git_path("worktrees/%s/gitdir", id), &st)) { | |
782 | strbuf_addstr(reason, _("gitdir file does not exist")); | |
783 | return 1; | |
784 | } | |
785 | fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY); | |
786 | if (fd < 0) { | |
787 | strbuf_addf(reason, _("unable to read gitdir file (%s)"), | |
788 | strerror(errno)); | |
789 | return 1; | |
790 | } | |
791 | len = xsize_t(st.st_size); | |
792 | path = xmallocz(len); | |
793 | ||
794 | read_result = read_in_full(fd, path, len); | |
795 | if (read_result < 0) { | |
796 | strbuf_addf(reason, _("unable to read gitdir file (%s)"), | |
797 | strerror(errno)); | |
798 | close(fd); | |
799 | free(path); | |
800 | return 1; | |
801 | } | |
802 | close(fd); | |
803 | ||
804 | if (read_result != len) { | |
805 | strbuf_addf(reason, | |
806 | _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"), | |
807 | (uintmax_t)len, (uintmax_t)read_result); | |
808 | free(path); | |
809 | return 1; | |
810 | } | |
811 | while (len && (path[len - 1] == '\n' || path[len - 1] == '\r')) | |
812 | len--; | |
813 | if (!len) { | |
814 | strbuf_addstr(reason, _("invalid gitdir file")); | |
815 | free(path); | |
816 | return 1; | |
817 | } | |
818 | path[len] = '\0'; | |
819 | if (!file_exists(path)) { | |
820 | if (stat(git_path("worktrees/%s/index", id), &st) || | |
821 | st.st_mtime <= expire) { | |
822 | strbuf_addstr(reason, _("gitdir file points to non-existent location")); | |
823 | free(path); | |
824 | return 1; | |
825 | } else { | |
826 | *wtpath = path; | |
827 | return 0; | |
828 | } | |
829 | } | |
830 | *wtpath = path; | |
831 | return 0; | |
832 | } |