Merge branch 'jc/t5512-sigpipe-fix'
[git/gitster.git] / worktree.c
blob0f032ccedffe636e1db2717e713663de4976963e
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "abspath.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "path.h"
8 #include "repository.h"
9 #include "refs.h"
10 #include "setup.h"
11 #include "strbuf.h"
12 #include "worktree.h"
13 #include "dir.h"
14 #include "wt-status.h"
15 #include "config.h"
17 void free_worktree(struct worktree *worktree)
19 if (!worktree)
20 return;
21 free(worktree->path);
22 free(worktree->id);
23 free(worktree->head_ref);
24 free(worktree->lock_reason);
25 free(worktree->prune_reason);
26 free(worktree);
29 void free_worktrees(struct worktree **worktrees)
31 int i = 0;
32 for (i = 0; worktrees[i]; i++)
33 free_worktree(worktrees[i]);
34 free (worktrees);
37 /**
38 * Update head_oid, head_ref and is_detached of the given worktree
40 static void add_head_info(struct worktree *wt)
42 int flags;
43 const char *target;
45 target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
46 "HEAD",
48 &wt->head_oid, &flags);
49 if (!target)
50 return;
52 if (flags & REF_ISSYMREF)
53 wt->head_ref = xstrdup(target);
54 else
55 wt->is_detached = 1;
58 static int is_current_worktree(struct worktree *wt)
60 char *git_dir = absolute_pathdup(repo_get_git_dir(the_repository));
61 const char *wt_git_dir = get_worktree_git_dir(wt);
62 int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir));
63 free(git_dir);
64 return is_current;
67 /**
68 * get the main worktree
70 static struct worktree *get_main_worktree(int skip_reading_head)
72 struct worktree *worktree = NULL;
73 struct strbuf worktree_path = STRBUF_INIT;
75 strbuf_add_real_path(&worktree_path, repo_get_common_dir(the_repository));
76 strbuf_strip_suffix(&worktree_path, "/.git");
78 CALLOC_ARRAY(worktree, 1);
79 worktree->repo = the_repository;
80 worktree->path = strbuf_detach(&worktree_path, NULL);
82 * NEEDSWORK: If this function is called from a secondary worktree and
83 * config.worktree is present, is_bare_repository_cfg will reflect the
84 * contents of config.worktree, not the contents of the main worktree.
85 * This means that worktree->is_bare may be set to 0 even if the main
86 * worktree is configured to be bare.
88 worktree->is_bare = (is_bare_repository_cfg == 1) ||
89 is_bare_repository();
90 worktree->is_current = is_current_worktree(worktree);
91 if (!skip_reading_head)
92 add_head_info(worktree);
93 return worktree;
96 struct worktree *get_linked_worktree(const char *id,
97 int skip_reading_head)
99 struct worktree *worktree = NULL;
100 struct strbuf path = STRBUF_INIT;
101 struct strbuf worktree_path = STRBUF_INIT;
103 if (!id)
104 die("Missing linked worktree name");
106 strbuf_git_common_path(&path, the_repository, "worktrees/%s/gitdir", id);
107 if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
108 /* invalid gitdir file */
109 goto done;
110 strbuf_rtrim(&worktree_path);
111 strbuf_strip_suffix(&worktree_path, "/.git");
113 CALLOC_ARRAY(worktree, 1);
114 worktree->repo = the_repository;
115 worktree->path = strbuf_detach(&worktree_path, NULL);
116 worktree->id = xstrdup(id);
117 worktree->is_current = is_current_worktree(worktree);
118 if (!skip_reading_head)
119 add_head_info(worktree);
121 done:
122 strbuf_release(&path);
123 strbuf_release(&worktree_path);
124 return worktree;
128 * NEEDSWORK: This function exists so that we can look up metadata of a
129 * worktree without trying to access any of its internals like the refdb. It
130 * would be preferable to instead have a corruption-tolerant function for
131 * retrieving worktree metadata that could be used when the worktree is known
132 * to not be in a healthy state, e.g. when creating or repairing it.
134 static struct worktree **get_worktrees_internal(int skip_reading_head)
136 struct worktree **list = NULL;
137 struct strbuf path = STRBUF_INIT;
138 DIR *dir;
139 struct dirent *d;
140 int counter = 0, alloc = 2;
142 ALLOC_ARRAY(list, alloc);
144 list[counter++] = get_main_worktree(skip_reading_head);
146 strbuf_addf(&path, "%s/worktrees", repo_get_common_dir(the_repository));
147 dir = opendir(path.buf);
148 strbuf_release(&path);
149 if (dir) {
150 while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {
151 struct worktree *linked = NULL;
153 if ((linked = get_linked_worktree(d->d_name, skip_reading_head))) {
154 ALLOC_GROW(list, counter + 1, alloc);
155 list[counter++] = linked;
158 closedir(dir);
160 ALLOC_GROW(list, counter + 1, alloc);
161 list[counter] = NULL;
163 return list;
166 struct worktree **get_worktrees(void)
168 return get_worktrees_internal(0);
171 const char *get_worktree_git_dir(const struct worktree *wt)
173 if (!wt)
174 return repo_get_git_dir(the_repository);
175 else if (!wt->id)
176 return repo_get_common_dir(the_repository);
177 else
178 return git_common_path("worktrees/%s", wt->id);
181 static struct worktree *find_worktree_by_suffix(struct worktree **list,
182 const char *suffix)
184 struct worktree *found = NULL;
185 int nr_found = 0, suffixlen;
187 suffixlen = strlen(suffix);
188 if (!suffixlen)
189 return NULL;
191 for (; *list && nr_found < 2; list++) {
192 const char *path = (*list)->path;
193 int pathlen = strlen(path);
194 int start = pathlen - suffixlen;
196 /* suffix must start at directory boundary */
197 if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) &&
198 !fspathcmp(suffix, path + start)) {
199 found = *list;
200 nr_found++;
203 return nr_found == 1 ? found : NULL;
206 struct worktree *find_worktree(struct worktree **list,
207 const char *prefix,
208 const char *arg)
210 struct worktree *wt;
211 char *to_free = NULL;
213 if ((wt = find_worktree_by_suffix(list, arg)))
214 return wt;
216 if (prefix)
217 arg = to_free = prefix_filename(prefix, arg);
218 wt = find_worktree_by_path(list, arg);
219 free(to_free);
220 return wt;
223 struct worktree *find_worktree_by_path(struct worktree **list, const char *p)
225 struct strbuf wt_path = STRBUF_INIT;
226 char *path = real_pathdup(p, 0);
228 if (!path)
229 return NULL;
230 for (; *list; list++) {
231 if (!strbuf_realpath(&wt_path, (*list)->path, 0))
232 continue;
234 if (!fspathcmp(path, wt_path.buf))
235 break;
237 free(path);
238 strbuf_release(&wt_path);
239 return *list;
242 int is_main_worktree(const struct worktree *wt)
244 return !wt->id;
247 const char *worktree_lock_reason(struct worktree *wt)
249 if (is_main_worktree(wt))
250 return NULL;
252 if (!wt->lock_reason_valid) {
253 struct strbuf path = STRBUF_INIT;
255 strbuf_addstr(&path, worktree_git_path(the_repository, wt, "locked"));
256 if (file_exists(path.buf)) {
257 struct strbuf lock_reason = STRBUF_INIT;
258 if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
259 die_errno(_("failed to read '%s'"), path.buf);
260 strbuf_trim(&lock_reason);
261 wt->lock_reason = strbuf_detach(&lock_reason, NULL);
262 } else
263 wt->lock_reason = NULL;
264 wt->lock_reason_valid = 1;
265 strbuf_release(&path);
268 return wt->lock_reason;
271 const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire)
273 struct strbuf reason = STRBUF_INIT;
274 char *path = NULL;
276 if (is_main_worktree(wt))
277 return NULL;
278 if (wt->prune_reason_valid)
279 return wt->prune_reason;
281 if (should_prune_worktree(wt->id, &reason, &path, expire))
282 wt->prune_reason = strbuf_detach(&reason, NULL);
283 wt->prune_reason_valid = 1;
285 strbuf_release(&reason);
286 free(path);
287 return wt->prune_reason;
290 /* convenient wrapper to deal with NULL strbuf */
291 __attribute__((format (printf, 2, 3)))
292 static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...)
294 va_list params;
296 if (!buf)
297 return;
299 va_start(params, fmt);
300 strbuf_vaddf(buf, fmt, params);
301 va_end(params);
304 int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
305 unsigned flags)
307 struct strbuf wt_path = STRBUF_INIT;
308 struct strbuf realpath = STRBUF_INIT;
309 char *path = NULL;
310 int err, ret = -1;
312 strbuf_addf(&wt_path, "%s/.git", wt->path);
314 if (is_main_worktree(wt)) {
315 if (is_directory(wt_path.buf)) {
316 ret = 0;
317 goto done;
320 * Main worktree using .git file to point to the
321 * repository would make it impossible to know where
322 * the actual worktree is if this function is executed
323 * from another worktree. No .git file support for now.
325 strbuf_addf_gently(errmsg,
326 _("'%s' at main working tree is not the repository directory"),
327 wt_path.buf);
328 goto done;
332 * Make sure "gitdir" file points to a real .git file and that
333 * file points back here.
335 if (!is_absolute_path(wt->path)) {
336 strbuf_addf_gently(errmsg,
337 _("'%s' file does not contain absolute path to the working tree location"),
338 git_common_path("worktrees/%s/gitdir", wt->id));
339 goto done;
342 if (flags & WT_VALIDATE_WORKTREE_MISSING_OK &&
343 !file_exists(wt->path)) {
344 ret = 0;
345 goto done;
348 if (!file_exists(wt_path.buf)) {
349 strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf);
350 goto done;
353 path = xstrdup_or_null(read_gitfile_gently(wt_path.buf, &err));
354 if (!path) {
355 strbuf_addf_gently(errmsg, _("'%s' is not a .git file, error code %d"),
356 wt_path.buf, err);
357 goto done;
360 strbuf_realpath(&realpath, git_common_path("worktrees/%s", wt->id), 1);
361 ret = fspathcmp(path, realpath.buf);
363 if (ret)
364 strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"),
365 wt->path, git_common_path("worktrees/%s", wt->id));
366 done:
367 free(path);
368 strbuf_release(&wt_path);
369 strbuf_release(&realpath);
370 return ret;
373 void update_worktree_location(struct worktree *wt, const char *path_)
375 struct strbuf path = STRBUF_INIT;
377 if (is_main_worktree(wt))
378 BUG("can't relocate main worktree");
380 strbuf_realpath(&path, path_, 1);
381 if (fspathcmp(wt->path, path.buf)) {
382 write_file(git_common_path("worktrees/%s/gitdir", wt->id),
383 "%s/.git", path.buf);
384 free(wt->path);
385 wt->path = strbuf_detach(&path, NULL);
387 strbuf_release(&path);
390 int is_worktree_being_rebased(const struct worktree *wt,
391 const char *target)
393 struct wt_status_state state;
394 int found_rebase;
396 memset(&state, 0, sizeof(state));
397 found_rebase = wt_status_check_rebase(wt, &state) &&
398 (state.rebase_in_progress ||
399 state.rebase_interactive_in_progress) &&
400 state.branch &&
401 skip_prefix(target, "refs/heads/", &target) &&
402 !strcmp(state.branch, target);
403 wt_status_state_free_buffers(&state);
404 return found_rebase;
407 int is_worktree_being_bisected(const struct worktree *wt,
408 const char *target)
410 struct wt_status_state state;
411 int found_bisect;
413 memset(&state, 0, sizeof(state));
414 found_bisect = wt_status_check_bisect(wt, &state) &&
415 state.bisecting_from &&
416 skip_prefix(target, "refs/heads/", &target) &&
417 !strcmp(state.bisecting_from, target);
418 wt_status_state_free_buffers(&state);
419 return found_bisect;
423 * note: this function should be able to detect shared symref even if
424 * HEAD is temporarily detached (e.g. in the middle of rebase or
425 * bisect). New commands that do similar things should update this
426 * function as well.
428 int is_shared_symref(const struct worktree *wt, const char *symref,
429 const char *target)
431 const char *symref_target;
432 struct ref_store *refs;
433 int flags;
435 if (wt->is_bare)
436 return 0;
438 if (wt->is_detached && !strcmp(symref, "HEAD")) {
439 if (is_worktree_being_rebased(wt, target))
440 return 1;
441 if (is_worktree_being_bisected(wt, target))
442 return 1;
445 refs = get_worktree_ref_store(wt);
446 symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
447 NULL, &flags);
448 if ((flags & REF_ISSYMREF) &&
449 symref_target && !strcmp(symref_target, target))
450 return 1;
452 return 0;
455 const struct worktree *find_shared_symref(struct worktree **worktrees,
456 const char *symref,
457 const char *target)
460 for (int i = 0; worktrees[i]; i++)
461 if (is_shared_symref(worktrees[i], symref, target))
462 return worktrees[i];
464 return NULL;
467 int submodule_uses_worktrees(const char *path)
469 char *submodule_gitdir;
470 struct strbuf sb = STRBUF_INIT, err = STRBUF_INIT;
471 DIR *dir;
472 struct dirent *d;
473 int ret = 0;
474 struct repository_format format = REPOSITORY_FORMAT_INIT;
476 submodule_gitdir = git_pathdup_submodule(path, "%s", "");
477 if (!submodule_gitdir)
478 return 0;
480 /* The env would be set for the superproject. */
481 get_common_dir_noenv(&sb, submodule_gitdir);
482 free(submodule_gitdir);
484 strbuf_addstr(&sb, "/config");
485 read_repository_format(&format, sb.buf);
486 if (verify_repository_format(&format, &err)) {
487 strbuf_release(&err);
488 strbuf_release(&sb);
489 clear_repository_format(&format);
490 return 1;
492 clear_repository_format(&format);
493 strbuf_release(&err);
495 /* Replace config by worktrees. */
496 strbuf_setlen(&sb, sb.len - strlen("config"));
497 strbuf_addstr(&sb, "worktrees");
499 /* See if there is any file inside the worktrees directory. */
500 dir = opendir(sb.buf);
501 strbuf_release(&sb);
503 if (!dir)
504 return 0;
506 d = readdir_skip_dot_and_dotdot(dir);
507 if (d)
508 ret = 1;
509 closedir(dir);
510 return ret;
513 void strbuf_worktree_ref(const struct worktree *wt,
514 struct strbuf *sb,
515 const char *refname)
517 if (parse_worktree_ref(refname, NULL, NULL, NULL) ==
518 REF_WORKTREE_CURRENT &&
519 wt && !wt->is_current) {
520 if (is_main_worktree(wt))
521 strbuf_addstr(sb, "main-worktree/");
522 else
523 strbuf_addf(sb, "worktrees/%s/", wt->id);
525 strbuf_addstr(sb, refname);
528 int other_head_refs(each_ref_fn fn, void *cb_data)
530 struct worktree **worktrees, **p;
531 struct strbuf refname = STRBUF_INIT;
532 int ret = 0;
534 worktrees = get_worktrees();
535 for (p = worktrees; *p; p++) {
536 struct worktree *wt = *p;
537 struct object_id oid;
538 int flag;
540 if (wt->is_current)
541 continue;
543 strbuf_reset(&refname);
544 strbuf_worktree_ref(wt, &refname, "HEAD");
545 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
546 refname.buf,
547 RESOLVE_REF_READING,
548 &oid, &flag))
549 ret = fn(refname.buf, NULL, &oid, flag, cb_data);
550 if (ret)
551 break;
553 free_worktrees(worktrees);
554 strbuf_release(&refname);
555 return ret;
559 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
560 * pointing at <repo>/worktrees/<id>.
562 static void repair_gitfile(struct worktree *wt,
563 worktree_repair_fn fn, void *cb_data)
565 struct strbuf dotgit = STRBUF_INIT;
566 struct strbuf repo = STRBUF_INIT;
567 char *backlink;
568 const char *repair = NULL;
569 int err;
571 /* missing worktree can't be repaired */
572 if (!file_exists(wt->path))
573 return;
575 if (!is_directory(wt->path)) {
576 fn(1, wt->path, _("not a directory"), cb_data);
577 return;
580 strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
581 strbuf_addf(&dotgit, "%s/.git", wt->path);
582 backlink = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
584 if (err == READ_GITFILE_ERR_NOT_A_FILE)
585 fn(1, wt->path, _(".git is not a file"), cb_data);
586 else if (err)
587 repair = _(".git file broken");
588 else if (fspathcmp(backlink, repo.buf))
589 repair = _(".git file incorrect");
591 if (repair) {
592 fn(0, wt->path, repair, cb_data);
593 write_file(dotgit.buf, "gitdir: %s", repo.buf);
596 free(backlink);
597 strbuf_release(&repo);
598 strbuf_release(&dotgit);
601 static void repair_noop(int iserr UNUSED,
602 const char *path UNUSED,
603 const char *msg UNUSED,
604 void *cb_data UNUSED)
606 /* nothing */
609 void repair_worktrees(worktree_repair_fn fn, void *cb_data)
611 struct worktree **worktrees = get_worktrees_internal(1);
612 struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
614 if (!fn)
615 fn = repair_noop;
616 for (; *wt; wt++)
617 repair_gitfile(*wt, fn, cb_data);
618 free_worktrees(worktrees);
621 static int is_main_worktree_path(const char *path)
623 struct strbuf target = STRBUF_INIT;
624 struct strbuf maindir = STRBUF_INIT;
625 int cmp;
627 strbuf_add_real_path(&target, path);
628 strbuf_strip_suffix(&target, "/.git");
629 strbuf_add_real_path(&maindir, repo_get_common_dir(the_repository));
630 strbuf_strip_suffix(&maindir, "/.git");
631 cmp = fspathcmp(maindir.buf, target.buf);
633 strbuf_release(&maindir);
634 strbuf_release(&target);
635 return !cmp;
639 * If both the main worktree and linked worktree have been moved, then the
640 * gitfile /path/to/worktree/.git won't point into the repository, thus we
641 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
642 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
643 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
645 static char *infer_backlink(const char *gitfile)
647 struct strbuf actual = STRBUF_INIT;
648 struct strbuf inferred = STRBUF_INIT;
649 const char *id;
651 if (strbuf_read_file(&actual, gitfile, 0) < 0)
652 goto error;
653 if (!starts_with(actual.buf, "gitdir:"))
654 goto error;
655 if (!(id = find_last_dir_sep(actual.buf)))
656 goto error;
657 strbuf_trim(&actual);
658 id++; /* advance past '/' to point at <id> */
659 if (!*id)
660 goto error;
661 strbuf_git_common_path(&inferred, the_repository, "worktrees/%s", id);
662 if (!is_directory(inferred.buf))
663 goto error;
665 strbuf_release(&actual);
666 return strbuf_detach(&inferred, NULL);
668 error:
669 strbuf_release(&actual);
670 strbuf_release(&inferred);
671 return NULL;
675 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
676 * the worktree's path.
678 void repair_worktree_at_path(const char *path,
679 worktree_repair_fn fn, void *cb_data)
681 struct strbuf dotgit = STRBUF_INIT;
682 struct strbuf realdotgit = STRBUF_INIT;
683 struct strbuf gitdir = STRBUF_INIT;
684 struct strbuf olddotgit = STRBUF_INIT;
685 char *backlink = NULL;
686 const char *repair = NULL;
687 int err;
689 if (!fn)
690 fn = repair_noop;
692 if (is_main_worktree_path(path))
693 goto done;
695 strbuf_addf(&dotgit, "%s/.git", path);
696 if (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {
697 fn(1, path, _("not a valid path"), cb_data);
698 goto done;
701 backlink = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));
702 if (err == READ_GITFILE_ERR_NOT_A_FILE) {
703 fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
704 goto done;
705 } else if (err == READ_GITFILE_ERR_NOT_A_REPO) {
706 if (!(backlink = infer_backlink(realdotgit.buf))) {
707 fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
708 goto done;
710 } else if (err) {
711 fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
712 goto done;
715 strbuf_addf(&gitdir, "%s/gitdir", backlink);
716 if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
717 repair = _("gitdir unreadable");
718 else {
719 strbuf_rtrim(&olddotgit);
720 if (fspathcmp(olddotgit.buf, realdotgit.buf))
721 repair = _("gitdir incorrect");
724 if (repair) {
725 fn(0, gitdir.buf, repair, cb_data);
726 write_file(gitdir.buf, "%s", realdotgit.buf);
728 done:
729 free(backlink);
730 strbuf_release(&olddotgit);
731 strbuf_release(&gitdir);
732 strbuf_release(&realdotgit);
733 strbuf_release(&dotgit);
736 int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire)
738 struct stat st;
739 char *path;
740 int fd;
741 size_t len;
742 ssize_t read_result;
744 *wtpath = NULL;
745 if (!is_directory(git_path("worktrees/%s", id))) {
746 strbuf_addstr(reason, _("not a valid directory"));
747 return 1;
749 if (file_exists(git_path("worktrees/%s/locked", id)))
750 return 0;
751 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
752 strbuf_addstr(reason, _("gitdir file does not exist"));
753 return 1;
755 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
756 if (fd < 0) {
757 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
758 strerror(errno));
759 return 1;
761 len = xsize_t(st.st_size);
762 path = xmallocz(len);
764 read_result = read_in_full(fd, path, len);
765 if (read_result < 0) {
766 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
767 strerror(errno));
768 close(fd);
769 free(path);
770 return 1;
772 close(fd);
774 if (read_result != len) {
775 strbuf_addf(reason,
776 _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
777 (uintmax_t)len, (uintmax_t)read_result);
778 free(path);
779 return 1;
781 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
782 len--;
783 if (!len) {
784 strbuf_addstr(reason, _("invalid gitdir file"));
785 free(path);
786 return 1;
788 path[len] = '\0';
789 if (!file_exists(path)) {
790 if (stat(git_path("worktrees/%s/index", id), &st) ||
791 st.st_mtime <= expire) {
792 strbuf_addstr(reason, _("gitdir file points to non-existent location"));
793 free(path);
794 return 1;
795 } else {
796 *wtpath = path;
797 return 0;
800 *wtpath = path;
801 return 0;
804 static int move_config_setting(const char *key, const char *value,
805 const char *from_file, const char *to_file)
807 if (git_config_set_in_file_gently(to_file, key, NULL, value))
808 return error(_("unable to set %s in '%s'"), key, to_file);
809 if (git_config_set_in_file_gently(from_file, key, NULL, NULL))
810 return error(_("unable to unset %s in '%s'"), key, from_file);
811 return 0;
814 int init_worktree_config(struct repository *r)
816 int res = 0;
817 int bare = 0;
818 struct config_set cs = { { 0 } };
819 const char *core_worktree;
820 char *common_config_file;
821 char *main_worktree_file;
824 * If the extension is already enabled, then we can skip the
825 * upgrade process.
827 if (r->repository_format_worktree_config)
828 return 0;
829 if ((res = git_config_set_gently("extensions.worktreeConfig", "true")))
830 return error(_("failed to set extensions.worktreeConfig setting"));
832 common_config_file = xstrfmt("%s/config", r->commondir);
833 main_worktree_file = xstrfmt("%s/config.worktree", r->commondir);
835 git_configset_init(&cs);
836 git_configset_add_file(&cs, common_config_file);
839 * If core.bare is true in the common config file, then we need to
840 * move it to the main worktree's config file or it will break all
841 * worktrees. If it is false, then leave it in place because it
842 * _could_ be negating a global core.bare=true.
844 if (!git_configset_get_bool(&cs, "core.bare", &bare) && bare) {
845 if ((res = move_config_setting("core.bare", "true",
846 common_config_file,
847 main_worktree_file)))
848 goto cleanup;
851 * If core.worktree is set, then the main worktree is located
852 * somewhere different than the parent of the common Git dir.
853 * Relocate that value to avoid breaking all worktrees with this
854 * upgrade to worktree config.
856 if (!git_configset_get_value(&cs, "core.worktree", &core_worktree, NULL)) {
857 if ((res = move_config_setting("core.worktree", core_worktree,
858 common_config_file,
859 main_worktree_file)))
860 goto cleanup;
864 * Ensure that we use worktree config for the remaining lifetime
865 * of the current process.
867 r->repository_format_worktree_config = 1;
869 cleanup:
870 git_configset_clear(&cs);
871 free(common_config_file);
872 free(main_worktree_file);
873 return res;