1 #include "git-compat-util.h"
4 #include "environment.h"
6 #include "repository.h"
12 #include "wt-status.h"
16 void free_worktrees(struct worktree
**worktrees
)
20 for (i
= 0; worktrees
[i
]; i
++) {
21 free(worktrees
[i
]->path
);
22 free(worktrees
[i
]->id
);
23 free(worktrees
[i
]->head_ref
);
24 free(worktrees
[i
]->lock_reason
);
25 free(worktrees
[i
]->prune_reason
);
32 * Update head_oid, head_ref and is_detached of the given worktree
34 static void add_head_info(struct worktree
*wt
)
39 target
= refs_resolve_ref_unsafe(get_worktree_ref_store(wt
),
42 &wt
->head_oid
, &flags
);
46 if (flags
& REF_ISSYMREF
)
47 wt
->head_ref
= xstrdup(target
);
53 * get the main worktree
55 static struct worktree
*get_main_worktree(void)
57 struct worktree
*worktree
= NULL
;
58 struct strbuf worktree_path
= STRBUF_INIT
;
60 strbuf_add_real_path(&worktree_path
, get_git_common_dir());
61 strbuf_strip_suffix(&worktree_path
, "/.git");
63 CALLOC_ARRAY(worktree
, 1);
64 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
66 * NEEDSWORK: If this function is called from a secondary worktree and
67 * config.worktree is present, is_bare_repository_cfg will reflect the
68 * contents of config.worktree, not the contents of the main worktree.
69 * This means that worktree->is_bare may be set to 0 even if the main
70 * worktree is configured to be bare.
72 worktree
->is_bare
= (is_bare_repository_cfg
== 1) ||
74 add_head_info(worktree
);
78 static struct worktree
*get_linked_worktree(const char *id
)
80 struct worktree
*worktree
= NULL
;
81 struct strbuf path
= STRBUF_INIT
;
82 struct strbuf worktree_path
= STRBUF_INIT
;
85 die("Missing linked worktree name");
87 strbuf_git_common_path(&path
, the_repository
, "worktrees/%s/gitdir", id
);
88 if (strbuf_read_file(&worktree_path
, path
.buf
, 0) <= 0)
89 /* invalid gitdir file */
91 strbuf_rtrim(&worktree_path
);
92 strbuf_strip_suffix(&worktree_path
, "/.git");
94 CALLOC_ARRAY(worktree
, 1);
95 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
96 worktree
->id
= xstrdup(id
);
97 add_head_info(worktree
);
100 strbuf_release(&path
);
101 strbuf_release(&worktree_path
);
105 static void mark_current_worktree(struct worktree
**worktrees
)
107 char *git_dir
= absolute_pathdup(get_git_dir());
110 for (i
= 0; worktrees
[i
]; i
++) {
111 struct worktree
*wt
= worktrees
[i
];
112 const char *wt_git_dir
= get_worktree_git_dir(wt
);
114 if (!fspathcmp(git_dir
, absolute_path(wt_git_dir
))) {
122 struct worktree
**get_worktrees(void)
124 struct worktree
**list
= NULL
;
125 struct strbuf path
= STRBUF_INIT
;
128 int counter
= 0, alloc
= 2;
130 ALLOC_ARRAY(list
, alloc
);
132 list
[counter
++] = get_main_worktree();
134 strbuf_addf(&path
, "%s/worktrees", get_git_common_dir());
135 dir
= opendir(path
.buf
);
136 strbuf_release(&path
);
138 while ((d
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
139 struct worktree
*linked
= NULL
;
141 if ((linked
= get_linked_worktree(d
->d_name
))) {
142 ALLOC_GROW(list
, counter
+ 1, alloc
);
143 list
[counter
++] = linked
;
148 ALLOC_GROW(list
, counter
+ 1, alloc
);
149 list
[counter
] = NULL
;
151 mark_current_worktree(list
);
155 const char *get_worktree_git_dir(const struct worktree
*wt
)
158 return get_git_dir();
160 return get_git_common_dir();
162 return git_common_path("worktrees/%s", wt
->id
);
165 static struct worktree
*find_worktree_by_suffix(struct worktree
**list
,
168 struct worktree
*found
= NULL
;
169 int nr_found
= 0, suffixlen
;
171 suffixlen
= strlen(suffix
);
175 for (; *list
&& nr_found
< 2; list
++) {
176 const char *path
= (*list
)->path
;
177 int pathlen
= strlen(path
);
178 int start
= pathlen
- suffixlen
;
180 /* suffix must start at directory boundary */
181 if ((!start
|| (start
> 0 && is_dir_sep(path
[start
- 1]))) &&
182 !fspathcmp(suffix
, path
+ start
)) {
187 return nr_found
== 1 ? found
: NULL
;
190 struct worktree
*find_worktree(struct worktree
**list
,
195 char *to_free
= NULL
;
197 if ((wt
= find_worktree_by_suffix(list
, arg
)))
201 arg
= to_free
= prefix_filename(prefix
, arg
);
202 wt
= find_worktree_by_path(list
, arg
);
207 struct worktree
*find_worktree_by_path(struct worktree
**list
, const char *p
)
209 struct strbuf wt_path
= STRBUF_INIT
;
210 char *path
= real_pathdup(p
, 0);
214 for (; *list
; list
++) {
215 if (!strbuf_realpath(&wt_path
, (*list
)->path
, 0))
218 if (!fspathcmp(path
, wt_path
.buf
))
222 strbuf_release(&wt_path
);
226 int is_main_worktree(const struct worktree
*wt
)
231 const char *worktree_lock_reason(struct worktree
*wt
)
233 if (is_main_worktree(wt
))
236 if (!wt
->lock_reason_valid
) {
237 struct strbuf path
= STRBUF_INIT
;
239 strbuf_addstr(&path
, worktree_git_path(wt
, "locked"));
240 if (file_exists(path
.buf
)) {
241 struct strbuf lock_reason
= STRBUF_INIT
;
242 if (strbuf_read_file(&lock_reason
, path
.buf
, 0) < 0)
243 die_errno(_("failed to read '%s'"), path
.buf
);
244 strbuf_trim(&lock_reason
);
245 wt
->lock_reason
= strbuf_detach(&lock_reason
, NULL
);
247 wt
->lock_reason
= NULL
;
248 wt
->lock_reason_valid
= 1;
249 strbuf_release(&path
);
252 return wt
->lock_reason
;
255 const char *worktree_prune_reason(struct worktree
*wt
, timestamp_t expire
)
257 struct strbuf reason
= STRBUF_INIT
;
260 if (is_main_worktree(wt
))
262 if (wt
->prune_reason_valid
)
263 return wt
->prune_reason
;
265 if (should_prune_worktree(wt
->id
, &reason
, &path
, expire
))
266 wt
->prune_reason
= strbuf_detach(&reason
, NULL
);
267 wt
->prune_reason_valid
= 1;
269 strbuf_release(&reason
);
271 return wt
->prune_reason
;
274 /* convenient wrapper to deal with NULL strbuf */
275 __attribute__((format (printf
, 2, 3)))
276 static void strbuf_addf_gently(struct strbuf
*buf
, const char *fmt
, ...)
283 va_start(params
, fmt
);
284 strbuf_vaddf(buf
, fmt
, params
);
288 int validate_worktree(const struct worktree
*wt
, struct strbuf
*errmsg
,
291 struct strbuf wt_path
= STRBUF_INIT
;
292 struct strbuf realpath
= STRBUF_INIT
;
296 strbuf_addf(&wt_path
, "%s/.git", wt
->path
);
298 if (is_main_worktree(wt
)) {
299 if (is_directory(wt_path
.buf
)) {
304 * Main worktree using .git file to point to the
305 * repository would make it impossible to know where
306 * the actual worktree is if this function is executed
307 * from another worktree. No .git file support for now.
309 strbuf_addf_gently(errmsg
,
310 _("'%s' at main working tree is not the repository directory"),
316 * Make sure "gitdir" file points to a real .git file and that
317 * file points back here.
319 if (!is_absolute_path(wt
->path
)) {
320 strbuf_addf_gently(errmsg
,
321 _("'%s' file does not contain absolute path to the working tree location"),
322 git_common_path("worktrees/%s/gitdir", wt
->id
));
326 if (flags
& WT_VALIDATE_WORKTREE_MISSING_OK
&&
327 !file_exists(wt
->path
)) {
332 if (!file_exists(wt_path
.buf
)) {
333 strbuf_addf_gently(errmsg
, _("'%s' does not exist"), wt_path
.buf
);
337 path
= xstrdup_or_null(read_gitfile_gently(wt_path
.buf
, &err
));
339 strbuf_addf_gently(errmsg
, _("'%s' is not a .git file, error code %d"),
344 strbuf_realpath(&realpath
, git_common_path("worktrees/%s", wt
->id
), 1);
345 ret
= fspathcmp(path
, realpath
.buf
);
348 strbuf_addf_gently(errmsg
, _("'%s' does not point back to '%s'"),
349 wt
->path
, git_common_path("worktrees/%s", wt
->id
));
352 strbuf_release(&wt_path
);
353 strbuf_release(&realpath
);
357 void update_worktree_location(struct worktree
*wt
, const char *path_
)
359 struct strbuf path
= STRBUF_INIT
;
361 if (is_main_worktree(wt
))
362 BUG("can't relocate main worktree");
364 strbuf_realpath(&path
, path_
, 1);
365 if (fspathcmp(wt
->path
, path
.buf
)) {
366 write_file(git_common_path("worktrees/%s/gitdir", wt
->id
),
367 "%s/.git", path
.buf
);
369 wt
->path
= strbuf_detach(&path
, NULL
);
371 strbuf_release(&path
);
374 int is_worktree_being_rebased(const struct worktree
*wt
,
377 struct wt_status_state state
;
380 memset(&state
, 0, sizeof(state
));
381 found_rebase
= wt_status_check_rebase(wt
, &state
) &&
382 (state
.rebase_in_progress
||
383 state
.rebase_interactive_in_progress
) &&
385 skip_prefix(target
, "refs/heads/", &target
) &&
386 !strcmp(state
.branch
, target
);
387 wt_status_state_free_buffers(&state
);
391 int is_worktree_being_bisected(const struct worktree
*wt
,
394 struct wt_status_state state
;
397 memset(&state
, 0, sizeof(state
));
398 found_bisect
= wt_status_check_bisect(wt
, &state
) &&
400 skip_prefix(target
, "refs/heads/", &target
) &&
401 !strcmp(state
.branch
, target
);
402 wt_status_state_free_buffers(&state
);
407 * note: this function should be able to detect shared symref even if
408 * HEAD is temporarily detached (e.g. in the middle of rebase or
409 * bisect). New commands that do similar things should update this
412 int is_shared_symref(const struct worktree
*wt
, const char *symref
,
415 const char *symref_target
;
416 struct ref_store
*refs
;
422 if (wt
->is_detached
&& !strcmp(symref
, "HEAD")) {
423 if (is_worktree_being_rebased(wt
, target
))
425 if (is_worktree_being_bisected(wt
, target
))
429 refs
= get_worktree_ref_store(wt
);
430 symref_target
= refs_resolve_ref_unsafe(refs
, symref
, 0,
432 if ((flags
& REF_ISSYMREF
) &&
433 symref_target
&& !strcmp(symref_target
, target
))
439 const struct worktree
*find_shared_symref(struct worktree
**worktrees
,
444 for (int i
= 0; worktrees
[i
]; i
++)
445 if (is_shared_symref(worktrees
[i
], symref
, target
))
451 int submodule_uses_worktrees(const char *path
)
453 char *submodule_gitdir
;
454 struct strbuf sb
= STRBUF_INIT
, err
= STRBUF_INIT
;
458 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
460 submodule_gitdir
= git_pathdup_submodule(path
, "%s", "");
461 if (!submodule_gitdir
)
464 /* The env would be set for the superproject. */
465 get_common_dir_noenv(&sb
, submodule_gitdir
);
466 free(submodule_gitdir
);
468 strbuf_addstr(&sb
, "/config");
469 read_repository_format(&format
, sb
.buf
);
470 if (verify_repository_format(&format
, &err
)) {
471 strbuf_release(&err
);
473 clear_repository_format(&format
);
476 clear_repository_format(&format
);
477 strbuf_release(&err
);
479 /* Replace config by worktrees. */
480 strbuf_setlen(&sb
, sb
.len
- strlen("config"));
481 strbuf_addstr(&sb
, "worktrees");
483 /* See if there is any file inside the worktrees directory. */
484 dir
= opendir(sb
.buf
);
490 d
= readdir_skip_dot_and_dotdot(dir
);
497 void strbuf_worktree_ref(const struct worktree
*wt
,
501 if (parse_worktree_ref(refname
, NULL
, NULL
, NULL
) ==
502 REF_WORKTREE_CURRENT
&&
503 wt
&& !wt
->is_current
) {
504 if (is_main_worktree(wt
))
505 strbuf_addstr(sb
, "main-worktree/");
507 strbuf_addf(sb
, "worktrees/%s/", wt
->id
);
509 strbuf_addstr(sb
, refname
);
512 int other_head_refs(each_ref_fn fn
, void *cb_data
)
514 struct worktree
**worktrees
, **p
;
515 struct strbuf refname
= STRBUF_INIT
;
518 worktrees
= get_worktrees();
519 for (p
= worktrees
; *p
; p
++) {
520 struct worktree
*wt
= *p
;
521 struct object_id oid
;
527 strbuf_reset(&refname
);
528 strbuf_worktree_ref(wt
, &refname
, "HEAD");
529 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
533 ret
= fn(refname
.buf
, &oid
, flag
, cb_data
);
537 free_worktrees(worktrees
);
538 strbuf_release(&refname
);
543 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
544 * pointing at <repo>/worktrees/<id>.
546 static void repair_gitfile(struct worktree
*wt
,
547 worktree_repair_fn fn
, void *cb_data
)
549 struct strbuf dotgit
= STRBUF_INIT
;
550 struct strbuf repo
= STRBUF_INIT
;
552 const char *repair
= NULL
;
555 /* missing worktree can't be repaired */
556 if (!file_exists(wt
->path
))
559 if (!is_directory(wt
->path
)) {
560 fn(1, wt
->path
, _("not a directory"), cb_data
);
564 strbuf_realpath(&repo
, git_common_path("worktrees/%s", wt
->id
), 1);
565 strbuf_addf(&dotgit
, "%s/.git", wt
->path
);
566 backlink
= xstrdup_or_null(read_gitfile_gently(dotgit
.buf
, &err
));
568 if (err
== READ_GITFILE_ERR_NOT_A_FILE
)
569 fn(1, wt
->path
, _(".git is not a file"), cb_data
);
571 repair
= _(".git file broken");
572 else if (fspathcmp(backlink
, repo
.buf
))
573 repair
= _(".git file incorrect");
576 fn(0, wt
->path
, repair
, cb_data
);
577 write_file(dotgit
.buf
, "gitdir: %s", repo
.buf
);
581 strbuf_release(&repo
);
582 strbuf_release(&dotgit
);
585 static void repair_noop(int iserr
, const char *path
, const char *msg
,
591 void repair_worktrees(worktree_repair_fn fn
, void *cb_data
)
593 struct worktree
**worktrees
= get_worktrees();
594 struct worktree
**wt
= worktrees
+ 1; /* +1 skips main worktree */
599 repair_gitfile(*wt
, fn
, cb_data
);
600 free_worktrees(worktrees
);
603 static int is_main_worktree_path(const char *path
)
605 struct strbuf target
= STRBUF_INIT
;
606 struct strbuf maindir
= STRBUF_INIT
;
609 strbuf_add_real_path(&target
, path
);
610 strbuf_strip_suffix(&target
, "/.git");
611 strbuf_add_real_path(&maindir
, get_git_common_dir());
612 strbuf_strip_suffix(&maindir
, "/.git");
613 cmp
= fspathcmp(maindir
.buf
, target
.buf
);
615 strbuf_release(&maindir
);
616 strbuf_release(&target
);
621 * If both the main worktree and linked worktree have been moved, then the
622 * gitfile /path/to/worktree/.git won't point into the repository, thus we
623 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
624 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
625 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
627 static char *infer_backlink(const char *gitfile
)
629 struct strbuf actual
= STRBUF_INIT
;
630 struct strbuf inferred
= STRBUF_INIT
;
633 if (strbuf_read_file(&actual
, gitfile
, 0) < 0)
635 if (!starts_with(actual
.buf
, "gitdir:"))
637 if (!(id
= find_last_dir_sep(actual
.buf
)))
639 strbuf_trim(&actual
);
640 id
++; /* advance past '/' to point at <id> */
643 strbuf_git_common_path(&inferred
, the_repository
, "worktrees/%s", id
);
644 if (!is_directory(inferred
.buf
))
647 strbuf_release(&actual
);
648 return strbuf_detach(&inferred
, NULL
);
651 strbuf_release(&actual
);
652 strbuf_release(&inferred
);
657 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
658 * the worktree's path.
660 void repair_worktree_at_path(const char *path
,
661 worktree_repair_fn fn
, void *cb_data
)
663 struct strbuf dotgit
= STRBUF_INIT
;
664 struct strbuf realdotgit
= STRBUF_INIT
;
665 struct strbuf gitdir
= STRBUF_INIT
;
666 struct strbuf olddotgit
= STRBUF_INIT
;
667 char *backlink
= NULL
;
668 const char *repair
= NULL
;
674 if (is_main_worktree_path(path
))
677 strbuf_addf(&dotgit
, "%s/.git", path
);
678 if (!strbuf_realpath(&realdotgit
, dotgit
.buf
, 0)) {
679 fn(1, path
, _("not a valid path"), cb_data
);
683 backlink
= xstrdup_or_null(read_gitfile_gently(realdotgit
.buf
, &err
));
684 if (err
== READ_GITFILE_ERR_NOT_A_FILE
) {
685 fn(1, realdotgit
.buf
, _("unable to locate repository; .git is not a file"), cb_data
);
687 } else if (err
== READ_GITFILE_ERR_NOT_A_REPO
) {
688 if (!(backlink
= infer_backlink(realdotgit
.buf
))) {
689 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file does not reference a repository"), cb_data
);
693 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file broken"), cb_data
);
697 strbuf_addf(&gitdir
, "%s/gitdir", backlink
);
698 if (strbuf_read_file(&olddotgit
, gitdir
.buf
, 0) < 0)
699 repair
= _("gitdir unreadable");
701 strbuf_rtrim(&olddotgit
);
702 if (fspathcmp(olddotgit
.buf
, realdotgit
.buf
))
703 repair
= _("gitdir incorrect");
707 fn(0, gitdir
.buf
, repair
, cb_data
);
708 write_file(gitdir
.buf
, "%s", realdotgit
.buf
);
712 strbuf_release(&olddotgit
);
713 strbuf_release(&gitdir
);
714 strbuf_release(&realdotgit
);
715 strbuf_release(&dotgit
);
718 int should_prune_worktree(const char *id
, struct strbuf
*reason
, char **wtpath
, timestamp_t expire
)
727 if (!is_directory(git_path("worktrees/%s", id
))) {
728 strbuf_addstr(reason
, _("not a valid directory"));
731 if (file_exists(git_path("worktrees/%s/locked", id
)))
733 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
734 strbuf_addstr(reason
, _("gitdir file does not exist"));
737 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
739 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
743 len
= xsize_t(st
.st_size
);
744 path
= xmallocz(len
);
746 read_result
= read_in_full(fd
, path
, len
);
747 if (read_result
< 0) {
748 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
756 if (read_result
!= len
) {
758 _("short read (expected %"PRIuMAX
" bytes, read %"PRIuMAX
")"),
759 (uintmax_t)len
, (uintmax_t)read_result
);
763 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
766 strbuf_addstr(reason
, _("invalid gitdir file"));
771 if (!file_exists(path
)) {
772 if (stat(git_path("worktrees/%s/index", id
), &st
) ||
773 st
.st_mtime
<= expire
) {
774 strbuf_addstr(reason
, _("gitdir file points to non-existent location"));
786 static int move_config_setting(const char *key
, const char *value
,
787 const char *from_file
, const char *to_file
)
789 if (git_config_set_in_file_gently(to_file
, key
, value
))
790 return error(_("unable to set %s in '%s'"), key
, to_file
);
791 if (git_config_set_in_file_gently(from_file
, key
, NULL
))
792 return error(_("unable to unset %s in '%s'"), key
, from_file
);
796 int init_worktree_config(struct repository
*r
)
800 struct config_set cs
= { { 0 } };
801 const char *core_worktree
;
802 char *common_config_file
;
803 char *main_worktree_file
;
806 * If the extension is already enabled, then we can skip the
809 if (r
->repository_format_worktree_config
)
811 if ((res
= git_config_set_gently("extensions.worktreeConfig", "true")))
812 return error(_("failed to set extensions.worktreeConfig setting"));
814 common_config_file
= xstrfmt("%s/config", r
->commondir
);
815 main_worktree_file
= xstrfmt("%s/config.worktree", r
->commondir
);
817 git_configset_init(&cs
);
818 git_configset_add_file(&cs
, common_config_file
);
821 * If core.bare is true in the common config file, then we need to
822 * move it to the main worktree's config file or it will break all
823 * worktrees. If it is false, then leave it in place because it
824 * _could_ be negating a global core.bare=true.
826 if (!git_configset_get_bool(&cs
, "core.bare", &bare
) && bare
) {
827 if ((res
= move_config_setting("core.bare", "true",
829 main_worktree_file
)))
833 * If core.worktree is set, then the main worktree is located
834 * somewhere different than the parent of the common Git dir.
835 * Relocate that value to avoid breaking all worktrees with this
836 * upgrade to worktree config.
838 if (!git_configset_get_value(&cs
, "core.worktree", &core_worktree
)) {
839 if ((res
= move_config_setting("core.worktree", core_worktree
,
841 main_worktree_file
)))
846 * Ensure that we use worktree config for the remaining lifetime
847 * of the current process.
849 r
->repository_format_worktree_config
= 1;
852 git_configset_clear(&cs
);
853 free(common_config_file
);
854 free(main_worktree_file
);