1 #include "git-compat-util.h"
3 #include "environment.h"
6 #include "repository.h"
12 #include "wt-status.h"
15 void free_worktree(struct worktree
*worktree
)
21 free(worktree
->head_ref
);
22 free(worktree
->lock_reason
);
23 free(worktree
->prune_reason
);
27 void free_worktrees(struct worktree
**worktrees
)
30 for (i
= 0; worktrees
[i
]; i
++)
31 free_worktree(worktrees
[i
]);
36 * Update head_oid, head_ref and is_detached of the given worktree
38 static void add_head_info(struct worktree
*wt
)
43 target
= refs_resolve_ref_unsafe(get_worktree_ref_store(wt
),
46 &wt
->head_oid
, &flags
);
50 if (flags
& REF_ISSYMREF
)
51 wt
->head_ref
= xstrdup(target
);
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
->path
= strbuf_detach(&worktree_path
, NULL
);
70 * NEEDSWORK: If this function is called from a secondary worktree and
71 * config.worktree is present, is_bare_repository_cfg will reflect the
72 * contents of config.worktree, not the contents of the main worktree.
73 * This means that worktree->is_bare may be set to 0 even if the main
74 * worktree is configured to be bare.
76 worktree
->is_bare
= (is_bare_repository_cfg
== 1) ||
78 if (!skip_reading_head
)
79 add_head_info(worktree
);
83 struct worktree
*get_linked_worktree(const char *id
,
84 int skip_reading_head
)
86 struct worktree
*worktree
= NULL
;
87 struct strbuf path
= STRBUF_INIT
;
88 struct strbuf worktree_path
= STRBUF_INIT
;
91 die("Missing linked worktree name");
93 strbuf_git_common_path(&path
, the_repository
, "worktrees/%s/gitdir", id
);
94 if (strbuf_read_file(&worktree_path
, path
.buf
, 0) <= 0)
95 /* invalid gitdir file */
97 strbuf_rtrim(&worktree_path
);
98 strbuf_strip_suffix(&worktree_path
, "/.git");
100 CALLOC_ARRAY(worktree
, 1);
101 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
102 worktree
->id
= xstrdup(id
);
103 if (!skip_reading_head
)
104 add_head_info(worktree
);
107 strbuf_release(&path
);
108 strbuf_release(&worktree_path
);
112 static void mark_current_worktree(struct worktree
**worktrees
)
114 char *git_dir
= absolute_pathdup(get_git_dir());
117 for (i
= 0; worktrees
[i
]; i
++) {
118 struct worktree
*wt
= worktrees
[i
];
119 const char *wt_git_dir
= get_worktree_git_dir(wt
);
121 if (!fspathcmp(git_dir
, absolute_path(wt_git_dir
))) {
130 * NEEDSWORK: This function exists so that we can look up metadata of a
131 * worktree without trying to access any of its internals like the refdb. It
132 * would be preferable to instead have a corruption-tolerant function for
133 * retrieving worktree metadata that could be used when the worktree is known
134 * to not be in a healthy state, e.g. when creating or repairing it.
136 static struct worktree
**get_worktrees_internal(int skip_reading_head
)
138 struct worktree
**list
= NULL
;
139 struct strbuf path
= STRBUF_INIT
;
142 int counter
= 0, alloc
= 2;
144 ALLOC_ARRAY(list
, alloc
);
146 list
[counter
++] = get_main_worktree(skip_reading_head
);
148 strbuf_addf(&path
, "%s/worktrees", get_git_common_dir());
149 dir
= opendir(path
.buf
);
150 strbuf_release(&path
);
152 while ((d
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
153 struct worktree
*linked
= NULL
;
155 if ((linked
= get_linked_worktree(d
->d_name
, skip_reading_head
))) {
156 ALLOC_GROW(list
, counter
+ 1, alloc
);
157 list
[counter
++] = linked
;
162 ALLOC_GROW(list
, counter
+ 1, alloc
);
163 list
[counter
] = NULL
;
165 mark_current_worktree(list
);
169 struct worktree
**get_worktrees(void)
171 return get_worktrees_internal(0);
174 const char *get_worktree_git_dir(const struct worktree
*wt
)
177 return get_git_dir();
179 return get_git_common_dir();
181 return git_common_path("worktrees/%s", wt
->id
);
184 static struct worktree
*find_worktree_by_suffix(struct worktree
**list
,
187 struct worktree
*found
= NULL
;
188 int nr_found
= 0, suffixlen
;
190 suffixlen
= strlen(suffix
);
194 for (; *list
&& nr_found
< 2; list
++) {
195 const char *path
= (*list
)->path
;
196 int pathlen
= strlen(path
);
197 int start
= pathlen
- suffixlen
;
199 /* suffix must start at directory boundary */
200 if ((!start
|| (start
> 0 && is_dir_sep(path
[start
- 1]))) &&
201 !fspathcmp(suffix
, path
+ start
)) {
206 return nr_found
== 1 ? found
: NULL
;
209 struct worktree
*find_worktree(struct worktree
**list
,
214 char *to_free
= NULL
;
216 if ((wt
= find_worktree_by_suffix(list
, arg
)))
220 arg
= to_free
= prefix_filename(prefix
, arg
);
221 wt
= find_worktree_by_path(list
, arg
);
226 struct worktree
*find_worktree_by_path(struct worktree
**list
, const char *p
)
228 struct strbuf wt_path
= STRBUF_INIT
;
229 char *path
= real_pathdup(p
, 0);
233 for (; *list
; list
++) {
234 if (!strbuf_realpath(&wt_path
, (*list
)->path
, 0))
237 if (!fspathcmp(path
, wt_path
.buf
))
241 strbuf_release(&wt_path
);
245 int is_main_worktree(const struct worktree
*wt
)
250 const char *worktree_lock_reason(struct worktree
*wt
)
252 if (is_main_worktree(wt
))
255 if (!wt
->lock_reason_valid
) {
256 struct strbuf path
= STRBUF_INIT
;
258 strbuf_addstr(&path
, worktree_git_path(wt
, "locked"));
259 if (file_exists(path
.buf
)) {
260 struct strbuf lock_reason
= STRBUF_INIT
;
261 if (strbuf_read_file(&lock_reason
, path
.buf
, 0) < 0)
262 die_errno(_("failed to read '%s'"), path
.buf
);
263 strbuf_trim(&lock_reason
);
264 wt
->lock_reason
= strbuf_detach(&lock_reason
, NULL
);
266 wt
->lock_reason
= NULL
;
267 wt
->lock_reason_valid
= 1;
268 strbuf_release(&path
);
271 return wt
->lock_reason
;
274 const char *worktree_prune_reason(struct worktree
*wt
, timestamp_t expire
)
276 struct strbuf reason
= STRBUF_INIT
;
279 if (is_main_worktree(wt
))
281 if (wt
->prune_reason_valid
)
282 return wt
->prune_reason
;
284 if (should_prune_worktree(wt
->id
, &reason
, &path
, expire
))
285 wt
->prune_reason
= strbuf_detach(&reason
, NULL
);
286 wt
->prune_reason_valid
= 1;
288 strbuf_release(&reason
);
290 return wt
->prune_reason
;
293 /* convenient wrapper to deal with NULL strbuf */
294 __attribute__((format (printf
, 2, 3)))
295 static void strbuf_addf_gently(struct strbuf
*buf
, const char *fmt
, ...)
302 va_start(params
, fmt
);
303 strbuf_vaddf(buf
, fmt
, params
);
307 int validate_worktree(const struct worktree
*wt
, struct strbuf
*errmsg
,
310 struct strbuf wt_path
= STRBUF_INIT
;
311 struct strbuf realpath
= STRBUF_INIT
;
315 strbuf_addf(&wt_path
, "%s/.git", wt
->path
);
317 if (is_main_worktree(wt
)) {
318 if (is_directory(wt_path
.buf
)) {
323 * Main worktree using .git file to point to the
324 * repository would make it impossible to know where
325 * the actual worktree is if this function is executed
326 * from another worktree. No .git file support for now.
328 strbuf_addf_gently(errmsg
,
329 _("'%s' at main working tree is not the repository directory"),
335 * Make sure "gitdir" file points to a real .git file and that
336 * file points back here.
338 if (!is_absolute_path(wt
->path
)) {
339 strbuf_addf_gently(errmsg
,
340 _("'%s' file does not contain absolute path to the working tree location"),
341 git_common_path("worktrees/%s/gitdir", wt
->id
));
345 if (flags
& WT_VALIDATE_WORKTREE_MISSING_OK
&&
346 !file_exists(wt
->path
)) {
351 if (!file_exists(wt_path
.buf
)) {
352 strbuf_addf_gently(errmsg
, _("'%s' does not exist"), wt_path
.buf
);
356 path
= xstrdup_or_null(read_gitfile_gently(wt_path
.buf
, &err
));
358 strbuf_addf_gently(errmsg
, _("'%s' is not a .git file, error code %d"),
363 strbuf_realpath(&realpath
, git_common_path("worktrees/%s", wt
->id
), 1);
364 ret
= fspathcmp(path
, realpath
.buf
);
367 strbuf_addf_gently(errmsg
, _("'%s' does not point back to '%s'"),
368 wt
->path
, git_common_path("worktrees/%s", wt
->id
));
371 strbuf_release(&wt_path
);
372 strbuf_release(&realpath
);
376 void update_worktree_location(struct worktree
*wt
, const char *path_
)
378 struct strbuf path
= STRBUF_INIT
;
380 if (is_main_worktree(wt
))
381 BUG("can't relocate main worktree");
383 strbuf_realpath(&path
, path_
, 1);
384 if (fspathcmp(wt
->path
, path
.buf
)) {
385 write_file(git_common_path("worktrees/%s/gitdir", wt
->id
),
386 "%s/.git", path
.buf
);
388 wt
->path
= strbuf_detach(&path
, NULL
);
390 strbuf_release(&path
);
393 int is_worktree_being_rebased(const struct worktree
*wt
,
396 struct wt_status_state state
;
399 memset(&state
, 0, sizeof(state
));
400 found_rebase
= wt_status_check_rebase(wt
, &state
) &&
401 (state
.rebase_in_progress
||
402 state
.rebase_interactive_in_progress
) &&
404 skip_prefix(target
, "refs/heads/", &target
) &&
405 !strcmp(state
.branch
, target
);
406 wt_status_state_free_buffers(&state
);
410 int is_worktree_being_bisected(const struct worktree
*wt
,
413 struct wt_status_state state
;
416 memset(&state
, 0, sizeof(state
));
417 found_bisect
= wt_status_check_bisect(wt
, &state
) &&
418 state
.bisecting_from
&&
419 skip_prefix(target
, "refs/heads/", &target
) &&
420 !strcmp(state
.bisecting_from
, target
);
421 wt_status_state_free_buffers(&state
);
426 * note: this function should be able to detect shared symref even if
427 * HEAD is temporarily detached (e.g. in the middle of rebase or
428 * bisect). New commands that do similar things should update this
431 int is_shared_symref(const struct worktree
*wt
, const char *symref
,
434 const char *symref_target
;
435 struct ref_store
*refs
;
441 if (wt
->is_detached
&& !strcmp(symref
, "HEAD")) {
442 if (is_worktree_being_rebased(wt
, target
))
444 if (is_worktree_being_bisected(wt
, target
))
448 refs
= get_worktree_ref_store(wt
);
449 symref_target
= refs_resolve_ref_unsafe(refs
, symref
, 0,
451 if ((flags
& REF_ISSYMREF
) &&
452 symref_target
&& !strcmp(symref_target
, target
))
458 const struct worktree
*find_shared_symref(struct worktree
**worktrees
,
463 for (int i
= 0; worktrees
[i
]; i
++)
464 if (is_shared_symref(worktrees
[i
], symref
, target
))
470 int submodule_uses_worktrees(const char *path
)
472 char *submodule_gitdir
;
473 struct strbuf sb
= STRBUF_INIT
, err
= STRBUF_INIT
;
477 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
479 submodule_gitdir
= git_pathdup_submodule(path
, "%s", "");
480 if (!submodule_gitdir
)
483 /* The env would be set for the superproject. */
484 get_common_dir_noenv(&sb
, submodule_gitdir
);
485 free(submodule_gitdir
);
487 strbuf_addstr(&sb
, "/config");
488 read_repository_format(&format
, sb
.buf
);
489 if (verify_repository_format(&format
, &err
)) {
490 strbuf_release(&err
);
492 clear_repository_format(&format
);
495 clear_repository_format(&format
);
496 strbuf_release(&err
);
498 /* Replace config by worktrees. */
499 strbuf_setlen(&sb
, sb
.len
- strlen("config"));
500 strbuf_addstr(&sb
, "worktrees");
502 /* See if there is any file inside the worktrees directory. */
503 dir
= opendir(sb
.buf
);
509 d
= readdir_skip_dot_and_dotdot(dir
);
516 void strbuf_worktree_ref(const struct worktree
*wt
,
520 if (parse_worktree_ref(refname
, NULL
, NULL
, NULL
) ==
521 REF_WORKTREE_CURRENT
&&
522 wt
&& !wt
->is_current
) {
523 if (is_main_worktree(wt
))
524 strbuf_addstr(sb
, "main-worktree/");
526 strbuf_addf(sb
, "worktrees/%s/", wt
->id
);
528 strbuf_addstr(sb
, refname
);
531 int other_head_refs(each_ref_fn fn
, void *cb_data
)
533 struct worktree
**worktrees
, **p
;
534 struct strbuf refname
= STRBUF_INIT
;
537 worktrees
= get_worktrees();
538 for (p
= worktrees
; *p
; p
++) {
539 struct worktree
*wt
= *p
;
540 struct object_id oid
;
546 strbuf_reset(&refname
);
547 strbuf_worktree_ref(wt
, &refname
, "HEAD");
548 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
552 ret
= fn(refname
.buf
, &oid
, flag
, cb_data
);
556 free_worktrees(worktrees
);
557 strbuf_release(&refname
);
562 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
563 * pointing at <repo>/worktrees/<id>.
565 static void repair_gitfile(struct worktree
*wt
,
566 worktree_repair_fn fn
, void *cb_data
)
568 struct strbuf dotgit
= STRBUF_INIT
;
569 struct strbuf repo
= STRBUF_INIT
;
571 const char *repair
= NULL
;
574 /* missing worktree can't be repaired */
575 if (!file_exists(wt
->path
))
578 if (!is_directory(wt
->path
)) {
579 fn(1, wt
->path
, _("not a directory"), cb_data
);
583 strbuf_realpath(&repo
, git_common_path("worktrees/%s", wt
->id
), 1);
584 strbuf_addf(&dotgit
, "%s/.git", wt
->path
);
585 backlink
= xstrdup_or_null(read_gitfile_gently(dotgit
.buf
, &err
));
587 if (err
== READ_GITFILE_ERR_NOT_A_FILE
)
588 fn(1, wt
->path
, _(".git is not a file"), cb_data
);
590 repair
= _(".git file broken");
591 else if (fspathcmp(backlink
, repo
.buf
))
592 repair
= _(".git file incorrect");
595 fn(0, wt
->path
, repair
, cb_data
);
596 write_file(dotgit
.buf
, "gitdir: %s", repo
.buf
);
600 strbuf_release(&repo
);
601 strbuf_release(&dotgit
);
604 static void repair_noop(int iserr UNUSED
,
605 const char *path UNUSED
,
606 const char *msg UNUSED
,
607 void *cb_data UNUSED
)
612 void repair_worktrees(worktree_repair_fn fn
, void *cb_data
)
614 struct worktree
**worktrees
= get_worktrees_internal(1);
615 struct worktree
**wt
= worktrees
+ 1; /* +1 skips main worktree */
620 repair_gitfile(*wt
, fn
, cb_data
);
621 free_worktrees(worktrees
);
624 static int is_main_worktree_path(const char *path
)
626 struct strbuf target
= STRBUF_INIT
;
627 struct strbuf maindir
= STRBUF_INIT
;
630 strbuf_add_real_path(&target
, path
);
631 strbuf_strip_suffix(&target
, "/.git");
632 strbuf_add_real_path(&maindir
, get_git_common_dir());
633 strbuf_strip_suffix(&maindir
, "/.git");
634 cmp
= fspathcmp(maindir
.buf
, target
.buf
);
636 strbuf_release(&maindir
);
637 strbuf_release(&target
);
642 * If both the main worktree and linked worktree have been moved, then the
643 * gitfile /path/to/worktree/.git won't point into the repository, thus we
644 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
645 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
646 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
648 static char *infer_backlink(const char *gitfile
)
650 struct strbuf actual
= STRBUF_INIT
;
651 struct strbuf inferred
= STRBUF_INIT
;
654 if (strbuf_read_file(&actual
, gitfile
, 0) < 0)
656 if (!starts_with(actual
.buf
, "gitdir:"))
658 if (!(id
= find_last_dir_sep(actual
.buf
)))
660 strbuf_trim(&actual
);
661 id
++; /* advance past '/' to point at <id> */
664 strbuf_git_common_path(&inferred
, the_repository
, "worktrees/%s", id
);
665 if (!is_directory(inferred
.buf
))
668 strbuf_release(&actual
);
669 return strbuf_detach(&inferred
, NULL
);
672 strbuf_release(&actual
);
673 strbuf_release(&inferred
);
678 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
679 * the worktree's path.
681 void repair_worktree_at_path(const char *path
,
682 worktree_repair_fn fn
, void *cb_data
)
684 struct strbuf dotgit
= STRBUF_INIT
;
685 struct strbuf realdotgit
= STRBUF_INIT
;
686 struct strbuf gitdir
= STRBUF_INIT
;
687 struct strbuf olddotgit
= STRBUF_INIT
;
688 char *backlink
= NULL
;
689 const char *repair
= NULL
;
695 if (is_main_worktree_path(path
))
698 strbuf_addf(&dotgit
, "%s/.git", path
);
699 if (!strbuf_realpath(&realdotgit
, dotgit
.buf
, 0)) {
700 fn(1, path
, _("not a valid path"), cb_data
);
704 backlink
= xstrdup_or_null(read_gitfile_gently(realdotgit
.buf
, &err
));
705 if (err
== READ_GITFILE_ERR_NOT_A_FILE
) {
706 fn(1, realdotgit
.buf
, _("unable to locate repository; .git is not a file"), cb_data
);
708 } else if (err
== READ_GITFILE_ERR_NOT_A_REPO
) {
709 if (!(backlink
= infer_backlink(realdotgit
.buf
))) {
710 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file does not reference a repository"), cb_data
);
714 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file broken"), cb_data
);
718 strbuf_addf(&gitdir
, "%s/gitdir", backlink
);
719 if (strbuf_read_file(&olddotgit
, gitdir
.buf
, 0) < 0)
720 repair
= _("gitdir unreadable");
722 strbuf_rtrim(&olddotgit
);
723 if (fspathcmp(olddotgit
.buf
, realdotgit
.buf
))
724 repair
= _("gitdir incorrect");
728 fn(0, gitdir
.buf
, repair
, cb_data
);
729 write_file(gitdir
.buf
, "%s", realdotgit
.buf
);
733 strbuf_release(&olddotgit
);
734 strbuf_release(&gitdir
);
735 strbuf_release(&realdotgit
);
736 strbuf_release(&dotgit
);
739 int should_prune_worktree(const char *id
, struct strbuf
*reason
, char **wtpath
, timestamp_t expire
)
748 if (!is_directory(git_path("worktrees/%s", id
))) {
749 strbuf_addstr(reason
, _("not a valid directory"));
752 if (file_exists(git_path("worktrees/%s/locked", id
)))
754 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
755 strbuf_addstr(reason
, _("gitdir file does not exist"));
758 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
760 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
764 len
= xsize_t(st
.st_size
);
765 path
= xmallocz(len
);
767 read_result
= read_in_full(fd
, path
, len
);
768 if (read_result
< 0) {
769 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
777 if (read_result
!= len
) {
779 _("short read (expected %"PRIuMAX
" bytes, read %"PRIuMAX
")"),
780 (uintmax_t)len
, (uintmax_t)read_result
);
784 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
787 strbuf_addstr(reason
, _("invalid gitdir file"));
792 if (!file_exists(path
)) {
793 if (stat(git_path("worktrees/%s/index", id
), &st
) ||
794 st
.st_mtime
<= expire
) {
795 strbuf_addstr(reason
, _("gitdir file points to non-existent location"));
807 static int move_config_setting(const char *key
, const char *value
,
808 const char *from_file
, const char *to_file
)
810 if (git_config_set_in_file_gently(to_file
, key
, value
))
811 return error(_("unable to set %s in '%s'"), key
, to_file
);
812 if (git_config_set_in_file_gently(from_file
, key
, NULL
))
813 return error(_("unable to unset %s in '%s'"), key
, from_file
);
817 int init_worktree_config(struct repository
*r
)
821 struct config_set cs
= { { 0 } };
822 const char *core_worktree
;
823 char *common_config_file
;
824 char *main_worktree_file
;
827 * If the extension is already enabled, then we can skip the
830 if (r
->repository_format_worktree_config
)
832 if ((res
= git_config_set_gently("extensions.worktreeConfig", "true")))
833 return error(_("failed to set extensions.worktreeConfig setting"));
835 common_config_file
= xstrfmt("%s/config", r
->commondir
);
836 main_worktree_file
= xstrfmt("%s/config.worktree", r
->commondir
);
838 git_configset_init(&cs
);
839 git_configset_add_file(&cs
, common_config_file
);
842 * If core.bare is true in the common config file, then we need to
843 * move it to the main worktree's config file or it will break all
844 * worktrees. If it is false, then leave it in place because it
845 * _could_ be negating a global core.bare=true.
847 if (!git_configset_get_bool(&cs
, "core.bare", &bare
) && bare
) {
848 if ((res
= move_config_setting("core.bare", "true",
850 main_worktree_file
)))
854 * If core.worktree is set, then the main worktree is located
855 * somewhere different than the parent of the common Git dir.
856 * Relocate that value to avoid breaking all worktrees with this
857 * upgrade to worktree config.
859 if (!git_configset_get_value(&cs
, "core.worktree", &core_worktree
, NULL
)) {
860 if ((res
= move_config_setting("core.worktree", core_worktree
,
862 main_worktree_file
)))
867 * Ensure that we use worktree config for the remaining lifetime
868 * of the current process.
870 r
->repository_format_worktree_config
= 1;
873 git_configset_clear(&cs
);
874 free(common_config_file
);
875 free(main_worktree_file
);