Merge branch 'gt/unit-test-strcmp-offset'
[git.git] / worktree.c
blob12eadacc618a1db5e734bea16a5fb7ae15085131
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "environment.h"
4 #include "gettext.h"
5 #include "path.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"
15 void free_worktree(struct worktree *worktree)
17 if (!worktree)
18 return;
19 free(worktree->path);
20 free(worktree->id);
21 free(worktree->head_ref);
22 free(worktree->lock_reason);
23 free(worktree->prune_reason);
24 free(worktree);
27 void free_worktrees(struct worktree **worktrees)
29 int i = 0;
30 for (i = 0; worktrees[i]; i++)
31 free_worktree(worktrees[i]);
32 free (worktrees);
35 /**
36 * Update head_oid, head_ref and is_detached of the given worktree
38 static void add_head_info(struct worktree *wt)
40 int flags;
41 const char *target;
43 target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
44 "HEAD",
46 &wt->head_oid, &flags);
47 if (!target)
48 return;
50 if (flags & REF_ISSYMREF)
51 wt->head_ref = xstrdup(target);
52 else
53 wt->is_detached = 1;
56 /**
57 * get the main worktree
59 static struct worktree *get_main_worktree(int skip_reading_head)
61 struct worktree *worktree = NULL;
62 struct strbuf worktree_path = STRBUF_INIT;
64 strbuf_add_real_path(&worktree_path, get_git_common_dir());
65 strbuf_strip_suffix(&worktree_path, "/.git");
67 CALLOC_ARRAY(worktree, 1);
68 worktree->repo = the_repository;
69 worktree->path = strbuf_detach(&worktree_path, NULL);
71 * NEEDSWORK: If this function is called from a secondary worktree and
72 * config.worktree is present, is_bare_repository_cfg will reflect the
73 * contents of config.worktree, not the contents of the main worktree.
74 * This means that worktree->is_bare may be set to 0 even if the main
75 * worktree is configured to be bare.
77 worktree->is_bare = (is_bare_repository_cfg == 1) ||
78 is_bare_repository();
79 if (!skip_reading_head)
80 add_head_info(worktree);
81 return worktree;
84 struct worktree *get_linked_worktree(const char *id,
85 int skip_reading_head)
87 struct worktree *worktree = NULL;
88 struct strbuf path = STRBUF_INIT;
89 struct strbuf worktree_path = STRBUF_INIT;
91 if (!id)
92 die("Missing linked worktree name");
94 strbuf_git_common_path(&path, the_repository, "worktrees/%s/gitdir", id);
95 if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
96 /* invalid gitdir file */
97 goto done;
98 strbuf_rtrim(&worktree_path);
99 strbuf_strip_suffix(&worktree_path, "/.git");
101 CALLOC_ARRAY(worktree, 1);
102 worktree->repo = the_repository;
103 worktree->path = strbuf_detach(&worktree_path, NULL);
104 worktree->id = xstrdup(id);
105 if (!skip_reading_head)
106 add_head_info(worktree);
108 done:
109 strbuf_release(&path);
110 strbuf_release(&worktree_path);
111 return worktree;
114 static void mark_current_worktree(struct worktree **worktrees)
116 char *git_dir = absolute_pathdup(get_git_dir());
117 int i;
119 for (i = 0; worktrees[i]; i++) {
120 struct worktree *wt = worktrees[i];
121 const char *wt_git_dir = get_worktree_git_dir(wt);
123 if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
124 wt->is_current = 1;
125 break;
128 free(git_dir);
132 * NEEDSWORK: This function exists so that we can look up metadata of a
133 * worktree without trying to access any of its internals like the refdb. It
134 * would be preferable to instead have a corruption-tolerant function for
135 * retrieving worktree metadata that could be used when the worktree is known
136 * to not be in a healthy state, e.g. when creating or repairing it.
138 static struct worktree **get_worktrees_internal(int skip_reading_head)
140 struct worktree **list = NULL;
141 struct strbuf path = STRBUF_INIT;
142 DIR *dir;
143 struct dirent *d;
144 int counter = 0, alloc = 2;
146 ALLOC_ARRAY(list, alloc);
148 list[counter++] = get_main_worktree(skip_reading_head);
150 strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
151 dir = opendir(path.buf);
152 strbuf_release(&path);
153 if (dir) {
154 while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {
155 struct worktree *linked = NULL;
157 if ((linked = get_linked_worktree(d->d_name, skip_reading_head))) {
158 ALLOC_GROW(list, counter + 1, alloc);
159 list[counter++] = linked;
162 closedir(dir);
164 ALLOC_GROW(list, counter + 1, alloc);
165 list[counter] = NULL;
167 mark_current_worktree(list);
168 return list;
171 struct worktree **get_worktrees(void)
173 return get_worktrees_internal(0);
176 const char *get_worktree_git_dir(const struct worktree *wt)
178 if (!wt)
179 return get_git_dir();
180 else if (!wt->id)
181 return get_git_common_dir();
182 else
183 return git_common_path("worktrees/%s", wt->id);
186 static struct worktree *find_worktree_by_suffix(struct worktree **list,
187 const char *suffix)
189 struct worktree *found = NULL;
190 int nr_found = 0, suffixlen;
192 suffixlen = strlen(suffix);
193 if (!suffixlen)
194 return NULL;
196 for (; *list && nr_found < 2; list++) {
197 const char *path = (*list)->path;
198 int pathlen = strlen(path);
199 int start = pathlen - suffixlen;
201 /* suffix must start at directory boundary */
202 if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) &&
203 !fspathcmp(suffix, path + start)) {
204 found = *list;
205 nr_found++;
208 return nr_found == 1 ? found : NULL;
211 struct worktree *find_worktree(struct worktree **list,
212 const char *prefix,
213 const char *arg)
215 struct worktree *wt;
216 char *to_free = NULL;
218 if ((wt = find_worktree_by_suffix(list, arg)))
219 return wt;
221 if (prefix)
222 arg = to_free = prefix_filename(prefix, arg);
223 wt = find_worktree_by_path(list, arg);
224 free(to_free);
225 return wt;
228 struct worktree *find_worktree_by_path(struct worktree **list, const char *p)
230 struct strbuf wt_path = STRBUF_INIT;
231 char *path = real_pathdup(p, 0);
233 if (!path)
234 return NULL;
235 for (; *list; list++) {
236 if (!strbuf_realpath(&wt_path, (*list)->path, 0))
237 continue;
239 if (!fspathcmp(path, wt_path.buf))
240 break;
242 free(path);
243 strbuf_release(&wt_path);
244 return *list;
247 int is_main_worktree(const struct worktree *wt)
249 return !wt->id;
252 const char *worktree_lock_reason(struct worktree *wt)
254 if (is_main_worktree(wt))
255 return NULL;
257 if (!wt->lock_reason_valid) {
258 struct strbuf path = STRBUF_INIT;
260 strbuf_addstr(&path, worktree_git_path(wt, "locked"));
261 if (file_exists(path.buf)) {
262 struct strbuf lock_reason = STRBUF_INIT;
263 if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
264 die_errno(_("failed to read '%s'"), path.buf);
265 strbuf_trim(&lock_reason);
266 wt->lock_reason = strbuf_detach(&lock_reason, NULL);
267 } else
268 wt->lock_reason = NULL;
269 wt->lock_reason_valid = 1;
270 strbuf_release(&path);
273 return wt->lock_reason;
276 const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire)
278 struct strbuf reason = STRBUF_INIT;
279 char *path = NULL;
281 if (is_main_worktree(wt))
282 return NULL;
283 if (wt->prune_reason_valid)
284 return wt->prune_reason;
286 if (should_prune_worktree(wt->id, &reason, &path, expire))
287 wt->prune_reason = strbuf_detach(&reason, NULL);
288 wt->prune_reason_valid = 1;
290 strbuf_release(&reason);
291 free(path);
292 return wt->prune_reason;
295 /* convenient wrapper to deal with NULL strbuf */
296 __attribute__((format (printf, 2, 3)))
297 static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...)
299 va_list params;
301 if (!buf)
302 return;
304 va_start(params, fmt);
305 strbuf_vaddf(buf, fmt, params);
306 va_end(params);
309 int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
310 unsigned flags)
312 struct strbuf wt_path = STRBUF_INIT;
313 struct strbuf realpath = STRBUF_INIT;
314 char *path = NULL;
315 int err, ret = -1;
317 strbuf_addf(&wt_path, "%s/.git", wt->path);
319 if (is_main_worktree(wt)) {
320 if (is_directory(wt_path.buf)) {
321 ret = 0;
322 goto done;
325 * Main worktree using .git file to point to the
326 * repository would make it impossible to know where
327 * the actual worktree is if this function is executed
328 * from another worktree. No .git file support for now.
330 strbuf_addf_gently(errmsg,
331 _("'%s' at main working tree is not the repository directory"),
332 wt_path.buf);
333 goto done;
337 * Make sure "gitdir" file points to a real .git file and that
338 * file points back here.
340 if (!is_absolute_path(wt->path)) {
341 strbuf_addf_gently(errmsg,
342 _("'%s' file does not contain absolute path to the working tree location"),
343 git_common_path("worktrees/%s/gitdir", wt->id));
344 goto done;
347 if (flags & WT_VALIDATE_WORKTREE_MISSING_OK &&
348 !file_exists(wt->path)) {
349 ret = 0;
350 goto done;
353 if (!file_exists(wt_path.buf)) {
354 strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf);
355 goto done;
358 path = xstrdup_or_null(read_gitfile_gently(wt_path.buf, &err));
359 if (!path) {
360 strbuf_addf_gently(errmsg, _("'%s' is not a .git file, error code %d"),
361 wt_path.buf, err);
362 goto done;
365 strbuf_realpath(&realpath, git_common_path("worktrees/%s", wt->id), 1);
366 ret = fspathcmp(path, realpath.buf);
368 if (ret)
369 strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"),
370 wt->path, git_common_path("worktrees/%s", wt->id));
371 done:
372 free(path);
373 strbuf_release(&wt_path);
374 strbuf_release(&realpath);
375 return ret;
378 void update_worktree_location(struct worktree *wt, const char *path_)
380 struct strbuf path = STRBUF_INIT;
382 if (is_main_worktree(wt))
383 BUG("can't relocate main worktree");
385 strbuf_realpath(&path, path_, 1);
386 if (fspathcmp(wt->path, path.buf)) {
387 write_file(git_common_path("worktrees/%s/gitdir", wt->id),
388 "%s/.git", path.buf);
389 free(wt->path);
390 wt->path = strbuf_detach(&path, NULL);
392 strbuf_release(&path);
395 int is_worktree_being_rebased(const struct worktree *wt,
396 const char *target)
398 struct wt_status_state state;
399 int found_rebase;
401 memset(&state, 0, sizeof(state));
402 found_rebase = wt_status_check_rebase(wt, &state) &&
403 (state.rebase_in_progress ||
404 state.rebase_interactive_in_progress) &&
405 state.branch &&
406 skip_prefix(target, "refs/heads/", &target) &&
407 !strcmp(state.branch, target);
408 wt_status_state_free_buffers(&state);
409 return found_rebase;
412 int is_worktree_being_bisected(const struct worktree *wt,
413 const char *target)
415 struct wt_status_state state;
416 int found_bisect;
418 memset(&state, 0, sizeof(state));
419 found_bisect = wt_status_check_bisect(wt, &state) &&
420 state.bisecting_from &&
421 skip_prefix(target, "refs/heads/", &target) &&
422 !strcmp(state.bisecting_from, target);
423 wt_status_state_free_buffers(&state);
424 return found_bisect;
428 * note: this function should be able to detect shared symref even if
429 * HEAD is temporarily detached (e.g. in the middle of rebase or
430 * bisect). New commands that do similar things should update this
431 * function as well.
433 int is_shared_symref(const struct worktree *wt, const char *symref,
434 const char *target)
436 const char *symref_target;
437 struct ref_store *refs;
438 int flags;
440 if (wt->is_bare)
441 return 0;
443 if (wt->is_detached && !strcmp(symref, "HEAD")) {
444 if (is_worktree_being_rebased(wt, target))
445 return 1;
446 if (is_worktree_being_bisected(wt, target))
447 return 1;
450 refs = get_worktree_ref_store(wt);
451 symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
452 NULL, &flags);
453 if ((flags & REF_ISSYMREF) &&
454 symref_target && !strcmp(symref_target, target))
455 return 1;
457 return 0;
460 const struct worktree *find_shared_symref(struct worktree **worktrees,
461 const char *symref,
462 const char *target)
465 for (int i = 0; worktrees[i]; i++)
466 if (is_shared_symref(worktrees[i], symref, target))
467 return worktrees[i];
469 return NULL;
472 int submodule_uses_worktrees(const char *path)
474 char *submodule_gitdir;
475 struct strbuf sb = STRBUF_INIT, err = STRBUF_INIT;
476 DIR *dir;
477 struct dirent *d;
478 int ret = 0;
479 struct repository_format format = REPOSITORY_FORMAT_INIT;
481 submodule_gitdir = git_pathdup_submodule(path, "%s", "");
482 if (!submodule_gitdir)
483 return 0;
485 /* The env would be set for the superproject. */
486 get_common_dir_noenv(&sb, submodule_gitdir);
487 free(submodule_gitdir);
489 strbuf_addstr(&sb, "/config");
490 read_repository_format(&format, sb.buf);
491 if (verify_repository_format(&format, &err)) {
492 strbuf_release(&err);
493 strbuf_release(&sb);
494 clear_repository_format(&format);
495 return 1;
497 clear_repository_format(&format);
498 strbuf_release(&err);
500 /* Replace config by worktrees. */
501 strbuf_setlen(&sb, sb.len - strlen("config"));
502 strbuf_addstr(&sb, "worktrees");
504 /* See if there is any file inside the worktrees directory. */
505 dir = opendir(sb.buf);
506 strbuf_release(&sb);
508 if (!dir)
509 return 0;
511 d = readdir_skip_dot_and_dotdot(dir);
512 if (d)
513 ret = 1;
514 closedir(dir);
515 return ret;
518 void strbuf_worktree_ref(const struct worktree *wt,
519 struct strbuf *sb,
520 const char *refname)
522 if (parse_worktree_ref(refname, NULL, NULL, NULL) ==
523 REF_WORKTREE_CURRENT &&
524 wt && !wt->is_current) {
525 if (is_main_worktree(wt))
526 strbuf_addstr(sb, "main-worktree/");
527 else
528 strbuf_addf(sb, "worktrees/%s/", wt->id);
530 strbuf_addstr(sb, refname);
533 int other_head_refs(each_ref_fn fn, void *cb_data)
535 struct worktree **worktrees, **p;
536 struct strbuf refname = STRBUF_INIT;
537 int ret = 0;
539 worktrees = get_worktrees();
540 for (p = worktrees; *p; p++) {
541 struct worktree *wt = *p;
542 struct object_id oid;
543 int flag;
545 if (wt->is_current)
546 continue;
548 strbuf_reset(&refname);
549 strbuf_worktree_ref(wt, &refname, "HEAD");
550 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
551 refname.buf,
552 RESOLVE_REF_READING,
553 &oid, &flag))
554 ret = fn(refname.buf, &oid, flag, cb_data);
555 if (ret)
556 break;
558 free_worktrees(worktrees);
559 strbuf_release(&refname);
560 return ret;
564 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
565 * pointing at <repo>/worktrees/<id>.
567 static void repair_gitfile(struct worktree *wt,
568 worktree_repair_fn fn, void *cb_data)
570 struct strbuf dotgit = STRBUF_INIT;
571 struct strbuf repo = STRBUF_INIT;
572 char *backlink;
573 const char *repair = NULL;
574 int err;
576 /* missing worktree can't be repaired */
577 if (!file_exists(wt->path))
578 return;
580 if (!is_directory(wt->path)) {
581 fn(1, wt->path, _("not a directory"), cb_data);
582 return;
585 strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
586 strbuf_addf(&dotgit, "%s/.git", wt->path);
587 backlink = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
589 if (err == READ_GITFILE_ERR_NOT_A_FILE)
590 fn(1, wt->path, _(".git is not a file"), cb_data);
591 else if (err)
592 repair = _(".git file broken");
593 else if (fspathcmp(backlink, repo.buf))
594 repair = _(".git file incorrect");
596 if (repair) {
597 fn(0, wt->path, repair, cb_data);
598 write_file(dotgit.buf, "gitdir: %s", repo.buf);
601 free(backlink);
602 strbuf_release(&repo);
603 strbuf_release(&dotgit);
606 static void repair_noop(int iserr UNUSED,
607 const char *path UNUSED,
608 const char *msg UNUSED,
609 void *cb_data UNUSED)
611 /* nothing */
614 void repair_worktrees(worktree_repair_fn fn, void *cb_data)
616 struct worktree **worktrees = get_worktrees_internal(1);
617 struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
619 if (!fn)
620 fn = repair_noop;
621 for (; *wt; wt++)
622 repair_gitfile(*wt, fn, cb_data);
623 free_worktrees(worktrees);
626 static int is_main_worktree_path(const char *path)
628 struct strbuf target = STRBUF_INIT;
629 struct strbuf maindir = STRBUF_INIT;
630 int cmp;
632 strbuf_add_real_path(&target, path);
633 strbuf_strip_suffix(&target, "/.git");
634 strbuf_add_real_path(&maindir, get_git_common_dir());
635 strbuf_strip_suffix(&maindir, "/.git");
636 cmp = fspathcmp(maindir.buf, target.buf);
638 strbuf_release(&maindir);
639 strbuf_release(&target);
640 return !cmp;
644 * If both the main worktree and linked worktree have been moved, then the
645 * gitfile /path/to/worktree/.git won't point into the repository, thus we
646 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
647 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
648 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
650 static char *infer_backlink(const char *gitfile)
652 struct strbuf actual = STRBUF_INIT;
653 struct strbuf inferred = STRBUF_INIT;
654 const char *id;
656 if (strbuf_read_file(&actual, gitfile, 0) < 0)
657 goto error;
658 if (!starts_with(actual.buf, "gitdir:"))
659 goto error;
660 if (!(id = find_last_dir_sep(actual.buf)))
661 goto error;
662 strbuf_trim(&actual);
663 id++; /* advance past '/' to point at <id> */
664 if (!*id)
665 goto error;
666 strbuf_git_common_path(&inferred, the_repository, "worktrees/%s", id);
667 if (!is_directory(inferred.buf))
668 goto error;
670 strbuf_release(&actual);
671 return strbuf_detach(&inferred, NULL);
673 error:
674 strbuf_release(&actual);
675 strbuf_release(&inferred);
676 return NULL;
680 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
681 * the worktree's path.
683 void repair_worktree_at_path(const char *path,
684 worktree_repair_fn fn, void *cb_data)
686 struct strbuf dotgit = STRBUF_INIT;
687 struct strbuf realdotgit = STRBUF_INIT;
688 struct strbuf gitdir = STRBUF_INIT;
689 struct strbuf olddotgit = STRBUF_INIT;
690 char *backlink = NULL;
691 const char *repair = NULL;
692 int err;
694 if (!fn)
695 fn = repair_noop;
697 if (is_main_worktree_path(path))
698 goto done;
700 strbuf_addf(&dotgit, "%s/.git", path);
701 if (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {
702 fn(1, path, _("not a valid path"), cb_data);
703 goto done;
706 backlink = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));
707 if (err == READ_GITFILE_ERR_NOT_A_FILE) {
708 fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
709 goto done;
710 } else if (err == READ_GITFILE_ERR_NOT_A_REPO) {
711 if (!(backlink = infer_backlink(realdotgit.buf))) {
712 fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
713 goto done;
715 } else if (err) {
716 fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
717 goto done;
720 strbuf_addf(&gitdir, "%s/gitdir", backlink);
721 if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
722 repair = _("gitdir unreadable");
723 else {
724 strbuf_rtrim(&olddotgit);
725 if (fspathcmp(olddotgit.buf, realdotgit.buf))
726 repair = _("gitdir incorrect");
729 if (repair) {
730 fn(0, gitdir.buf, repair, cb_data);
731 write_file(gitdir.buf, "%s", realdotgit.buf);
733 done:
734 free(backlink);
735 strbuf_release(&olddotgit);
736 strbuf_release(&gitdir);
737 strbuf_release(&realdotgit);
738 strbuf_release(&dotgit);
741 int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire)
743 struct stat st;
744 char *path;
745 int fd;
746 size_t len;
747 ssize_t read_result;
749 *wtpath = NULL;
750 if (!is_directory(git_path("worktrees/%s", id))) {
751 strbuf_addstr(reason, _("not a valid directory"));
752 return 1;
754 if (file_exists(git_path("worktrees/%s/locked", id)))
755 return 0;
756 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
757 strbuf_addstr(reason, _("gitdir file does not exist"));
758 return 1;
760 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
761 if (fd < 0) {
762 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
763 strerror(errno));
764 return 1;
766 len = xsize_t(st.st_size);
767 path = xmallocz(len);
769 read_result = read_in_full(fd, path, len);
770 if (read_result < 0) {
771 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
772 strerror(errno));
773 close(fd);
774 free(path);
775 return 1;
777 close(fd);
779 if (read_result != len) {
780 strbuf_addf(reason,
781 _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
782 (uintmax_t)len, (uintmax_t)read_result);
783 free(path);
784 return 1;
786 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
787 len--;
788 if (!len) {
789 strbuf_addstr(reason, _("invalid gitdir file"));
790 free(path);
791 return 1;
793 path[len] = '\0';
794 if (!file_exists(path)) {
795 if (stat(git_path("worktrees/%s/index", id), &st) ||
796 st.st_mtime <= expire) {
797 strbuf_addstr(reason, _("gitdir file points to non-existent location"));
798 free(path);
799 return 1;
800 } else {
801 *wtpath = path;
802 return 0;
805 *wtpath = path;
806 return 0;
809 static int move_config_setting(const char *key, const char *value,
810 const char *from_file, const char *to_file)
812 if (git_config_set_in_file_gently(to_file, key, NULL, value))
813 return error(_("unable to set %s in '%s'"), key, to_file);
814 if (git_config_set_in_file_gently(from_file, key, NULL, NULL))
815 return error(_("unable to unset %s in '%s'"), key, from_file);
816 return 0;
819 int init_worktree_config(struct repository *r)
821 int res = 0;
822 int bare = 0;
823 struct config_set cs = { { 0 } };
824 const char *core_worktree;
825 char *common_config_file;
826 char *main_worktree_file;
829 * If the extension is already enabled, then we can skip the
830 * upgrade process.
832 if (r->repository_format_worktree_config)
833 return 0;
834 if ((res = git_config_set_gently("extensions.worktreeConfig", "true")))
835 return error(_("failed to set extensions.worktreeConfig setting"));
837 common_config_file = xstrfmt("%s/config", r->commondir);
838 main_worktree_file = xstrfmt("%s/config.worktree", r->commondir);
840 git_configset_init(&cs);
841 git_configset_add_file(&cs, common_config_file);
844 * If core.bare is true in the common config file, then we need to
845 * move it to the main worktree's config file or it will break all
846 * worktrees. If it is false, then leave it in place because it
847 * _could_ be negating a global core.bare=true.
849 if (!git_configset_get_bool(&cs, "core.bare", &bare) && bare) {
850 if ((res = move_config_setting("core.bare", "true",
851 common_config_file,
852 main_worktree_file)))
853 goto cleanup;
856 * If core.worktree is set, then the main worktree is located
857 * somewhere different than the parent of the common Git dir.
858 * Relocate that value to avoid breaking all worktrees with this
859 * upgrade to worktree config.
861 if (!git_configset_get_value(&cs, "core.worktree", &core_worktree, NULL)) {
862 if ((res = move_config_setting("core.worktree", core_worktree,
863 common_config_file,
864 main_worktree_file)))
865 goto cleanup;
869 * Ensure that we use worktree config for the remaining lifetime
870 * of the current process.
872 r->repository_format_worktree_config = 1;
874 cleanup:
875 git_configset_clear(&cs);
876 free(common_config_file);
877 free(main_worktree_file);
878 return res;