Merge tag 'v2.41.0-rc0'
[alt-git.git] / worktree.c
blobb5ee71c5ebda499899e5f64209611f0484359867
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "alloc.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "repository.h"
7 #include "refs.h"
8 #include "setup.h"
9 #include "strbuf.h"
10 #include "worktree.h"
11 #include "dir.h"
12 #include "wt-status.h"
13 #include "config.h"
14 #include "wrapper.h"
16 void free_worktrees(struct worktree **worktrees)
18 int i = 0;
20 for (i = 0; worktrees[i]; i++) {
21 free(worktrees[i]->path);
22 free(worktrees[i]->id);
23 free(worktrees[i]->head_ref);
24 free(worktrees[i]->lock_reason);
25 free(worktrees[i]->prune_reason);
26 free(worktrees[i]);
28 free (worktrees);
31 /**
32 * Update head_oid, head_ref and is_detached of the given worktree
34 static void add_head_info(struct worktree *wt)
36 int flags;
37 const char *target;
39 target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
40 "HEAD",
42 &wt->head_oid, &flags);
43 if (!target)
44 return;
46 if (flags & REF_ISSYMREF)
47 wt->head_ref = xstrdup(target);
48 else
49 wt->is_detached = 1;
52 /**
53 * get the main worktree
55 static struct worktree *get_main_worktree(void)
57 struct worktree *worktree = NULL;
58 struct strbuf worktree_path = STRBUF_INIT;
60 strbuf_add_real_path(&worktree_path, get_git_common_dir());
61 strbuf_strip_suffix(&worktree_path, "/.git");
63 CALLOC_ARRAY(worktree, 1);
64 worktree->path = strbuf_detach(&worktree_path, NULL);
66 * NEEDSWORK: If this function is called from a secondary worktree and
67 * config.worktree is present, is_bare_repository_cfg will reflect the
68 * contents of config.worktree, not the contents of the main worktree.
69 * This means that worktree->is_bare may be set to 0 even if the main
70 * worktree is configured to be bare.
72 worktree->is_bare = (is_bare_repository_cfg == 1) ||
73 is_bare_repository();
74 add_head_info(worktree);
75 return worktree;
78 static struct worktree *get_linked_worktree(const char *id)
80 struct worktree *worktree = NULL;
81 struct strbuf path = STRBUF_INIT;
82 struct strbuf worktree_path = STRBUF_INIT;
84 if (!id)
85 die("Missing linked worktree name");
87 strbuf_git_common_path(&path, the_repository, "worktrees/%s/gitdir", id);
88 if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
89 /* invalid gitdir file */
90 goto done;
91 strbuf_rtrim(&worktree_path);
92 strbuf_strip_suffix(&worktree_path, "/.git");
94 CALLOC_ARRAY(worktree, 1);
95 worktree->path = strbuf_detach(&worktree_path, NULL);
96 worktree->id = xstrdup(id);
97 add_head_info(worktree);
99 done:
100 strbuf_release(&path);
101 strbuf_release(&worktree_path);
102 return worktree;
105 static void mark_current_worktree(struct worktree **worktrees)
107 char *git_dir = absolute_pathdup(get_git_dir());
108 int i;
110 for (i = 0; worktrees[i]; i++) {
111 struct worktree *wt = worktrees[i];
112 const char *wt_git_dir = get_worktree_git_dir(wt);
114 if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
115 wt->is_current = 1;
116 break;
119 free(git_dir);
122 struct worktree **get_worktrees(void)
124 struct worktree **list = NULL;
125 struct strbuf path = STRBUF_INIT;
126 DIR *dir;
127 struct dirent *d;
128 int counter = 0, alloc = 2;
130 ALLOC_ARRAY(list, alloc);
132 list[counter++] = get_main_worktree();
134 strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
135 dir = opendir(path.buf);
136 strbuf_release(&path);
137 if (dir) {
138 while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {
139 struct worktree *linked = NULL;
141 if ((linked = get_linked_worktree(d->d_name))) {
142 ALLOC_GROW(list, counter + 1, alloc);
143 list[counter++] = linked;
146 closedir(dir);
148 ALLOC_GROW(list, counter + 1, alloc);
149 list[counter] = NULL;
151 mark_current_worktree(list);
152 return list;
155 const char *get_worktree_git_dir(const struct worktree *wt)
157 if (!wt)
158 return get_git_dir();
159 else if (!wt->id)
160 return get_git_common_dir();
161 else
162 return git_common_path("worktrees/%s", wt->id);
165 static struct worktree *find_worktree_by_suffix(struct worktree **list,
166 const char *suffix)
168 struct worktree *found = NULL;
169 int nr_found = 0, suffixlen;
171 suffixlen = strlen(suffix);
172 if (!suffixlen)
173 return NULL;
175 for (; *list && nr_found < 2; list++) {
176 const char *path = (*list)->path;
177 int pathlen = strlen(path);
178 int start = pathlen - suffixlen;
180 /* suffix must start at directory boundary */
181 if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) &&
182 !fspathcmp(suffix, path + start)) {
183 found = *list;
184 nr_found++;
187 return nr_found == 1 ? found : NULL;
190 struct worktree *find_worktree(struct worktree **list,
191 const char *prefix,
192 const char *arg)
194 struct worktree *wt;
195 char *to_free = NULL;
197 if ((wt = find_worktree_by_suffix(list, arg)))
198 return wt;
200 if (prefix)
201 arg = to_free = prefix_filename(prefix, arg);
202 wt = find_worktree_by_path(list, arg);
203 free(to_free);
204 return wt;
207 struct worktree *find_worktree_by_path(struct worktree **list, const char *p)
209 struct strbuf wt_path = STRBUF_INIT;
210 char *path = real_pathdup(p, 0);
212 if (!path)
213 return NULL;
214 for (; *list; list++) {
215 if (!strbuf_realpath(&wt_path, (*list)->path, 0))
216 continue;
218 if (!fspathcmp(path, wt_path.buf))
219 break;
221 free(path);
222 strbuf_release(&wt_path);
223 return *list;
226 int is_main_worktree(const struct worktree *wt)
228 return !wt->id;
231 const char *worktree_lock_reason(struct worktree *wt)
233 if (is_main_worktree(wt))
234 return NULL;
236 if (!wt->lock_reason_valid) {
237 struct strbuf path = STRBUF_INIT;
239 strbuf_addstr(&path, worktree_git_path(wt, "locked"));
240 if (file_exists(path.buf)) {
241 struct strbuf lock_reason = STRBUF_INIT;
242 if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
243 die_errno(_("failed to read '%s'"), path.buf);
244 strbuf_trim(&lock_reason);
245 wt->lock_reason = strbuf_detach(&lock_reason, NULL);
246 } else
247 wt->lock_reason = NULL;
248 wt->lock_reason_valid = 1;
249 strbuf_release(&path);
252 return wt->lock_reason;
255 const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire)
257 struct strbuf reason = STRBUF_INIT;
258 char *path = NULL;
260 if (is_main_worktree(wt))
261 return NULL;
262 if (wt->prune_reason_valid)
263 return wt->prune_reason;
265 if (should_prune_worktree(wt->id, &reason, &path, expire))
266 wt->prune_reason = strbuf_detach(&reason, NULL);
267 wt->prune_reason_valid = 1;
269 strbuf_release(&reason);
270 free(path);
271 return wt->prune_reason;
274 /* convenient wrapper to deal with NULL strbuf */
275 __attribute__((format (printf, 2, 3)))
276 static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...)
278 va_list params;
280 if (!buf)
281 return;
283 va_start(params, fmt);
284 strbuf_vaddf(buf, fmt, params);
285 va_end(params);
288 int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
289 unsigned flags)
291 struct strbuf wt_path = STRBUF_INIT;
292 struct strbuf realpath = STRBUF_INIT;
293 char *path = NULL;
294 int err, ret = -1;
296 strbuf_addf(&wt_path, "%s/.git", wt->path);
298 if (is_main_worktree(wt)) {
299 if (is_directory(wt_path.buf)) {
300 ret = 0;
301 goto done;
304 * Main worktree using .git file to point to the
305 * repository would make it impossible to know where
306 * the actual worktree is if this function is executed
307 * from another worktree. No .git file support for now.
309 strbuf_addf_gently(errmsg,
310 _("'%s' at main working tree is not the repository directory"),
311 wt_path.buf);
312 goto done;
316 * Make sure "gitdir" file points to a real .git file and that
317 * file points back here.
319 if (!is_absolute_path(wt->path)) {
320 strbuf_addf_gently(errmsg,
321 _("'%s' file does not contain absolute path to the working tree location"),
322 git_common_path("worktrees/%s/gitdir", wt->id));
323 goto done;
326 if (flags & WT_VALIDATE_WORKTREE_MISSING_OK &&
327 !file_exists(wt->path)) {
328 ret = 0;
329 goto done;
332 if (!file_exists(wt_path.buf)) {
333 strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf);
334 goto done;
337 path = xstrdup_or_null(read_gitfile_gently(wt_path.buf, &err));
338 if (!path) {
339 strbuf_addf_gently(errmsg, _("'%s' is not a .git file, error code %d"),
340 wt_path.buf, err);
341 goto done;
344 strbuf_realpath(&realpath, git_common_path("worktrees/%s", wt->id), 1);
345 ret = fspathcmp(path, realpath.buf);
347 if (ret)
348 strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"),
349 wt->path, git_common_path("worktrees/%s", wt->id));
350 done:
351 free(path);
352 strbuf_release(&wt_path);
353 strbuf_release(&realpath);
354 return ret;
357 void update_worktree_location(struct worktree *wt, const char *path_)
359 struct strbuf path = STRBUF_INIT;
361 if (is_main_worktree(wt))
362 BUG("can't relocate main worktree");
364 strbuf_realpath(&path, path_, 1);
365 if (fspathcmp(wt->path, path.buf)) {
366 write_file(git_common_path("worktrees/%s/gitdir", wt->id),
367 "%s/.git", path.buf);
368 free(wt->path);
369 wt->path = strbuf_detach(&path, NULL);
371 strbuf_release(&path);
374 int is_worktree_being_rebased(const struct worktree *wt,
375 const char *target)
377 struct wt_status_state state;
378 int found_rebase;
380 memset(&state, 0, sizeof(state));
381 found_rebase = wt_status_check_rebase(wt, &state) &&
382 (state.rebase_in_progress ||
383 state.rebase_interactive_in_progress) &&
384 state.branch &&
385 skip_prefix(target, "refs/heads/", &target) &&
386 !strcmp(state.branch, target);
387 wt_status_state_free_buffers(&state);
388 return found_rebase;
391 int is_worktree_being_bisected(const struct worktree *wt,
392 const char *target)
394 struct wt_status_state state;
395 int found_bisect;
397 memset(&state, 0, sizeof(state));
398 found_bisect = wt_status_check_bisect(wt, &state) &&
399 state.branch &&
400 skip_prefix(target, "refs/heads/", &target) &&
401 !strcmp(state.branch, target);
402 wt_status_state_free_buffers(&state);
403 return found_bisect;
407 * note: this function should be able to detect shared symref even if
408 * HEAD is temporarily detached (e.g. in the middle of rebase or
409 * bisect). New commands that do similar things should update this
410 * function as well.
412 int is_shared_symref(const struct worktree *wt, const char *symref,
413 const char *target)
415 const char *symref_target;
416 struct ref_store *refs;
417 int flags;
419 if (wt->is_bare)
420 return 0;
422 if (wt->is_detached && !strcmp(symref, "HEAD")) {
423 if (is_worktree_being_rebased(wt, target))
424 return 1;
425 if (is_worktree_being_bisected(wt, target))
426 return 1;
429 refs = get_worktree_ref_store(wt);
430 symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
431 NULL, &flags);
432 if ((flags & REF_ISSYMREF) &&
433 symref_target && !strcmp(symref_target, target))
434 return 1;
436 return 0;
439 const struct worktree *find_shared_symref(struct worktree **worktrees,
440 const char *symref,
441 const char *target)
444 for (int i = 0; worktrees[i]; i++)
445 if (is_shared_symref(worktrees[i], symref, target))
446 return worktrees[i];
448 return NULL;
451 int submodule_uses_worktrees(const char *path)
453 char *submodule_gitdir;
454 struct strbuf sb = STRBUF_INIT, err = STRBUF_INIT;
455 DIR *dir;
456 struct dirent *d;
457 int ret = 0;
458 struct repository_format format = REPOSITORY_FORMAT_INIT;
460 submodule_gitdir = git_pathdup_submodule(path, "%s", "");
461 if (!submodule_gitdir)
462 return 0;
464 /* The env would be set for the superproject. */
465 get_common_dir_noenv(&sb, submodule_gitdir);
466 free(submodule_gitdir);
468 strbuf_addstr(&sb, "/config");
469 read_repository_format(&format, sb.buf);
470 if (verify_repository_format(&format, &err)) {
471 strbuf_release(&err);
472 strbuf_release(&sb);
473 clear_repository_format(&format);
474 return 1;
476 clear_repository_format(&format);
477 strbuf_release(&err);
479 /* Replace config by worktrees. */
480 strbuf_setlen(&sb, sb.len - strlen("config"));
481 strbuf_addstr(&sb, "worktrees");
483 /* See if there is any file inside the worktrees directory. */
484 dir = opendir(sb.buf);
485 strbuf_release(&sb);
487 if (!dir)
488 return 0;
490 d = readdir_skip_dot_and_dotdot(dir);
491 if (d)
492 ret = 1;
493 closedir(dir);
494 return ret;
497 void strbuf_worktree_ref(const struct worktree *wt,
498 struct strbuf *sb,
499 const char *refname)
501 if (parse_worktree_ref(refname, NULL, NULL, NULL) ==
502 REF_WORKTREE_CURRENT &&
503 wt && !wt->is_current) {
504 if (is_main_worktree(wt))
505 strbuf_addstr(sb, "main-worktree/");
506 else
507 strbuf_addf(sb, "worktrees/%s/", wt->id);
509 strbuf_addstr(sb, refname);
512 int other_head_refs(each_ref_fn fn, void *cb_data)
514 struct worktree **worktrees, **p;
515 struct strbuf refname = STRBUF_INIT;
516 int ret = 0;
518 worktrees = get_worktrees();
519 for (p = worktrees; *p; p++) {
520 struct worktree *wt = *p;
521 struct object_id oid;
522 int flag;
524 if (wt->is_current)
525 continue;
527 strbuf_reset(&refname);
528 strbuf_worktree_ref(wt, &refname, "HEAD");
529 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
530 refname.buf,
531 RESOLVE_REF_READING,
532 &oid, &flag))
533 ret = fn(refname.buf, &oid, flag, cb_data);
534 if (ret)
535 break;
537 free_worktrees(worktrees);
538 strbuf_release(&refname);
539 return ret;
543 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
544 * pointing at <repo>/worktrees/<id>.
546 static void repair_gitfile(struct worktree *wt,
547 worktree_repair_fn fn, void *cb_data)
549 struct strbuf dotgit = STRBUF_INIT;
550 struct strbuf repo = STRBUF_INIT;
551 char *backlink;
552 const char *repair = NULL;
553 int err;
555 /* missing worktree can't be repaired */
556 if (!file_exists(wt->path))
557 return;
559 if (!is_directory(wt->path)) {
560 fn(1, wt->path, _("not a directory"), cb_data);
561 return;
564 strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
565 strbuf_addf(&dotgit, "%s/.git", wt->path);
566 backlink = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
568 if (err == READ_GITFILE_ERR_NOT_A_FILE)
569 fn(1, wt->path, _(".git is not a file"), cb_data);
570 else if (err)
571 repair = _(".git file broken");
572 else if (fspathcmp(backlink, repo.buf))
573 repair = _(".git file incorrect");
575 if (repair) {
576 fn(0, wt->path, repair, cb_data);
577 write_file(dotgit.buf, "gitdir: %s", repo.buf);
580 free(backlink);
581 strbuf_release(&repo);
582 strbuf_release(&dotgit);
585 static void repair_noop(int iserr, const char *path, const char *msg,
586 void *cb_data)
588 /* nothing */
591 void repair_worktrees(worktree_repair_fn fn, void *cb_data)
593 struct worktree **worktrees = get_worktrees();
594 struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
596 if (!fn)
597 fn = repair_noop;
598 for (; *wt; wt++)
599 repair_gitfile(*wt, fn, cb_data);
600 free_worktrees(worktrees);
603 static int is_main_worktree_path(const char *path)
605 struct strbuf target = STRBUF_INIT;
606 struct strbuf maindir = STRBUF_INIT;
607 int cmp;
609 strbuf_add_real_path(&target, path);
610 strbuf_strip_suffix(&target, "/.git");
611 strbuf_add_real_path(&maindir, get_git_common_dir());
612 strbuf_strip_suffix(&maindir, "/.git");
613 cmp = fspathcmp(maindir.buf, target.buf);
615 strbuf_release(&maindir);
616 strbuf_release(&target);
617 return !cmp;
621 * If both the main worktree and linked worktree have been moved, then the
622 * gitfile /path/to/worktree/.git won't point into the repository, thus we
623 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
624 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
625 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
627 static char *infer_backlink(const char *gitfile)
629 struct strbuf actual = STRBUF_INIT;
630 struct strbuf inferred = STRBUF_INIT;
631 const char *id;
633 if (strbuf_read_file(&actual, gitfile, 0) < 0)
634 goto error;
635 if (!starts_with(actual.buf, "gitdir:"))
636 goto error;
637 if (!(id = find_last_dir_sep(actual.buf)))
638 goto error;
639 strbuf_trim(&actual);
640 id++; /* advance past '/' to point at <id> */
641 if (!*id)
642 goto error;
643 strbuf_git_common_path(&inferred, the_repository, "worktrees/%s", id);
644 if (!is_directory(inferred.buf))
645 goto error;
647 strbuf_release(&actual);
648 return strbuf_detach(&inferred, NULL);
650 error:
651 strbuf_release(&actual);
652 strbuf_release(&inferred);
653 return NULL;
657 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
658 * the worktree's path.
660 void repair_worktree_at_path(const char *path,
661 worktree_repair_fn fn, void *cb_data)
663 struct strbuf dotgit = STRBUF_INIT;
664 struct strbuf realdotgit = STRBUF_INIT;
665 struct strbuf gitdir = STRBUF_INIT;
666 struct strbuf olddotgit = STRBUF_INIT;
667 char *backlink = NULL;
668 const char *repair = NULL;
669 int err;
671 if (!fn)
672 fn = repair_noop;
674 if (is_main_worktree_path(path))
675 goto done;
677 strbuf_addf(&dotgit, "%s/.git", path);
678 if (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {
679 fn(1, path, _("not a valid path"), cb_data);
680 goto done;
683 backlink = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));
684 if (err == READ_GITFILE_ERR_NOT_A_FILE) {
685 fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
686 goto done;
687 } else if (err == READ_GITFILE_ERR_NOT_A_REPO) {
688 if (!(backlink = infer_backlink(realdotgit.buf))) {
689 fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
690 goto done;
692 } else if (err) {
693 fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
694 goto done;
697 strbuf_addf(&gitdir, "%s/gitdir", backlink);
698 if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
699 repair = _("gitdir unreadable");
700 else {
701 strbuf_rtrim(&olddotgit);
702 if (fspathcmp(olddotgit.buf, realdotgit.buf))
703 repair = _("gitdir incorrect");
706 if (repair) {
707 fn(0, gitdir.buf, repair, cb_data);
708 write_file(gitdir.buf, "%s", realdotgit.buf);
710 done:
711 free(backlink);
712 strbuf_release(&olddotgit);
713 strbuf_release(&gitdir);
714 strbuf_release(&realdotgit);
715 strbuf_release(&dotgit);
718 int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire)
720 struct stat st;
721 char *path;
722 int fd;
723 size_t len;
724 ssize_t read_result;
726 *wtpath = NULL;
727 if (!is_directory(git_path("worktrees/%s", id))) {
728 strbuf_addstr(reason, _("not a valid directory"));
729 return 1;
731 if (file_exists(git_path("worktrees/%s/locked", id)))
732 return 0;
733 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
734 strbuf_addstr(reason, _("gitdir file does not exist"));
735 return 1;
737 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
738 if (fd < 0) {
739 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
740 strerror(errno));
741 return 1;
743 len = xsize_t(st.st_size);
744 path = xmallocz(len);
746 read_result = read_in_full(fd, path, len);
747 if (read_result < 0) {
748 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
749 strerror(errno));
750 close(fd);
751 free(path);
752 return 1;
754 close(fd);
756 if (read_result != len) {
757 strbuf_addf(reason,
758 _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
759 (uintmax_t)len, (uintmax_t)read_result);
760 free(path);
761 return 1;
763 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
764 len--;
765 if (!len) {
766 strbuf_addstr(reason, _("invalid gitdir file"));
767 free(path);
768 return 1;
770 path[len] = '\0';
771 if (!file_exists(path)) {
772 if (stat(git_path("worktrees/%s/index", id), &st) ||
773 st.st_mtime <= expire) {
774 strbuf_addstr(reason, _("gitdir file points to non-existent location"));
775 free(path);
776 return 1;
777 } else {
778 *wtpath = path;
779 return 0;
782 *wtpath = path;
783 return 0;
786 static int move_config_setting(const char *key, const char *value,
787 const char *from_file, const char *to_file)
789 if (git_config_set_in_file_gently(to_file, key, value))
790 return error(_("unable to set %s in '%s'"), key, to_file);
791 if (git_config_set_in_file_gently(from_file, key, NULL))
792 return error(_("unable to unset %s in '%s'"), key, from_file);
793 return 0;
796 int init_worktree_config(struct repository *r)
798 int res = 0;
799 int bare = 0;
800 struct config_set cs = { { 0 } };
801 const char *core_worktree;
802 char *common_config_file;
803 char *main_worktree_file;
806 * If the extension is already enabled, then we can skip the
807 * upgrade process.
809 if (repository_format_worktree_config)
810 return 0;
811 if ((res = git_config_set_gently("extensions.worktreeConfig", "true")))
812 return error(_("failed to set extensions.worktreeConfig setting"));
814 common_config_file = xstrfmt("%s/config", r->commondir);
815 main_worktree_file = xstrfmt("%s/config.worktree", r->commondir);
817 git_configset_init(&cs);
818 git_configset_add_file(&cs, common_config_file);
821 * If core.bare is true in the common config file, then we need to
822 * move it to the main worktree's config file or it will break all
823 * worktrees. If it is false, then leave it in place because it
824 * _could_ be negating a global core.bare=true.
826 if (!git_configset_get_bool(&cs, "core.bare", &bare) && bare) {
827 if ((res = move_config_setting("core.bare", "true",
828 common_config_file,
829 main_worktree_file)))
830 goto cleanup;
833 * If core.worktree is set, then the main worktree is located
834 * somewhere different than the parent of the common Git dir.
835 * Relocate that value to avoid breaking all worktrees with this
836 * upgrade to worktree config.
838 if (!git_configset_get_value(&cs, "core.worktree", &core_worktree)) {
839 if ((res = move_config_setting("core.worktree", core_worktree,
840 common_config_file,
841 main_worktree_file)))
842 goto cleanup;
846 * Ensure that we use worktree config for the remaining lifetime
847 * of the current process.
849 repository_format_worktree_config = 1;
851 cleanup:
852 git_configset_clear(&cs);
853 free(common_config_file);
854 free(main_worktree_file);
855 return res;