1 #include "git-compat-util.h"
3 #include "repository.h"
11 void free_worktrees(struct worktree
**worktrees
)
15 for (i
= 0; worktrees
[i
]; i
++) {
16 free(worktrees
[i
]->path
);
17 free(worktrees
[i
]->id
);
18 free(worktrees
[i
]->head_ref
);
19 free(worktrees
[i
]->lock_reason
);
20 free(worktrees
[i
]->prune_reason
);
27 * Update head_oid, head_ref and is_detached of the given worktree
29 static void add_head_info(struct worktree
*wt
)
34 target
= refs_resolve_ref_unsafe(get_worktree_ref_store(wt
),
37 &wt
->head_oid
, &flags
);
41 if (flags
& REF_ISSYMREF
)
42 wt
->head_ref
= xstrdup(target
);
48 * get the main worktree
50 static struct worktree
*get_main_worktree(void)
52 struct worktree
*worktree
= NULL
;
53 struct strbuf worktree_path
= STRBUF_INIT
;
55 strbuf_add_real_path(&worktree_path
, get_git_common_dir());
56 strbuf_strip_suffix(&worktree_path
, "/.git");
58 CALLOC_ARRAY(worktree
, 1);
59 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
61 * NEEDSWORK: If this function is called from a secondary worktree and
62 * config.worktree is present, is_bare_repository_cfg will reflect the
63 * contents of config.worktree, not the contents of the main worktree.
64 * This means that worktree->is_bare may be set to 0 even if the main
65 * worktree is configured to be bare.
67 worktree
->is_bare
= (is_bare_repository_cfg
== 1) ||
69 add_head_info(worktree
);
73 static struct worktree
*get_linked_worktree(const char *id
)
75 struct worktree
*worktree
= NULL
;
76 struct strbuf path
= STRBUF_INIT
;
77 struct strbuf worktree_path
= STRBUF_INIT
;
80 die("Missing linked worktree name");
82 strbuf_git_common_path(&path
, the_repository
, "worktrees/%s/gitdir", id
);
83 if (strbuf_read_file(&worktree_path
, path
.buf
, 0) <= 0)
84 /* invalid gitdir file */
86 strbuf_rtrim(&worktree_path
);
87 strbuf_strip_suffix(&worktree_path
, "/.git");
89 CALLOC_ARRAY(worktree
, 1);
90 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
91 worktree
->id
= xstrdup(id
);
92 add_head_info(worktree
);
95 strbuf_release(&path
);
96 strbuf_release(&worktree_path
);
100 static void mark_current_worktree(struct worktree
**worktrees
)
102 char *git_dir
= absolute_pathdup(get_git_dir());
105 for (i
= 0; worktrees
[i
]; i
++) {
106 struct worktree
*wt
= worktrees
[i
];
107 const char *wt_git_dir
= get_worktree_git_dir(wt
);
109 if (!fspathcmp(git_dir
, absolute_path(wt_git_dir
))) {
117 struct worktree
**get_worktrees(void)
119 struct worktree
**list
= NULL
;
120 struct strbuf path
= STRBUF_INIT
;
123 int counter
= 0, alloc
= 2;
125 ALLOC_ARRAY(list
, alloc
);
127 list
[counter
++] = get_main_worktree();
129 strbuf_addf(&path
, "%s/worktrees", get_git_common_dir());
130 dir
= opendir(path
.buf
);
131 strbuf_release(&path
);
133 while ((d
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
134 struct worktree
*linked
= NULL
;
136 if ((linked
= get_linked_worktree(d
->d_name
))) {
137 ALLOC_GROW(list
, counter
+ 1, alloc
);
138 list
[counter
++] = linked
;
143 ALLOC_GROW(list
, counter
+ 1, alloc
);
144 list
[counter
] = NULL
;
146 mark_current_worktree(list
);
150 const char *get_worktree_git_dir(const struct worktree
*wt
)
153 return get_git_dir();
155 return get_git_common_dir();
157 return git_common_path("worktrees/%s", wt
->id
);
160 static struct worktree
*find_worktree_by_suffix(struct worktree
**list
,
163 struct worktree
*found
= NULL
;
164 int nr_found
= 0, suffixlen
;
166 suffixlen
= strlen(suffix
);
170 for (; *list
&& nr_found
< 2; list
++) {
171 const char *path
= (*list
)->path
;
172 int pathlen
= strlen(path
);
173 int start
= pathlen
- suffixlen
;
175 /* suffix must start at directory boundary */
176 if ((!start
|| (start
> 0 && is_dir_sep(path
[start
- 1]))) &&
177 !fspathcmp(suffix
, path
+ start
)) {
182 return nr_found
== 1 ? found
: NULL
;
185 struct worktree
*find_worktree(struct worktree
**list
,
190 char *to_free
= NULL
;
192 if ((wt
= find_worktree_by_suffix(list
, arg
)))
196 arg
= to_free
= prefix_filename(prefix
, arg
);
197 wt
= find_worktree_by_path(list
, arg
);
202 struct worktree
*find_worktree_by_path(struct worktree
**list
, const char *p
)
204 struct strbuf wt_path
= STRBUF_INIT
;
205 char *path
= real_pathdup(p
, 0);
209 for (; *list
; list
++) {
210 if (!strbuf_realpath(&wt_path
, (*list
)->path
, 0))
213 if (!fspathcmp(path
, wt_path
.buf
))
217 strbuf_release(&wt_path
);
221 int is_main_worktree(const struct worktree
*wt
)
226 const char *worktree_lock_reason(struct worktree
*wt
)
228 if (is_main_worktree(wt
))
231 if (!wt
->lock_reason_valid
) {
232 struct strbuf path
= STRBUF_INIT
;
234 strbuf_addstr(&path
, worktree_git_path(wt
, "locked"));
235 if (file_exists(path
.buf
)) {
236 struct strbuf lock_reason
= STRBUF_INIT
;
237 if (strbuf_read_file(&lock_reason
, path
.buf
, 0) < 0)
238 die_errno(_("failed to read '%s'"), path
.buf
);
239 strbuf_trim(&lock_reason
);
240 wt
->lock_reason
= strbuf_detach(&lock_reason
, NULL
);
242 wt
->lock_reason
= NULL
;
243 wt
->lock_reason_valid
= 1;
244 strbuf_release(&path
);
247 return wt
->lock_reason
;
250 const char *worktree_prune_reason(struct worktree
*wt
, timestamp_t expire
)
252 struct strbuf reason
= STRBUF_INIT
;
255 if (is_main_worktree(wt
))
257 if (wt
->prune_reason_valid
)
258 return wt
->prune_reason
;
260 if (should_prune_worktree(wt
->id
, &reason
, &path
, expire
))
261 wt
->prune_reason
= strbuf_detach(&reason
, NULL
);
262 wt
->prune_reason_valid
= 1;
264 strbuf_release(&reason
);
266 return wt
->prune_reason
;
269 /* convenient wrapper to deal with NULL strbuf */
270 __attribute__((format (printf
, 2, 3)))
271 static void strbuf_addf_gently(struct strbuf
*buf
, const char *fmt
, ...)
278 va_start(params
, fmt
);
279 strbuf_vaddf(buf
, fmt
, params
);
283 int validate_worktree(const struct worktree
*wt
, struct strbuf
*errmsg
,
286 struct strbuf wt_path
= STRBUF_INIT
;
287 struct strbuf realpath
= STRBUF_INIT
;
291 strbuf_addf(&wt_path
, "%s/.git", wt
->path
);
293 if (is_main_worktree(wt
)) {
294 if (is_directory(wt_path
.buf
)) {
299 * Main worktree using .git file to point to the
300 * repository would make it impossible to know where
301 * the actual worktree is if this function is executed
302 * from another worktree. No .git file support for now.
304 strbuf_addf_gently(errmsg
,
305 _("'%s' at main working tree is not the repository directory"),
311 * Make sure "gitdir" file points to a real .git file and that
312 * file points back here.
314 if (!is_absolute_path(wt
->path
)) {
315 strbuf_addf_gently(errmsg
,
316 _("'%s' file does not contain absolute path to the working tree location"),
317 git_common_path("worktrees/%s/gitdir", wt
->id
));
321 if (flags
& WT_VALIDATE_WORKTREE_MISSING_OK
&&
322 !file_exists(wt
->path
)) {
327 if (!file_exists(wt_path
.buf
)) {
328 strbuf_addf_gently(errmsg
, _("'%s' does not exist"), wt_path
.buf
);
332 path
= xstrdup_or_null(read_gitfile_gently(wt_path
.buf
, &err
));
334 strbuf_addf_gently(errmsg
, _("'%s' is not a .git file, error code %d"),
339 strbuf_realpath(&realpath
, git_common_path("worktrees/%s", wt
->id
), 1);
340 ret
= fspathcmp(path
, realpath
.buf
);
343 strbuf_addf_gently(errmsg
, _("'%s' does not point back to '%s'"),
344 wt
->path
, git_common_path("worktrees/%s", wt
->id
));
347 strbuf_release(&wt_path
);
348 strbuf_release(&realpath
);
352 void update_worktree_location(struct worktree
*wt
, const char *path_
)
354 struct strbuf path
= STRBUF_INIT
;
356 if (is_main_worktree(wt
))
357 BUG("can't relocate main worktree");
359 strbuf_realpath(&path
, path_
, 1);
360 if (fspathcmp(wt
->path
, path
.buf
)) {
361 write_file(git_common_path("worktrees/%s/gitdir", wt
->id
),
362 "%s/.git", path
.buf
);
364 wt
->path
= strbuf_detach(&path
, NULL
);
366 strbuf_release(&path
);
369 int is_worktree_being_rebased(const struct worktree
*wt
,
372 struct wt_status_state state
;
375 memset(&state
, 0, sizeof(state
));
376 found_rebase
= wt_status_check_rebase(wt
, &state
) &&
377 (state
.rebase_in_progress
||
378 state
.rebase_interactive_in_progress
) &&
380 skip_prefix(target
, "refs/heads/", &target
) &&
381 !strcmp(state
.branch
, target
);
382 wt_status_state_free_buffers(&state
);
386 int is_worktree_being_bisected(const struct worktree
*wt
,
389 struct wt_status_state state
;
392 memset(&state
, 0, sizeof(state
));
393 found_bisect
= wt_status_check_bisect(wt
, &state
) &&
395 skip_prefix(target
, "refs/heads/", &target
) &&
396 !strcmp(state
.branch
, target
);
397 wt_status_state_free_buffers(&state
);
402 * note: this function should be able to detect shared symref even if
403 * HEAD is temporarily detached (e.g. in the middle of rebase or
404 * bisect). New commands that do similar things should update this
407 const struct worktree
*find_shared_symref(struct worktree
**worktrees
,
411 const struct worktree
*existing
= NULL
;
414 for (i
= 0; worktrees
[i
]; i
++) {
415 struct worktree
*wt
= worktrees
[i
];
416 const char *symref_target
;
417 struct ref_store
*refs
;
423 if (wt
->is_detached
&& !strcmp(symref
, "HEAD")) {
424 if (is_worktree_being_rebased(wt
, target
)) {
428 if (is_worktree_being_bisected(wt
, target
)) {
434 refs
= get_worktree_ref_store(wt
);
435 symref_target
= refs_resolve_ref_unsafe(refs
, symref
, 0,
437 if ((flags
& REF_ISSYMREF
) &&
438 symref_target
&& !strcmp(symref_target
, target
)) {
447 int submodule_uses_worktrees(const char *path
)
449 char *submodule_gitdir
;
450 struct strbuf sb
= STRBUF_INIT
, err
= STRBUF_INIT
;
454 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
456 submodule_gitdir
= git_pathdup_submodule(path
, "%s", "");
457 if (!submodule_gitdir
)
460 /* The env would be set for the superproject. */
461 get_common_dir_noenv(&sb
, submodule_gitdir
);
462 free(submodule_gitdir
);
464 strbuf_addstr(&sb
, "/config");
465 read_repository_format(&format
, sb
.buf
);
466 if (verify_repository_format(&format
, &err
)) {
467 strbuf_release(&err
);
469 clear_repository_format(&format
);
472 clear_repository_format(&format
);
473 strbuf_release(&err
);
475 /* Replace config by worktrees. */
476 strbuf_setlen(&sb
, sb
.len
- strlen("config"));
477 strbuf_addstr(&sb
, "worktrees");
479 /* See if there is any file inside the worktrees directory. */
480 dir
= opendir(sb
.buf
);
486 d
= readdir_skip_dot_and_dotdot(dir
);
493 void strbuf_worktree_ref(const struct worktree
*wt
,
497 if (parse_worktree_ref(refname
, NULL
, NULL
, NULL
) ==
498 REF_WORKTREE_CURRENT
&&
499 wt
&& !wt
->is_current
) {
500 if (is_main_worktree(wt
))
501 strbuf_addstr(sb
, "main-worktree/");
503 strbuf_addf(sb
, "worktrees/%s/", wt
->id
);
505 strbuf_addstr(sb
, refname
);
508 int other_head_refs(each_ref_fn fn
, void *cb_data
)
510 struct worktree
**worktrees
, **p
;
511 struct strbuf refname
= STRBUF_INIT
;
514 worktrees
= get_worktrees();
515 for (p
= worktrees
; *p
; p
++) {
516 struct worktree
*wt
= *p
;
517 struct object_id oid
;
523 strbuf_reset(&refname
);
524 strbuf_worktree_ref(wt
, &refname
, "HEAD");
525 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
529 ret
= fn(refname
.buf
, &oid
, flag
, cb_data
);
533 free_worktrees(worktrees
);
534 strbuf_release(&refname
);
539 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
540 * pointing at <repo>/worktrees/<id>.
542 static void repair_gitfile(struct worktree
*wt
,
543 worktree_repair_fn fn
, void *cb_data
)
545 struct strbuf dotgit
= STRBUF_INIT
;
546 struct strbuf repo
= STRBUF_INIT
;
548 const char *repair
= NULL
;
551 /* missing worktree can't be repaired */
552 if (!file_exists(wt
->path
))
555 if (!is_directory(wt
->path
)) {
556 fn(1, wt
->path
, _("not a directory"), cb_data
);
560 strbuf_realpath(&repo
, git_common_path("worktrees/%s", wt
->id
), 1);
561 strbuf_addf(&dotgit
, "%s/.git", wt
->path
);
562 backlink
= xstrdup_or_null(read_gitfile_gently(dotgit
.buf
, &err
));
564 if (err
== READ_GITFILE_ERR_NOT_A_FILE
)
565 fn(1, wt
->path
, _(".git is not a file"), cb_data
);
567 repair
= _(".git file broken");
568 else if (fspathcmp(backlink
, repo
.buf
))
569 repair
= _(".git file incorrect");
572 fn(0, wt
->path
, repair
, cb_data
);
573 write_file(dotgit
.buf
, "gitdir: %s", repo
.buf
);
577 strbuf_release(&repo
);
578 strbuf_release(&dotgit
);
581 static void repair_noop(int iserr
, const char *path
, const char *msg
,
587 void repair_worktrees(worktree_repair_fn fn
, void *cb_data
)
589 struct worktree
**worktrees
= get_worktrees();
590 struct worktree
**wt
= worktrees
+ 1; /* +1 skips main worktree */
595 repair_gitfile(*wt
, fn
, cb_data
);
596 free_worktrees(worktrees
);
599 static int is_main_worktree_path(const char *path
)
601 struct strbuf target
= STRBUF_INIT
;
602 struct strbuf maindir
= STRBUF_INIT
;
605 strbuf_add_real_path(&target
, path
);
606 strbuf_strip_suffix(&target
, "/.git");
607 strbuf_add_real_path(&maindir
, get_git_common_dir());
608 strbuf_strip_suffix(&maindir
, "/.git");
609 cmp
= fspathcmp(maindir
.buf
, target
.buf
);
611 strbuf_release(&maindir
);
612 strbuf_release(&target
);
617 * If both the main worktree and linked worktree have been moved, then the
618 * gitfile /path/to/worktree/.git won't point into the repository, thus we
619 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
620 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
621 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
623 static char *infer_backlink(const char *gitfile
)
625 struct strbuf actual
= STRBUF_INIT
;
626 struct strbuf inferred
= STRBUF_INIT
;
629 if (strbuf_read_file(&actual
, gitfile
, 0) < 0)
631 if (!starts_with(actual
.buf
, "gitdir:"))
633 if (!(id
= find_last_dir_sep(actual
.buf
)))
635 strbuf_trim(&actual
);
636 id
++; /* advance past '/' to point at <id> */
639 strbuf_git_common_path(&inferred
, the_repository
, "worktrees/%s", id
);
640 if (!is_directory(inferred
.buf
))
643 strbuf_release(&actual
);
644 return strbuf_detach(&inferred
, NULL
);
647 strbuf_release(&actual
);
648 strbuf_release(&inferred
);
653 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
654 * the worktree's path.
656 void repair_worktree_at_path(const char *path
,
657 worktree_repair_fn fn
, void *cb_data
)
659 struct strbuf dotgit
= STRBUF_INIT
;
660 struct strbuf realdotgit
= STRBUF_INIT
;
661 struct strbuf gitdir
= STRBUF_INIT
;
662 struct strbuf olddotgit
= STRBUF_INIT
;
663 char *backlink
= NULL
;
664 const char *repair
= NULL
;
670 if (is_main_worktree_path(path
))
673 strbuf_addf(&dotgit
, "%s/.git", path
);
674 if (!strbuf_realpath(&realdotgit
, dotgit
.buf
, 0)) {
675 fn(1, path
, _("not a valid path"), cb_data
);
679 backlink
= xstrdup_or_null(read_gitfile_gently(realdotgit
.buf
, &err
));
680 if (err
== READ_GITFILE_ERR_NOT_A_FILE
) {
681 fn(1, realdotgit
.buf
, _("unable to locate repository; .git is not a file"), cb_data
);
683 } else if (err
== READ_GITFILE_ERR_NOT_A_REPO
) {
684 if (!(backlink
= infer_backlink(realdotgit
.buf
))) {
685 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file does not reference a repository"), cb_data
);
689 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file broken"), cb_data
);
693 strbuf_addf(&gitdir
, "%s/gitdir", backlink
);
694 if (strbuf_read_file(&olddotgit
, gitdir
.buf
, 0) < 0)
695 repair
= _("gitdir unreadable");
697 strbuf_rtrim(&olddotgit
);
698 if (fspathcmp(olddotgit
.buf
, realdotgit
.buf
))
699 repair
= _("gitdir incorrect");
703 fn(0, gitdir
.buf
, repair
, cb_data
);
704 write_file(gitdir
.buf
, "%s", realdotgit
.buf
);
708 strbuf_release(&olddotgit
);
709 strbuf_release(&gitdir
);
710 strbuf_release(&realdotgit
);
711 strbuf_release(&dotgit
);
714 int should_prune_worktree(const char *id
, struct strbuf
*reason
, char **wtpath
, timestamp_t expire
)
723 if (!is_directory(git_path("worktrees/%s", id
))) {
724 strbuf_addstr(reason
, _("not a valid directory"));
727 if (file_exists(git_path("worktrees/%s/locked", id
)))
729 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
730 strbuf_addstr(reason
, _("gitdir file does not exist"));
733 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
735 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
739 len
= xsize_t(st
.st_size
);
740 path
= xmallocz(len
);
742 read_result
= read_in_full(fd
, path
, len
);
743 if (read_result
< 0) {
744 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
752 if (read_result
!= len
) {
754 _("short read (expected %"PRIuMAX
" bytes, read %"PRIuMAX
")"),
755 (uintmax_t)len
, (uintmax_t)read_result
);
759 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
762 strbuf_addstr(reason
, _("invalid gitdir file"));
767 if (!file_exists(path
)) {
768 if (stat(git_path("worktrees/%s/index", id
), &st
) ||
769 st
.st_mtime
<= expire
) {
770 strbuf_addstr(reason
, _("gitdir file points to non-existent location"));
782 static int move_config_setting(const char *key
, const char *value
,
783 const char *from_file
, const char *to_file
)
785 if (git_config_set_in_file_gently(to_file
, key
, value
))
786 return error(_("unable to set %s in '%s'"), key
, to_file
);
787 if (git_config_set_in_file_gently(from_file
, key
, NULL
))
788 return error(_("unable to unset %s in '%s'"), key
, from_file
);
792 int init_worktree_config(struct repository
*r
)
796 struct config_set cs
= { { 0 } };
797 const char *core_worktree
;
798 char *common_config_file
;
799 char *main_worktree_file
;
802 * If the extension is already enabled, then we can skip the
805 if (repository_format_worktree_config
)
807 if ((res
= git_config_set_gently("extensions.worktreeConfig", "true")))
808 return error(_("failed to set extensions.worktreeConfig setting"));
810 common_config_file
= xstrfmt("%s/config", r
->commondir
);
811 main_worktree_file
= xstrfmt("%s/config.worktree", r
->commondir
);
813 git_configset_init(&cs
);
814 git_configset_add_file(&cs
, common_config_file
);
817 * If core.bare is true in the common config file, then we need to
818 * move it to the main worktree's config file or it will break all
819 * worktrees. If it is false, then leave it in place because it
820 * _could_ be negating a global core.bare=true.
822 if (!git_configset_get_bool(&cs
, "core.bare", &bare
) && bare
) {
823 if ((res
= move_config_setting("core.bare", "true",
825 main_worktree_file
)))
829 * If core.worktree is set, then the main worktree is located
830 * somewhere different than the parent of the common Git dir.
831 * Relocate that value to avoid breaking all worktrees with this
832 * upgrade to worktree config.
834 if (!git_configset_get_value(&cs
, "core.worktree", &core_worktree
)) {
835 if ((res
= move_config_setting("core.worktree", core_worktree
,
837 main_worktree_file
)))
842 * Ensure that we use worktree config for the remaining lifetime
843 * of the current process.
845 repository_format_worktree_config
= 1;
848 git_configset_clear(&cs
);
849 free(common_config_file
);
850 free(main_worktree_file
);