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