]> git.ipfire.org Git - thirdparty/git.git/blob - worktree.c
Merge branch 'jk/bundle-use-dash-for-stdfiles'
[thirdparty/git.git] / worktree.c
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "repository.h"
4 #include "refs.h"
5 #include "strbuf.h"
6 #include "worktree.h"
7 #include "dir.h"
8 #include "wt-status.h"
9 #include "config.h"
10
11 void free_worktrees(struct worktree **worktrees)
12 {
13 int i = 0;
14
15 for (i = 0; worktrees[i]; i++) {
16 free(worktrees[i]->path);
17 free(worktrees[i]->id);
18 free(worktrees[i]->head_ref);
19 free(worktrees[i]->lock_reason);
20 free(worktrees[i]->prune_reason);
21 free(worktrees[i]);
22 }
23 free (worktrees);
24 }
25
26 /**
27 * Update head_oid, head_ref and is_detached of the given worktree
28 */
29 static void add_head_info(struct worktree *wt)
30 {
31 int flags;
32 const char *target;
33
34 target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
35 "HEAD",
36 0,
37 &wt->head_oid, &flags);
38 if (!target)
39 return;
40
41 if (flags & REF_ISSYMREF)
42 wt->head_ref = xstrdup(target);
43 else
44 wt->is_detached = 1;
45 }
46
47 /**
48 * get the main worktree
49 */
50 static struct worktree *get_main_worktree(void)
51 {
52 struct worktree *worktree = NULL;
53 struct strbuf worktree_path = STRBUF_INIT;
54
55 strbuf_add_real_path(&worktree_path, get_git_common_dir());
56 strbuf_strip_suffix(&worktree_path, "/.git");
57
58 CALLOC_ARRAY(worktree, 1);
59 worktree->path = strbuf_detach(&worktree_path, NULL);
60 /*
61 * NEEDSWORK: If this function is called from a secondary worktree and
62 * config.worktree is present, is_bare_repository_cfg will reflect the
63 * contents of config.worktree, not the contents of the main worktree.
64 * This means that worktree->is_bare may be set to 0 even if the main
65 * worktree is configured to be bare.
66 */
67 worktree->is_bare = (is_bare_repository_cfg == 1) ||
68 is_bare_repository();
69 add_head_info(worktree);
70 return worktree;
71 }
72
73 static struct worktree *get_linked_worktree(const char *id)
74 {
75 struct worktree *worktree = NULL;
76 struct strbuf path = STRBUF_INIT;
77 struct strbuf worktree_path = STRBUF_INIT;
78
79 if (!id)
80 die("Missing linked worktree name");
81
82 strbuf_git_common_path(&path, the_repository, "worktrees/%s/gitdir", id);
83 if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
84 /* invalid gitdir file */
85 goto done;
86 strbuf_rtrim(&worktree_path);
87 strbuf_strip_suffix(&worktree_path, "/.git");
88
89 CALLOC_ARRAY(worktree, 1);
90 worktree->path = strbuf_detach(&worktree_path, NULL);
91 worktree->id = xstrdup(id);
92 add_head_info(worktree);
93
94 done:
95 strbuf_release(&path);
96 strbuf_release(&worktree_path);
97 return worktree;
98 }
99
100 static void mark_current_worktree(struct worktree **worktrees)
101 {
102 char *git_dir = absolute_pathdup(get_git_dir());
103 int i;
104
105 for (i = 0; worktrees[i]; i++) {
106 struct worktree *wt = worktrees[i];
107 const char *wt_git_dir = get_worktree_git_dir(wt);
108
109 if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
110 wt->is_current = 1;
111 break;
112 }
113 }
114 free(git_dir);
115 }
116
117 struct worktree **get_worktrees(void)
118 {
119 struct worktree **list = NULL;
120 struct strbuf path = STRBUF_INIT;
121 DIR *dir;
122 struct dirent *d;
123 int counter = 0, alloc = 2;
124
125 ALLOC_ARRAY(list, alloc);
126
127 list[counter++] = get_main_worktree();
128
129 strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
130 dir = opendir(path.buf);
131 strbuf_release(&path);
132 if (dir) {
133 while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {
134 struct worktree *linked = NULL;
135
136 if ((linked = get_linked_worktree(d->d_name))) {
137 ALLOC_GROW(list, counter + 1, alloc);
138 list[counter++] = linked;
139 }
140 }
141 closedir(dir);
142 }
143 ALLOC_GROW(list, counter + 1, alloc);
144 list[counter] = NULL;
145
146 mark_current_worktree(list);
147 return list;
148 }
149
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
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
185 struct worktree *find_worktree(struct worktree **list,
186 const char *prefix,
187 const char *arg)
188 {
189 struct worktree *wt;
190 char *to_free = NULL;
191
192 if ((wt = find_worktree_by_suffix(list, arg)))
193 return wt;
194
195 if (prefix)
196 arg = to_free = prefix_filename(prefix, arg);
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 {
204 struct strbuf wt_path = STRBUF_INIT;
205 char *path = real_pathdup(p, 0);
206
207 if (!path)
208 return NULL;
209 for (; *list; list++) {
210 if (!strbuf_realpath(&wt_path, (*list)->path, 0))
211 continue;
212
213 if (!fspathcmp(path, wt_path.buf))
214 break;
215 }
216 free(path);
217 strbuf_release(&wt_path);
218 return *list;
219 }
220
221 int is_main_worktree(const struct worktree *wt)
222 {
223 return !wt->id;
224 }
225
226 const char *worktree_lock_reason(struct worktree *wt)
227 {
228 if (is_main_worktree(wt))
229 return NULL;
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
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
269 /* convenient wrapper to deal with NULL strbuf */
270 __attribute__((format (printf, 2, 3)))
271 static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...)
272 {
273 va_list params;
274
275 if (!buf)
276 return;
277
278 va_start(params, fmt);
279 strbuf_vaddf(buf, fmt, params);
280 va_end(params);
281 }
282
283 int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
284 unsigned flags)
285 {
286 struct strbuf wt_path = STRBUF_INIT;
287 struct strbuf realpath = STRBUF_INIT;
288 char *path = NULL;
289 int err, ret = -1;
290
291 strbuf_addf(&wt_path, "%s/.git", wt->path);
292
293 if (is_main_worktree(wt)) {
294 if (is_directory(wt_path.buf)) {
295 ret = 0;
296 goto done;
297 }
298 /*
299 * Main worktree using .git file to point to the
300 * repository would make it impossible to know where
301 * the actual worktree is if this function is executed
302 * from another worktree. No .git file support for now.
303 */
304 strbuf_addf_gently(errmsg,
305 _("'%s' at main working tree is not the repository directory"),
306 wt_path.buf);
307 goto done;
308 }
309
310 /*
311 * Make sure "gitdir" file points to a real .git file and that
312 * file points back here.
313 */
314 if (!is_absolute_path(wt->path)) {
315 strbuf_addf_gently(errmsg,
316 _("'%s' file does not contain absolute path to the working tree location"),
317 git_common_path("worktrees/%s/gitdir", wt->id));
318 goto done;
319 }
320
321 if (flags & WT_VALIDATE_WORKTREE_MISSING_OK &&
322 !file_exists(wt->path)) {
323 ret = 0;
324 goto done;
325 }
326
327 if (!file_exists(wt_path.buf)) {
328 strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf);
329 goto done;
330 }
331
332 path = xstrdup_or_null(read_gitfile_gently(wt_path.buf, &err));
333 if (!path) {
334 strbuf_addf_gently(errmsg, _("'%s' is not a .git file, error code %d"),
335 wt_path.buf, err);
336 goto done;
337 }
338
339 strbuf_realpath(&realpath, git_common_path("worktrees/%s", wt->id), 1);
340 ret = fspathcmp(path, realpath.buf);
341
342 if (ret)
343 strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"),
344 wt->path, git_common_path("worktrees/%s", wt->id));
345 done:
346 free(path);
347 strbuf_release(&wt_path);
348 strbuf_release(&realpath);
349 return ret;
350 }
351
352 void update_worktree_location(struct worktree *wt, const char *path_)
353 {
354 struct strbuf path = STRBUF_INIT;
355
356 if (is_main_worktree(wt))
357 BUG("can't relocate main worktree");
358
359 strbuf_realpath(&path, path_, 1);
360 if (fspathcmp(wt->path, path.buf)) {
361 write_file(git_common_path("worktrees/%s/gitdir", wt->id),
362 "%s/.git", path.buf);
363 free(wt->path);
364 wt->path = strbuf_detach(&path, NULL);
365 }
366 strbuf_release(&path);
367 }
368
369 int is_worktree_being_rebased(const struct worktree *wt,
370 const char *target)
371 {
372 struct wt_status_state state;
373 int found_rebase;
374
375 memset(&state, 0, sizeof(state));
376 found_rebase = wt_status_check_rebase(wt, &state) &&
377 (state.rebase_in_progress ||
378 state.rebase_interactive_in_progress) &&
379 state.branch &&
380 skip_prefix(target, "refs/heads/", &target) &&
381 !strcmp(state.branch, target);
382 wt_status_state_free_buffers(&state);
383 return found_rebase;
384 }
385
386 int is_worktree_being_bisected(const struct worktree *wt,
387 const char *target)
388 {
389 struct wt_status_state state;
390 int found_bisect;
391
392 memset(&state, 0, sizeof(state));
393 found_bisect = wt_status_check_bisect(wt, &state) &&
394 state.branch &&
395 skip_prefix(target, "refs/heads/", &target) &&
396 !strcmp(state.branch, target);
397 wt_status_state_free_buffers(&state);
398 return found_bisect;
399 }
400
401 /*
402 * note: this function should be able to detect shared symref even if
403 * HEAD is temporarily detached (e.g. in the middle of rebase or
404 * bisect). New commands that do similar things should update this
405 * function as well.
406 */
407 int is_shared_symref(const struct worktree *wt, const char *symref,
408 const char *target)
409 {
410 const char *symref_target;
411 struct ref_store *refs;
412 int flags;
413
414 if (wt->is_bare)
415 return 0;
416
417 if (wt->is_detached && !strcmp(symref, "HEAD")) {
418 if (is_worktree_being_rebased(wt, target))
419 return 1;
420 if (is_worktree_being_bisected(wt, target))
421 return 1;
422 }
423
424 refs = get_worktree_ref_store(wt);
425 symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
426 NULL, &flags);
427 if ((flags & REF_ISSYMREF) &&
428 symref_target && !strcmp(symref_target, target))
429 return 1;
430
431 return 0;
432 }
433
434 const struct worktree *find_shared_symref(struct worktree **worktrees,
435 const char *symref,
436 const char *target)
437 {
438
439 for (int i = 0; worktrees[i]; i++)
440 if (is_shared_symref(worktrees[i], symref, target))
441 return worktrees[i];
442
443 return NULL;
444 }
445
446 int submodule_uses_worktrees(const char *path)
447 {
448 char *submodule_gitdir;
449 struct strbuf sb = STRBUF_INIT, err = STRBUF_INIT;
450 DIR *dir;
451 struct dirent *d;
452 int ret = 0;
453 struct repository_format format = REPOSITORY_FORMAT_INIT;
454
455 submodule_gitdir = git_pathdup_submodule(path, "%s", "");
456 if (!submodule_gitdir)
457 return 0;
458
459 /* The env would be set for the superproject. */
460 get_common_dir_noenv(&sb, submodule_gitdir);
461 free(submodule_gitdir);
462
463 strbuf_addstr(&sb, "/config");
464 read_repository_format(&format, sb.buf);
465 if (verify_repository_format(&format, &err)) {
466 strbuf_release(&err);
467 strbuf_release(&sb);
468 clear_repository_format(&format);
469 return 1;
470 }
471 clear_repository_format(&format);
472 strbuf_release(&err);
473
474 /* Replace config by worktrees. */
475 strbuf_setlen(&sb, sb.len - strlen("config"));
476 strbuf_addstr(&sb, "worktrees");
477
478 /* See if there is any file inside the worktrees directory. */
479 dir = opendir(sb.buf);
480 strbuf_release(&sb);
481
482 if (!dir)
483 return 0;
484
485 d = readdir_skip_dot_and_dotdot(dir);
486 if (d)
487 ret = 1;
488 closedir(dir);
489 return ret;
490 }
491
492 void strbuf_worktree_ref(const struct worktree *wt,
493 struct strbuf *sb,
494 const char *refname)
495 {
496 if (parse_worktree_ref(refname, NULL, NULL, NULL) ==
497 REF_WORKTREE_CURRENT &&
498 wt && !wt->is_current) {
499 if (is_main_worktree(wt))
500 strbuf_addstr(sb, "main-worktree/");
501 else
502 strbuf_addf(sb, "worktrees/%s/", wt->id);
503 }
504 strbuf_addstr(sb, refname);
505 }
506
507 int other_head_refs(each_ref_fn fn, void *cb_data)
508 {
509 struct worktree **worktrees, **p;
510 struct strbuf refname = STRBUF_INIT;
511 int ret = 0;
512
513 worktrees = get_worktrees();
514 for (p = worktrees; *p; p++) {
515 struct worktree *wt = *p;
516 struct object_id oid;
517 int flag;
518
519 if (wt->is_current)
520 continue;
521
522 strbuf_reset(&refname);
523 strbuf_worktree_ref(wt, &refname, "HEAD");
524 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
525 refname.buf,
526 RESOLVE_REF_READING,
527 &oid, &flag))
528 ret = fn(refname.buf, &oid, flag, cb_data);
529 if (ret)
530 break;
531 }
532 free_worktrees(worktrees);
533 strbuf_release(&refname);
534 return ret;
535 }
536
537 /*
538 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
539 * pointing at <repo>/worktrees/<id>.
540 */
541 static void repair_gitfile(struct worktree *wt,
542 worktree_repair_fn fn, void *cb_data)
543 {
544 struct strbuf dotgit = STRBUF_INIT;
545 struct strbuf repo = STRBUF_INIT;
546 char *backlink;
547 const char *repair = NULL;
548 int err;
549
550 /* missing worktree can't be repaired */
551 if (!file_exists(wt->path))
552 return;
553
554 if (!is_directory(wt->path)) {
555 fn(1, wt->path, _("not a directory"), cb_data);
556 return;
557 }
558
559 strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
560 strbuf_addf(&dotgit, "%s/.git", wt->path);
561 backlink = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
562
563 if (err == READ_GITFILE_ERR_NOT_A_FILE)
564 fn(1, wt->path, _(".git is not a file"), cb_data);
565 else if (err)
566 repair = _(".git file broken");
567 else if (fspathcmp(backlink, repo.buf))
568 repair = _(".git file incorrect");
569
570 if (repair) {
571 fn(0, wt->path, repair, cb_data);
572 write_file(dotgit.buf, "gitdir: %s", repo.buf);
573 }
574
575 free(backlink);
576 strbuf_release(&repo);
577 strbuf_release(&dotgit);
578 }
579
580 static void repair_noop(int iserr, const char *path, const char *msg,
581 void *cb_data)
582 {
583 /* nothing */
584 }
585
586 void repair_worktrees(worktree_repair_fn fn, void *cb_data)
587 {
588 struct worktree **worktrees = get_worktrees();
589 struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
590
591 if (!fn)
592 fn = repair_noop;
593 for (; *wt; wt++)
594 repair_gitfile(*wt, fn, cb_data);
595 free_worktrees(worktrees);
596 }
597
598 static int is_main_worktree_path(const char *path)
599 {
600 struct strbuf target = STRBUF_INIT;
601 struct strbuf maindir = STRBUF_INIT;
602 int cmp;
603
604 strbuf_add_real_path(&target, path);
605 strbuf_strip_suffix(&target, "/.git");
606 strbuf_add_real_path(&maindir, get_git_common_dir());
607 strbuf_strip_suffix(&maindir, "/.git");
608 cmp = fspathcmp(maindir.buf, target.buf);
609
610 strbuf_release(&maindir);
611 strbuf_release(&target);
612 return !cmp;
613 }
614
615 /*
616 * If both the main worktree and linked worktree have been moved, then the
617 * gitfile /path/to/worktree/.git won't point into the repository, thus we
618 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
619 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
620 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
621 */
622 static char *infer_backlink(const char *gitfile)
623 {
624 struct strbuf actual = STRBUF_INIT;
625 struct strbuf inferred = STRBUF_INIT;
626 const char *id;
627
628 if (strbuf_read_file(&actual, gitfile, 0) < 0)
629 goto error;
630 if (!starts_with(actual.buf, "gitdir:"))
631 goto error;
632 if (!(id = find_last_dir_sep(actual.buf)))
633 goto error;
634 strbuf_trim(&actual);
635 id++; /* advance past '/' to point at <id> */
636 if (!*id)
637 goto error;
638 strbuf_git_common_path(&inferred, the_repository, "worktrees/%s", id);
639 if (!is_directory(inferred.buf))
640 goto error;
641
642 strbuf_release(&actual);
643 return strbuf_detach(&inferred, NULL);
644
645 error:
646 strbuf_release(&actual);
647 strbuf_release(&inferred);
648 return NULL;
649 }
650
651 /*
652 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
653 * the worktree's path.
654 */
655 void repair_worktree_at_path(const char *path,
656 worktree_repair_fn fn, void *cb_data)
657 {
658 struct strbuf dotgit = STRBUF_INIT;
659 struct strbuf realdotgit = STRBUF_INIT;
660 struct strbuf gitdir = STRBUF_INIT;
661 struct strbuf olddotgit = STRBUF_INIT;
662 char *backlink = NULL;
663 const char *repair = NULL;
664 int err;
665
666 if (!fn)
667 fn = repair_noop;
668
669 if (is_main_worktree_path(path))
670 goto done;
671
672 strbuf_addf(&dotgit, "%s/.git", path);
673 if (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {
674 fn(1, path, _("not a valid path"), cb_data);
675 goto done;
676 }
677
678 backlink = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));
679 if (err == READ_GITFILE_ERR_NOT_A_FILE) {
680 fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
681 goto done;
682 } else if (err == READ_GITFILE_ERR_NOT_A_REPO) {
683 if (!(backlink = infer_backlink(realdotgit.buf))) {
684 fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
685 goto done;
686 }
687 } else if (err) {
688 fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
689 goto done;
690 }
691
692 strbuf_addf(&gitdir, "%s/gitdir", backlink);
693 if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
694 repair = _("gitdir unreadable");
695 else {
696 strbuf_rtrim(&olddotgit);
697 if (fspathcmp(olddotgit.buf, realdotgit.buf))
698 repair = _("gitdir incorrect");
699 }
700
701 if (repair) {
702 fn(0, gitdir.buf, repair, cb_data);
703 write_file(gitdir.buf, "%s", realdotgit.buf);
704 }
705 done:
706 free(backlink);
707 strbuf_release(&olddotgit);
708 strbuf_release(&gitdir);
709 strbuf_release(&realdotgit);
710 strbuf_release(&dotgit);
711 }
712
713 int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire)
714 {
715 struct stat st;
716 char *path;
717 int fd;
718 size_t len;
719 ssize_t read_result;
720
721 *wtpath = NULL;
722 if (!is_directory(git_path("worktrees/%s", id))) {
723 strbuf_addstr(reason, _("not a valid directory"));
724 return 1;
725 }
726 if (file_exists(git_path("worktrees/%s/locked", id)))
727 return 0;
728 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
729 strbuf_addstr(reason, _("gitdir file does not exist"));
730 return 1;
731 }
732 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
733 if (fd < 0) {
734 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
735 strerror(errno));
736 return 1;
737 }
738 len = xsize_t(st.st_size);
739 path = xmallocz(len);
740
741 read_result = read_in_full(fd, path, len);
742 if (read_result < 0) {
743 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
744 strerror(errno));
745 close(fd);
746 free(path);
747 return 1;
748 }
749 close(fd);
750
751 if (read_result != len) {
752 strbuf_addf(reason,
753 _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
754 (uintmax_t)len, (uintmax_t)read_result);
755 free(path);
756 return 1;
757 }
758 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
759 len--;
760 if (!len) {
761 strbuf_addstr(reason, _("invalid gitdir file"));
762 free(path);
763 return 1;
764 }
765 path[len] = '\0';
766 if (!file_exists(path)) {
767 if (stat(git_path("worktrees/%s/index", id), &st) ||
768 st.st_mtime <= expire) {
769 strbuf_addstr(reason, _("gitdir file points to non-existent location"));
770 free(path);
771 return 1;
772 } else {
773 *wtpath = path;
774 return 0;
775 }
776 }
777 *wtpath = path;
778 return 0;
779 }
780
781 static int move_config_setting(const char *key, const char *value,
782 const char *from_file, const char *to_file)
783 {
784 if (git_config_set_in_file_gently(to_file, key, value))
785 return error(_("unable to set %s in '%s'"), key, to_file);
786 if (git_config_set_in_file_gently(from_file, key, NULL))
787 return error(_("unable to unset %s in '%s'"), key, from_file);
788 return 0;
789 }
790
791 int init_worktree_config(struct repository *r)
792 {
793 int res = 0;
794 int bare = 0;
795 struct config_set cs = { { 0 } };
796 const char *core_worktree;
797 char *common_config_file;
798 char *main_worktree_file;
799
800 /*
801 * If the extension is already enabled, then we can skip the
802 * upgrade process.
803 */
804 if (repository_format_worktree_config)
805 return 0;
806 if ((res = git_config_set_gently("extensions.worktreeConfig", "true")))
807 return error(_("failed to set extensions.worktreeConfig setting"));
808
809 common_config_file = xstrfmt("%s/config", r->commondir);
810 main_worktree_file = xstrfmt("%s/config.worktree", r->commondir);
811
812 git_configset_init(&cs);
813 git_configset_add_file(&cs, common_config_file);
814
815 /*
816 * If core.bare is true in the common config file, then we need to
817 * move it to the main worktree's config file or it will break all
818 * worktrees. If it is false, then leave it in place because it
819 * _could_ be negating a global core.bare=true.
820 */
821 if (!git_configset_get_bool(&cs, "core.bare", &bare) && bare) {
822 if ((res = move_config_setting("core.bare", "true",
823 common_config_file,
824 main_worktree_file)))
825 goto cleanup;
826 }
827 /*
828 * If core.worktree is set, then the main worktree is located
829 * somewhere different than the parent of the common Git dir.
830 * Relocate that value to avoid breaking all worktrees with this
831 * upgrade to worktree config.
832 */
833 if (!git_configset_get_value(&cs, "core.worktree", &core_worktree)) {
834 if ((res = move_config_setting("core.worktree", core_worktree,
835 common_config_file,
836 main_worktree_file)))
837 goto cleanup;
838 }
839
840 /*
841 * Ensure that we use worktree config for the remaining lifetime
842 * of the current process.
843 */
844 repository_format_worktree_config = 1;
845
846 cleanup:
847 git_configset_clear(&cs);
848 free(common_config_file);
849 free(main_worktree_file);
850 return res;
851 }