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
->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) ||
79 if (!skip_reading_head
)
80 add_head_info(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
;
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 */
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
);
109 strbuf_release(&path
);
110 strbuf_release(&worktree_path
);
114 static void mark_current_worktree(struct worktree
**worktrees
)
116 char *git_dir
= absolute_pathdup(get_git_dir());
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
))) {
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
;
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
);
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
;
164 ALLOC_GROW(list
, counter
+ 1, alloc
);
165 list
[counter
] = NULL
;
167 mark_current_worktree(list
);
171 struct worktree
**get_worktrees(void)
173 return get_worktrees_internal(0);
176 const char *get_worktree_git_dir(const struct worktree
*wt
)
179 return get_git_dir();
181 return get_git_common_dir();
183 return git_common_path("worktrees/%s", wt
->id
);
186 static struct worktree
*find_worktree_by_suffix(struct worktree
**list
,
189 struct worktree
*found
= NULL
;
190 int nr_found
= 0, suffixlen
;
192 suffixlen
= strlen(suffix
);
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
)) {
208 return nr_found
== 1 ? found
: NULL
;
211 struct worktree
*find_worktree(struct worktree
**list
,
216 char *to_free
= NULL
;
218 if ((wt
= find_worktree_by_suffix(list
, arg
)))
222 arg
= to_free
= prefix_filename(prefix
, arg
);
223 wt
= find_worktree_by_path(list
, arg
);
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);
235 for (; *list
; list
++) {
236 if (!strbuf_realpath(&wt_path
, (*list
)->path
, 0))
239 if (!fspathcmp(path
, wt_path
.buf
))
243 strbuf_release(&wt_path
);
247 int is_main_worktree(const struct worktree
*wt
)
252 const char *worktree_lock_reason(struct worktree
*wt
)
254 if (is_main_worktree(wt
))
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
);
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
;
281 if (is_main_worktree(wt
))
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
);
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
, ...)
304 va_start(params
, fmt
);
305 strbuf_vaddf(buf
, fmt
, params
);
309 int validate_worktree(const struct worktree
*wt
, struct strbuf
*errmsg
,
312 struct strbuf wt_path
= STRBUF_INIT
;
313 struct strbuf realpath
= STRBUF_INIT
;
317 strbuf_addf(&wt_path
, "%s/.git", wt
->path
);
319 if (is_main_worktree(wt
)) {
320 if (is_directory(wt_path
.buf
)) {
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"),
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
));
347 if (flags
& WT_VALIDATE_WORKTREE_MISSING_OK
&&
348 !file_exists(wt
->path
)) {
353 if (!file_exists(wt_path
.buf
)) {
354 strbuf_addf_gently(errmsg
, _("'%s' does not exist"), wt_path
.buf
);
358 path
= xstrdup_or_null(read_gitfile_gently(wt_path
.buf
, &err
));
360 strbuf_addf_gently(errmsg
, _("'%s' is not a .git file, error code %d"),
365 strbuf_realpath(&realpath
, git_common_path("worktrees/%s", wt
->id
), 1);
366 ret
= fspathcmp(path
, realpath
.buf
);
369 strbuf_addf_gently(errmsg
, _("'%s' does not point back to '%s'"),
370 wt
->path
, git_common_path("worktrees/%s", wt
->id
));
373 strbuf_release(&wt_path
);
374 strbuf_release(&realpath
);
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
);
390 wt
->path
= strbuf_detach(&path
, NULL
);
392 strbuf_release(&path
);
395 int is_worktree_being_rebased(const struct worktree
*wt
,
398 struct wt_status_state state
;
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
) &&
406 skip_prefix(target
, "refs/heads/", &target
) &&
407 !strcmp(state
.branch
, target
);
408 wt_status_state_free_buffers(&state
);
412 int is_worktree_being_bisected(const struct worktree
*wt
,
415 struct wt_status_state state
;
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
);
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
433 int is_shared_symref(const struct worktree
*wt
, const char *symref
,
436 const char *symref_target
;
437 struct ref_store
*refs
;
443 if (wt
->is_detached
&& !strcmp(symref
, "HEAD")) {
444 if (is_worktree_being_rebased(wt
, target
))
446 if (is_worktree_being_bisected(wt
, target
))
450 refs
= get_worktree_ref_store(wt
);
451 symref_target
= refs_resolve_ref_unsafe(refs
, symref
, 0,
453 if ((flags
& REF_ISSYMREF
) &&
454 symref_target
&& !strcmp(symref_target
, target
))
460 const struct worktree
*find_shared_symref(struct worktree
**worktrees
,
465 for (int i
= 0; worktrees
[i
]; i
++)
466 if (is_shared_symref(worktrees
[i
], symref
, target
))
472 int submodule_uses_worktrees(const char *path
)
474 char *submodule_gitdir
;
475 struct strbuf sb
= STRBUF_INIT
, err
= STRBUF_INIT
;
479 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
481 submodule_gitdir
= git_pathdup_submodule(path
, "%s", "");
482 if (!submodule_gitdir
)
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
);
494 clear_repository_format(&format
);
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
);
511 d
= readdir_skip_dot_and_dotdot(dir
);
518 void strbuf_worktree_ref(const struct worktree
*wt
,
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/");
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
;
539 worktrees
= get_worktrees();
540 for (p
= worktrees
; *p
; p
++) {
541 struct worktree
*wt
= *p
;
542 struct object_id oid
;
548 strbuf_reset(&refname
);
549 strbuf_worktree_ref(wt
, &refname
, "HEAD");
550 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
554 ret
= fn(refname
.buf
, &oid
, flag
, cb_data
);
558 free_worktrees(worktrees
);
559 strbuf_release(&refname
);
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
;
573 const char *repair
= NULL
;
576 /* missing worktree can't be repaired */
577 if (!file_exists(wt
->path
))
580 if (!is_directory(wt
->path
)) {
581 fn(1, wt
->path
, _("not a directory"), cb_data
);
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
);
592 repair
= _(".git file broken");
593 else if (fspathcmp(backlink
, repo
.buf
))
594 repair
= _(".git file incorrect");
597 fn(0, wt
->path
, repair
, cb_data
);
598 write_file(dotgit
.buf
, "gitdir: %s", repo
.buf
);
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
)
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 */
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
;
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
);
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
;
656 if (strbuf_read_file(&actual
, gitfile
, 0) < 0)
658 if (!starts_with(actual
.buf
, "gitdir:"))
660 if (!(id
= find_last_dir_sep(actual
.buf
)))
662 strbuf_trim(&actual
);
663 id
++; /* advance past '/' to point at <id> */
666 strbuf_git_common_path(&inferred
, the_repository
, "worktrees/%s", id
);
667 if (!is_directory(inferred
.buf
))
670 strbuf_release(&actual
);
671 return strbuf_detach(&inferred
, NULL
);
674 strbuf_release(&actual
);
675 strbuf_release(&inferred
);
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
;
697 if (is_main_worktree_path(path
))
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
);
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
);
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
);
716 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file broken"), cb_data
);
720 strbuf_addf(&gitdir
, "%s/gitdir", backlink
);
721 if (strbuf_read_file(&olddotgit
, gitdir
.buf
, 0) < 0)
722 repair
= _("gitdir unreadable");
724 strbuf_rtrim(&olddotgit
);
725 if (fspathcmp(olddotgit
.buf
, realdotgit
.buf
))
726 repair
= _("gitdir incorrect");
730 fn(0, gitdir
.buf
, repair
, cb_data
);
731 write_file(gitdir
.buf
, "%s", realdotgit
.buf
);
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
)
750 if (!is_directory(git_path("worktrees/%s", id
))) {
751 strbuf_addstr(reason
, _("not a valid directory"));
754 if (file_exists(git_path("worktrees/%s/locked", id
)))
756 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
757 strbuf_addstr(reason
, _("gitdir file does not exist"));
760 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
762 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
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)"),
779 if (read_result
!= len
) {
781 _("short read (expected %"PRIuMAX
" bytes, read %"PRIuMAX
")"),
782 (uintmax_t)len
, (uintmax_t)read_result
);
786 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
789 strbuf_addstr(reason
, _("invalid gitdir file"));
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"));
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
);
819 int init_worktree_config(struct repository
*r
)
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
832 if (r
->repository_format_worktree_config
)
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",
852 main_worktree_file
)))
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
,
864 main_worktree_file
)))
869 * Ensure that we use worktree config for the remaining lifetime
870 * of the current process.
872 r
->repository_format_worktree_config
= 1;
875 git_configset_clear(&cs
);
876 free(common_config_file
);
877 free(main_worktree_file
);