]> git.ipfire.org Git - thirdparty/git.git/blame - worktree.c
branch: fix die_if_checked_out() when ignore_current_worktree
[thirdparty/git.git] / worktree.c
CommitLineData
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"
615a84ad 8#include "config.h"
ac6c561b 9
51934904
MR
10void free_worktrees(struct worktree **worktrees)
11{
12 int i = 0;
13
14 for (i = 0; worktrees[i]; i++) {
15 free(worktrees[i]->path);
69dfe3b9 16 free(worktrees[i]->id);
92718b74 17 free(worktrees[i]->head_ref);
346ef530 18 free(worktrees[i]->lock_reason);
fc0c7d5e 19 free(worktrees[i]->prune_reason);
51934904
MR
20 free(worktrees[i]);
21 }
22 free (worktrees);
23}
24
92718b74 25/**
cfaf9f05 26 * Update head_oid, head_ref and is_detached of the given worktree
92718b74 27 */
fa099d23 28static void add_head_info(struct worktree *wt)
92718b74 29{
fa099d23
NTND
30 int flags;
31 const char *target;
32
f1da24ca 33 target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
fa099d23 34 "HEAD",
31824d18 35 0,
ce14de03 36 &wt->head_oid, &flags);
fa099d23
NTND
37 if (!target)
38 return;
39
40 if (flags & REF_ISSYMREF)
41 wt->head_ref = xstrdup(target);
42 else
43 wt->is_detached = 1;
92718b74
MR
44}
45
51934904
MR
46/**
47 * get the main worktree
48 */
49static struct worktree *get_main_worktree(void)
1ceb7f90 50{
51934904 51 struct worktree *worktree = NULL;
51934904 52 struct strbuf worktree_path = STRBUF_INIT;
1ceb7f90 53
918d8ff7
ES
54 strbuf_add_real_path(&worktree_path, get_git_common_dir());
55 strbuf_strip_suffix(&worktree_path, "/.git");
51934904 56
ca56dadb 57 CALLOC_ARRAY(worktree, 1);
92718b74 58 worktree->path = strbuf_detach(&worktree_path, NULL);
f3534c98
JT
59 /*
60 * NEEDSWORK: If this function is called from a secondary worktree and
61 * config.worktree is present, is_bare_repository_cfg will reflect the
62 * contents of config.worktree, not the contents of the main worktree.
63 * This means that worktree->is_bare may be set to 0 even if the main
64 * worktree is configured to be bare.
65 */
66 worktree->is_bare = (is_bare_repository_cfg == 1) ||
67 is_bare_repository();
fa099d23 68 add_head_info(worktree);
51934904 69 return worktree;
1ceb7f90
MR
70}
71
51934904 72static struct worktree *get_linked_worktree(const char *id)
ac6c561b 73{
51934904 74 struct worktree *worktree = NULL;
ac6c561b 75 struct strbuf path = STRBUF_INIT;
51934904 76 struct strbuf worktree_path = STRBUF_INIT;
ac6c561b 77
1ceb7f90
MR
78 if (!id)
79 die("Missing linked worktree name");
ac6c561b 80
b337172c 81 strbuf_git_common_path(&path, the_repository, "worktrees/%s/gitdir", id);
51934904
MR
82 if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
83 /* invalid gitdir file */
ac6c561b 84 goto done;
51934904 85 strbuf_rtrim(&worktree_path);
1c4854ec 86 strbuf_strip_suffix(&worktree_path, "/.git");
51934904 87
ca56dadb 88 CALLOC_ARRAY(worktree, 1);
92718b74 89 worktree->path = strbuf_detach(&worktree_path, NULL);
69dfe3b9 90 worktree->id = xstrdup(id);
fa099d23 91 add_head_info(worktree);
ac6c561b 92
ac6c561b
MR
93done:
94 strbuf_release(&path);
51934904 95 strbuf_release(&worktree_path);
51934904 96 return worktree;
ac6c561b
MR
97}
98
750e8a60
NTND
99static void mark_current_worktree(struct worktree **worktrees)
100{
0aaad415 101 char *git_dir = absolute_pathdup(get_git_dir());
750e8a60
NTND
102 int i;
103
750e8a60
NTND
104 for (i = 0; worktrees[i]; i++) {
105 struct worktree *wt = worktrees[i];
360af2da
NTND
106 const char *wt_git_dir = get_worktree_git_dir(wt);
107
108 if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
109 wt->is_current = 1;
750e8a60 110 break;
360af2da 111 }
750e8a60 112 }
360af2da 113 free(git_dir);
750e8a60
NTND
114}
115
03f2465b 116struct worktree **get_worktrees(void)
ac6c561b 117{
51934904 118 struct worktree **list = NULL;
ac6c561b
MR
119 struct strbuf path = STRBUF_INIT;
120 DIR *dir;
121 struct dirent *d;
51934904
MR
122 int counter = 0, alloc = 2;
123
3f64699f 124 ALLOC_ARRAY(list, alloc);
ac6c561b 125
a234563a 126 list[counter++] = get_main_worktree();
ac6c561b
MR
127
128 strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
129 dir = opendir(path.buf);
130 strbuf_release(&path);
51934904 131 if (dir) {
b548f0f1 132 while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {
51934904 133 struct worktree *linked = NULL;
ac6c561b 134
d4cddd66
NTND
135 if ((linked = get_linked_worktree(d->d_name))) {
136 ALLOC_GROW(list, counter + 1, alloc);
137 list[counter++] = linked;
138 }
51934904
MR
139 }
140 closedir(dir);
141 }
142 ALLOC_GROW(list, counter + 1, alloc);
143 list[counter] = NULL;
750e8a60
NTND
144
145 mark_current_worktree(list);
51934904
MR
146 return list;
147}
148
69dfe3b9
NTND
149const char *get_worktree_git_dir(const struct worktree *wt)
150{
151 if (!wt)
152 return get_git_dir();
153 else if (!wt->id)
154 return get_git_common_dir();
155 else
156 return git_common_path("worktrees/%s", wt->id);
157}
158
080739ba
NTND
159static struct worktree *find_worktree_by_suffix(struct worktree **list,
160 const char *suffix)
161{
162 struct worktree *found = NULL;
163 int nr_found = 0, suffixlen;
164
165 suffixlen = strlen(suffix);
166 if (!suffixlen)
167 return NULL;
168
169 for (; *list && nr_found < 2; list++) {
170 const char *path = (*list)->path;
171 int pathlen = strlen(path);
172 int start = pathlen - suffixlen;
173
174 /* suffix must start at directory boundary */
175 if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) &&
176 !fspathcmp(suffix, path + start)) {
177 found = *list;
178 nr_found++;
179 }
180 }
181 return nr_found == 1 ? found : NULL;
182}
183
68353144
NTND
184struct worktree *find_worktree(struct worktree **list,
185 const char *prefix,
186 const char *arg)
187{
080739ba 188 struct worktree *wt;
e4da43b1 189 char *to_free = NULL;
68353144 190
080739ba
NTND
191 if ((wt = find_worktree_by_suffix(list, arg)))
192 return wt;
193
e4da43b1
JK
194 if (prefix)
195 arg = to_free = prefix_filename(prefix, arg);
bb4995fc
ES
196 wt = find_worktree_by_path(list, arg);
197 free(to_free);
198 return wt;
199}
200
201struct worktree *find_worktree_by_path(struct worktree **list, const char *p)
202{
4530a85b 203 struct strbuf wt_path = STRBUF_INIT;
bb4995fc
ES
204 char *path = real_pathdup(p, 0);
205
206 if (!path)
4c5fa9e6 207 return NULL;
105df73e 208 for (; *list; list++) {
4530a85b
AM
209 if (!strbuf_realpath(&wt_path, (*list)->path, 0))
210 continue;
105df73e 211
4530a85b 212 if (!fspathcmp(path, wt_path.buf))
68353144 213 break;
105df73e 214 }
68353144 215 free(path);
4530a85b 216 strbuf_release(&wt_path);
68353144
NTND
217 return *list;
218}
219
984ad9e5
NTND
220int is_main_worktree(const struct worktree *wt)
221{
222 return !wt->id;
223}
224
d236f12b 225const char *worktree_lock_reason(struct worktree *wt)
346ef530 226{
eb36135a
RS
227 if (is_main_worktree(wt))
228 return NULL;
346ef530
NTND
229
230 if (!wt->lock_reason_valid) {
231 struct strbuf path = STRBUF_INIT;
232
233 strbuf_addstr(&path, worktree_git_path(wt, "locked"));
234 if (file_exists(path.buf)) {
235 struct strbuf lock_reason = STRBUF_INIT;
236 if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
237 die_errno(_("failed to read '%s'"), path.buf);
238 strbuf_trim(&lock_reason);
239 wt->lock_reason = strbuf_detach(&lock_reason, NULL);
240 } else
241 wt->lock_reason = NULL;
242 wt->lock_reason_valid = 1;
243 strbuf_release(&path);
244 }
245
246 return wt->lock_reason;
247}
248
fc0c7d5e
RS
249const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire)
250{
251 struct strbuf reason = STRBUF_INIT;
252 char *path = NULL;
253
254 if (is_main_worktree(wt))
255 return NULL;
256 if (wt->prune_reason_valid)
257 return wt->prune_reason;
258
259 if (should_prune_worktree(wt->id, &reason, &path, expire))
260 wt->prune_reason = strbuf_detach(&reason, NULL);
261 wt->prune_reason_valid = 1;
262
263 strbuf_release(&reason);
264 free(path);
265 return wt->prune_reason;
266}
267
4ddddc1f 268/* convenient wrapper to deal with NULL strbuf */
48ca53ca 269__attribute__((format (printf, 2, 3)))
4ddddc1f
NTND
270static 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
282int 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));
344done:
345 free(path);
346 strbuf_release(&wt_path);
3d7747e3 347 strbuf_release(&realpath);
4ddddc1f
NTND
348 return ret;
349}
350
9c620fc7
NTND
351void 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
368int 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
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
385int 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
392 found_bisect = wt_status_check_bisect(wt, &state) &&
393 state.branch &&
a46d1f73
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 */
662078ca
RJ
406int is_shared_symref(const struct worktree *wt, const char *symref,
407 const char *target)
51934904 408{
662078ca
RJ
409 const char *symref_target;
410 struct ref_store *refs;
411 int flags;
51934904 412
662078ca
RJ
413 if (wt->is_bare)
414 return 0;
fa099d23 415
662078ca
RJ
416 if (wt->is_detached && !strcmp(symref, "HEAD")) {
417 if (is_worktree_being_rebased(wt, target))
418 return 1;
419 if (is_worktree_being_bisected(wt, target))
420 return 1;
421 }
c8717148 422
662078ca
RJ
423 refs = get_worktree_ref_store(wt);
424 symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
425 NULL, &flags);
426 if ((flags & REF_ISSYMREF) &&
427 symref_target && !strcmp(symref_target, target))
428 return 1;
8d9fdd70 429
662078ca
RJ
430 return 0;
431}
432
433const struct worktree *find_shared_symref(struct worktree **worktrees,
434 const char *symref,
435 const char *target)
436{
437
faa4d598 438 for (int i = 0; worktrees[i]; i++)
662078ca
RJ
439 if (is_shared_symref(worktrees[i], symref, target))
440 return worktrees[i];
51934904 441
662078ca 442 return NULL;
ac6c561b 443}
1a248cf2
SB
444
445int submodule_uses_worktrees(const char *path)
446{
447 char *submodule_gitdir;
e02a7141 448 struct strbuf sb = STRBUF_INIT, err = STRBUF_INIT;
1a248cf2
SB
449 DIR *dir;
450 struct dirent *d;
7c4be458 451 int ret = 0;
e8805af1 452 struct repository_format format = REPOSITORY_FORMAT_INIT;
1a248cf2
SB
453
454 submodule_gitdir = git_pathdup_submodule(path, "%s", "");
455 if (!submodule_gitdir)
456 return 0;
457
458 /* The env would be set for the superproject. */
459 get_common_dir_noenv(&sb, submodule_gitdir);
d32de66a 460 free(submodule_gitdir);
1a248cf2 461
1a248cf2
SB
462 strbuf_addstr(&sb, "/config");
463 read_repository_format(&format, sb.buf);
e02a7141 464 if (verify_repository_format(&format, &err)) {
465 strbuf_release(&err);
1a248cf2 466 strbuf_release(&sb);
e8805af1 467 clear_repository_format(&format);
1a248cf2
SB
468 return 1;
469 }
e8805af1 470 clear_repository_format(&format);
e02a7141 471 strbuf_release(&err);
1a248cf2
SB
472
473 /* Replace config by worktrees. */
474 strbuf_setlen(&sb, sb.len - strlen("config"));
475 strbuf_addstr(&sb, "worktrees");
476
477 /* See if there is any file inside the worktrees directory. */
478 dir = opendir(sb.buf);
479 strbuf_release(&sb);
1a248cf2
SB
480
481 if (!dir)
482 return 0;
483
b548f0f1 484 d = readdir_skip_dot_and_dotdot(dir);
afe8a907 485 if (d)
1a248cf2 486 ret = 1;
1a248cf2
SB
487 closedir(dir);
488 return ret;
489}
d0c39a49 490
ab3e1f78
NTND
491void strbuf_worktree_ref(const struct worktree *wt,
492 struct strbuf *sb,
493 const char *refname)
494{
71e54734
HWN
495 if (parse_worktree_ref(refname, NULL, NULL, NULL) ==
496 REF_WORKTREE_CURRENT &&
497 wt && !wt->is_current) {
498 if (is_main_worktree(wt))
499 strbuf_addstr(sb, "main-worktree/");
500 else
501 strbuf_addf(sb, "worktrees/%s/", wt->id);
ab3e1f78
NTND
502 }
503 strbuf_addstr(sb, refname);
504}
505
d0c39a49
NTND
506int other_head_refs(each_ref_fn fn, void *cb_data)
507{
508 struct worktree **worktrees, **p;
ef2d5547 509 struct strbuf refname = STRBUF_INIT;
d0c39a49
NTND
510 int ret = 0;
511
03f2465b 512 worktrees = get_worktrees();
d0c39a49
NTND
513 for (p = worktrees; *p; p++) {
514 struct worktree *wt = *p;
ab3e1f78
NTND
515 struct object_id oid;
516 int flag;
d0c39a49
NTND
517
518 if (wt->is_current)
519 continue;
520
ef2d5547
521 strbuf_reset(&refname);
522 strbuf_worktree_ref(wt, &refname, "HEAD");
f1da24ca 523 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
76887df0
ÆAB
524 refname.buf,
525 RESOLVE_REF_READING,
ce14de03 526 &oid, &flag))
ef2d5547 527 ret = fn(refname.buf, &oid, flag, cb_data);
d0c39a49
NTND
528 if (ret)
529 break;
530 }
531 free_worktrees(worktrees);
ef2d5547 532 strbuf_release(&refname);
d0c39a49
NTND
533 return ret;
534}
bdd1f3e4
ES
535
536/*
537 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
538 * pointing at <repo>/worktrees/<id>.
539 */
540static void repair_gitfile(struct worktree *wt,
541 worktree_repair_fn fn, void *cb_data)
542{
543 struct strbuf dotgit = STRBUF_INIT;
544 struct strbuf repo = STRBUF_INIT;
545 char *backlink;
546 const char *repair = NULL;
547 int err;
548
549 /* missing worktree can't be repaired */
550 if (!file_exists(wt->path))
551 return;
552
553 if (!is_directory(wt->path)) {
554 fn(1, wt->path, _("not a directory"), cb_data);
555 return;
556 }
557
558 strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
559 strbuf_addf(&dotgit, "%s/.git", wt->path);
560 backlink = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
561
562 if (err == READ_GITFILE_ERR_NOT_A_FILE)
563 fn(1, wt->path, _(".git is not a file"), cb_data);
564 else if (err)
565 repair = _(".git file broken");
566 else if (fspathcmp(backlink, repo.buf))
567 repair = _(".git file incorrect");
568
569 if (repair) {
570 fn(0, wt->path, repair, cb_data);
571 write_file(dotgit.buf, "gitdir: %s", repo.buf);
572 }
573
574 free(backlink);
575 strbuf_release(&repo);
576 strbuf_release(&dotgit);
577}
578
579static void repair_noop(int iserr, const char *path, const char *msg,
580 void *cb_data)
581{
582 /* nothing */
583}
584
585void repair_worktrees(worktree_repair_fn fn, void *cb_data)
586{
587 struct worktree **worktrees = get_worktrees();
588 struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
589
590 if (!fn)
591 fn = repair_noop;
592 for (; *wt; wt++)
593 repair_gitfile(*wt, fn, cb_data);
594 free_worktrees(worktrees);
595}
b214ab5a
ES
596
597static int is_main_worktree_path(const char *path)
598{
599 struct strbuf target = STRBUF_INIT;
600 struct strbuf maindir = STRBUF_INIT;
601 int cmp;
602
603 strbuf_add_real_path(&target, path);
604 strbuf_strip_suffix(&target, "/.git");
605 strbuf_add_real_path(&maindir, get_git_common_dir());
606 strbuf_strip_suffix(&maindir, "/.git");
607 cmp = fspathcmp(maindir.buf, target.buf);
608
609 strbuf_release(&maindir);
610 strbuf_release(&target);
611 return !cmp;
612}
613
cf76baea
ES
614/*
615 * If both the main worktree and linked worktree have been moved, then the
616 * gitfile /path/to/worktree/.git won't point into the repository, thus we
617 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
618 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
619 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
620 */
621static char *infer_backlink(const char *gitfile)
622{
623 struct strbuf actual = STRBUF_INIT;
624 struct strbuf inferred = STRBUF_INIT;
625 const char *id;
626
627 if (strbuf_read_file(&actual, gitfile, 0) < 0)
628 goto error;
629 if (!starts_with(actual.buf, "gitdir:"))
630 goto error;
631 if (!(id = find_last_dir_sep(actual.buf)))
632 goto error;
633 strbuf_trim(&actual);
634 id++; /* advance past '/' to point at <id> */
635 if (!*id)
636 goto error;
637 strbuf_git_common_path(&inferred, the_repository, "worktrees/%s", id);
638 if (!is_directory(inferred.buf))
639 goto error;
640
641 strbuf_release(&actual);
642 return strbuf_detach(&inferred, NULL);
643
644error:
645 strbuf_release(&actual);
646 strbuf_release(&inferred);
647 return NULL;
648}
649
b214ab5a
ES
650/*
651 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
652 * the worktree's path.
653 */
654void repair_worktree_at_path(const char *path,
655 worktree_repair_fn fn, void *cb_data)
656{
657 struct strbuf dotgit = STRBUF_INIT;
658 struct strbuf realdotgit = STRBUF_INIT;
659 struct strbuf gitdir = STRBUF_INIT;
660 struct strbuf olddotgit = STRBUF_INIT;
661 char *backlink = NULL;
662 const char *repair = NULL;
663 int err;
664
665 if (!fn)
666 fn = repair_noop;
667
668 if (is_main_worktree_path(path))
669 goto done;
670
671 strbuf_addf(&dotgit, "%s/.git", path);
672 if (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {
673 fn(1, path, _("not a valid path"), cb_data);
674 goto done;
675 }
676
677 backlink = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));
678 if (err == READ_GITFILE_ERR_NOT_A_FILE) {
679 fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
680 goto done;
cf76baea
ES
681 } else if (err == READ_GITFILE_ERR_NOT_A_REPO) {
682 if (!(backlink = infer_backlink(realdotgit.buf))) {
683 fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
684 goto done;
685 }
b214ab5a
ES
686 } else if (err) {
687 fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
688 goto done;
689 }
690
691 strbuf_addf(&gitdir, "%s/gitdir", backlink);
692 if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
693 repair = _("gitdir unreadable");
694 else {
695 strbuf_rtrim(&olddotgit);
696 if (fspathcmp(olddotgit.buf, realdotgit.buf))
697 repair = _("gitdir incorrect");
698 }
699
700 if (repair) {
701 fn(0, gitdir.buf, repair, cb_data);
702 write_file(gitdir.buf, "%s", realdotgit.buf);
703 }
704done:
705 free(backlink);
706 strbuf_release(&olddotgit);
707 strbuf_release(&gitdir);
708 strbuf_release(&realdotgit);
709 strbuf_release(&dotgit);
710}
a29a8b75
RS
711
712int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire)
713{
714 struct stat st;
715 char *path;
716 int fd;
717 size_t len;
718 ssize_t read_result;
719
720 *wtpath = NULL;
721 if (!is_directory(git_path("worktrees/%s", id))) {
722 strbuf_addstr(reason, _("not a valid directory"));
723 return 1;
724 }
725 if (file_exists(git_path("worktrees/%s/locked", id)))
726 return 0;
727 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
728 strbuf_addstr(reason, _("gitdir file does not exist"));
729 return 1;
730 }
731 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
732 if (fd < 0) {
733 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
734 strerror(errno));
735 return 1;
736 }
737 len = xsize_t(st.st_size);
738 path = xmallocz(len);
739
740 read_result = read_in_full(fd, path, len);
741 if (read_result < 0) {
742 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
743 strerror(errno));
744 close(fd);
745 free(path);
746 return 1;
747 }
748 close(fd);
749
750 if (read_result != len) {
751 strbuf_addf(reason,
752 _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
753 (uintmax_t)len, (uintmax_t)read_result);
754 free(path);
755 return 1;
756 }
757 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
758 len--;
759 if (!len) {
760 strbuf_addstr(reason, _("invalid gitdir file"));
761 free(path);
762 return 1;
763 }
764 path[len] = '\0';
765 if (!file_exists(path)) {
766 if (stat(git_path("worktrees/%s/index", id), &st) ||
767 st.st_mtime <= expire) {
768 strbuf_addstr(reason, _("gitdir file points to non-existent location"));
769 free(path);
770 return 1;
771 } else {
772 *wtpath = path;
773 return 0;
774 }
775 }
776 *wtpath = path;
777 return 0;
778}
615a84ad
DS
779
780static int move_config_setting(const char *key, const char *value,
781 const char *from_file, const char *to_file)
782{
783 if (git_config_set_in_file_gently(to_file, key, value))
784 return error(_("unable to set %s in '%s'"), key, to_file);
785 if (git_config_set_in_file_gently(from_file, key, NULL))
786 return error(_("unable to unset %s in '%s'"), key, from_file);
787 return 0;
788}
789
790int init_worktree_config(struct repository *r)
791{
792 int res = 0;
793 int bare = 0;
794 struct config_set cs = { { 0 } };
795 const char *core_worktree;
796 char *common_config_file;
797 char *main_worktree_file;
798
799 /*
800 * If the extension is already enabled, then we can skip the
801 * upgrade process.
802 */
803 if (repository_format_worktree_config)
804 return 0;
805 if ((res = git_config_set_gently("extensions.worktreeConfig", "true")))
806 return error(_("failed to set extensions.worktreeConfig setting"));
807
808 common_config_file = xstrfmt("%s/config", r->commondir);
809 main_worktree_file = xstrfmt("%s/config.worktree", r->commondir);
810
811 git_configset_init(&cs);
812 git_configset_add_file(&cs, common_config_file);
813
814 /*
815 * If core.bare is true in the common config file, then we need to
816 * move it to the main worktree's config file or it will break all
817 * worktrees. If it is false, then leave it in place because it
818 * _could_ be negating a global core.bare=true.
819 */
820 if (!git_configset_get_bool(&cs, "core.bare", &bare) && bare) {
821 if ((res = move_config_setting("core.bare", "true",
822 common_config_file,
823 main_worktree_file)))
824 goto cleanup;
825 }
826 /*
827 * If core.worktree is set, then the main worktree is located
828 * somewhere different than the parent of the common Git dir.
829 * Relocate that value to avoid breaking all worktrees with this
830 * upgrade to worktree config.
831 */
832 if (!git_configset_get_value(&cs, "core.worktree", &core_worktree)) {
833 if ((res = move_config_setting("core.worktree", core_worktree,
834 common_config_file,
835 main_worktree_file)))
836 goto cleanup;
837 }
838
839 /*
840 * Ensure that we use worktree config for the remaining lifetime
841 * of the current process.
842 */
843 repository_format_worktree_config = 1;
844
845cleanup:
846 git_configset_clear(&cs);
847 free(common_config_file);
848 free(main_worktree_file);
849 return res;
850}