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
);
56 static int is_current_worktree(struct worktree
*wt
)
58 char *git_dir
= absolute_pathdup(get_git_dir());
59 const char *wt_git_dir
= get_worktree_git_dir(wt
);
60 int is_current
= !fspathcmp(git_dir
, absolute_path(wt_git_dir
));
66 * get the main worktree
68 static struct worktree
*get_main_worktree(int skip_reading_head
)
70 struct worktree
*worktree
= NULL
;
71 struct strbuf worktree_path
= STRBUF_INIT
;
73 strbuf_add_real_path(&worktree_path
, get_git_common_dir());
74 strbuf_strip_suffix(&worktree_path
, "/.git");
76 CALLOC_ARRAY(worktree
, 1);
77 worktree
->repo
= the_repository
;
78 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
80 * NEEDSWORK: If this function is called from a secondary worktree and
81 * config.worktree is present, is_bare_repository_cfg will reflect the
82 * contents of config.worktree, not the contents of the main worktree.
83 * This means that worktree->is_bare may be set to 0 even if the main
84 * worktree is configured to be bare.
86 worktree
->is_bare
= (is_bare_repository_cfg
== 1) ||
88 worktree
->is_current
= is_current_worktree(worktree
);
89 if (!skip_reading_head
)
90 add_head_info(worktree
);
94 struct worktree
*get_linked_worktree(const char *id
,
95 int skip_reading_head
)
97 struct worktree
*worktree
= NULL
;
98 struct strbuf path
= STRBUF_INIT
;
99 struct strbuf worktree_path
= STRBUF_INIT
;
102 die("Missing linked worktree name");
104 strbuf_git_common_path(&path
, the_repository
, "worktrees/%s/gitdir", id
);
105 if (strbuf_read_file(&worktree_path
, path
.buf
, 0) <= 0)
106 /* invalid gitdir file */
108 strbuf_rtrim(&worktree_path
);
109 strbuf_strip_suffix(&worktree_path
, "/.git");
111 CALLOC_ARRAY(worktree
, 1);
112 worktree
->repo
= the_repository
;
113 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
114 worktree
->id
= xstrdup(id
);
115 worktree
->is_current
= is_current_worktree(worktree
);
116 if (!skip_reading_head
)
117 add_head_info(worktree
);
120 strbuf_release(&path
);
121 strbuf_release(&worktree_path
);
126 * NEEDSWORK: This function exists so that we can look up metadata of a
127 * worktree without trying to access any of its internals like the refdb. It
128 * would be preferable to instead have a corruption-tolerant function for
129 * retrieving worktree metadata that could be used when the worktree is known
130 * to not be in a healthy state, e.g. when creating or repairing it.
132 static struct worktree
**get_worktrees_internal(int skip_reading_head
)
134 struct worktree
**list
= NULL
;
135 struct strbuf path
= STRBUF_INIT
;
138 int counter
= 0, alloc
= 2;
140 ALLOC_ARRAY(list
, alloc
);
142 list
[counter
++] = get_main_worktree(skip_reading_head
);
144 strbuf_addf(&path
, "%s/worktrees", get_git_common_dir());
145 dir
= opendir(path
.buf
);
146 strbuf_release(&path
);
148 while ((d
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
149 struct worktree
*linked
= NULL
;
151 if ((linked
= get_linked_worktree(d
->d_name
, skip_reading_head
))) {
152 ALLOC_GROW(list
, counter
+ 1, alloc
);
153 list
[counter
++] = linked
;
158 ALLOC_GROW(list
, counter
+ 1, alloc
);
159 list
[counter
] = NULL
;
164 struct worktree
**get_worktrees(void)
166 return get_worktrees_internal(0);
169 const char *get_worktree_git_dir(const struct worktree
*wt
)
172 return get_git_dir();
174 return get_git_common_dir();
176 return git_common_path("worktrees/%s", wt
->id
);
179 static struct worktree
*find_worktree_by_suffix(struct worktree
**list
,
182 struct worktree
*found
= NULL
;
183 int nr_found
= 0, suffixlen
;
185 suffixlen
= strlen(suffix
);
189 for (; *list
&& nr_found
< 2; list
++) {
190 const char *path
= (*list
)->path
;
191 int pathlen
= strlen(path
);
192 int start
= pathlen
- suffixlen
;
194 /* suffix must start at directory boundary */
195 if ((!start
|| (start
> 0 && is_dir_sep(path
[start
- 1]))) &&
196 !fspathcmp(suffix
, path
+ start
)) {
201 return nr_found
== 1 ? found
: NULL
;
204 struct worktree
*find_worktree(struct worktree
**list
,
209 char *to_free
= NULL
;
211 if ((wt
= find_worktree_by_suffix(list
, arg
)))
215 arg
= to_free
= prefix_filename(prefix
, arg
);
216 wt
= find_worktree_by_path(list
, arg
);
221 struct worktree
*find_worktree_by_path(struct worktree
**list
, const char *p
)
223 struct strbuf wt_path
= STRBUF_INIT
;
224 char *path
= real_pathdup(p
, 0);
228 for (; *list
; list
++) {
229 if (!strbuf_realpath(&wt_path
, (*list
)->path
, 0))
232 if (!fspathcmp(path
, wt_path
.buf
))
236 strbuf_release(&wt_path
);
240 int is_main_worktree(const struct worktree
*wt
)
245 const char *worktree_lock_reason(struct worktree
*wt
)
247 if (is_main_worktree(wt
))
250 if (!wt
->lock_reason_valid
) {
251 struct strbuf path
= STRBUF_INIT
;
253 strbuf_addstr(&path
, worktree_git_path(wt
, "locked"));
254 if (file_exists(path
.buf
)) {
255 struct strbuf lock_reason
= STRBUF_INIT
;
256 if (strbuf_read_file(&lock_reason
, path
.buf
, 0) < 0)
257 die_errno(_("failed to read '%s'"), path
.buf
);
258 strbuf_trim(&lock_reason
);
259 wt
->lock_reason
= strbuf_detach(&lock_reason
, NULL
);
261 wt
->lock_reason
= NULL
;
262 wt
->lock_reason_valid
= 1;
263 strbuf_release(&path
);
266 return wt
->lock_reason
;
269 const char *worktree_prune_reason(struct worktree
*wt
, timestamp_t expire
)
271 struct strbuf reason
= STRBUF_INIT
;
274 if (is_main_worktree(wt
))
276 if (wt
->prune_reason_valid
)
277 return wt
->prune_reason
;
279 if (should_prune_worktree(wt
->id
, &reason
, &path
, expire
))
280 wt
->prune_reason
= strbuf_detach(&reason
, NULL
);
281 wt
->prune_reason_valid
= 1;
283 strbuf_release(&reason
);
285 return wt
->prune_reason
;
288 /* convenient wrapper to deal with NULL strbuf */
289 __attribute__((format (printf
, 2, 3)))
290 static void strbuf_addf_gently(struct strbuf
*buf
, const char *fmt
, ...)
297 va_start(params
, fmt
);
298 strbuf_vaddf(buf
, fmt
, params
);
302 int validate_worktree(const struct worktree
*wt
, struct strbuf
*errmsg
,
305 struct strbuf wt_path
= STRBUF_INIT
;
306 struct strbuf realpath
= STRBUF_INIT
;
310 strbuf_addf(&wt_path
, "%s/.git", wt
->path
);
312 if (is_main_worktree(wt
)) {
313 if (is_directory(wt_path
.buf
)) {
318 * Main worktree using .git file to point to the
319 * repository would make it impossible to know where
320 * the actual worktree is if this function is executed
321 * from another worktree. No .git file support for now.
323 strbuf_addf_gently(errmsg
,
324 _("'%s' at main working tree is not the repository directory"),
330 * Make sure "gitdir" file points to a real .git file and that
331 * file points back here.
333 if (!is_absolute_path(wt
->path
)) {
334 strbuf_addf_gently(errmsg
,
335 _("'%s' file does not contain absolute path to the working tree location"),
336 git_common_path("worktrees/%s/gitdir", wt
->id
));
340 if (flags
& WT_VALIDATE_WORKTREE_MISSING_OK
&&
341 !file_exists(wt
->path
)) {
346 if (!file_exists(wt_path
.buf
)) {
347 strbuf_addf_gently(errmsg
, _("'%s' does not exist"), wt_path
.buf
);
351 path
= xstrdup_or_null(read_gitfile_gently(wt_path
.buf
, &err
));
353 strbuf_addf_gently(errmsg
, _("'%s' is not a .git file, error code %d"),
358 strbuf_realpath(&realpath
, git_common_path("worktrees/%s", wt
->id
), 1);
359 ret
= fspathcmp(path
, realpath
.buf
);
362 strbuf_addf_gently(errmsg
, _("'%s' does not point back to '%s'"),
363 wt
->path
, git_common_path("worktrees/%s", wt
->id
));
366 strbuf_release(&wt_path
);
367 strbuf_release(&realpath
);
371 void update_worktree_location(struct worktree
*wt
, const char *path_
)
373 struct strbuf path
= STRBUF_INIT
;
375 if (is_main_worktree(wt
))
376 BUG("can't relocate main worktree");
378 strbuf_realpath(&path
, path_
, 1);
379 if (fspathcmp(wt
->path
, path
.buf
)) {
380 write_file(git_common_path("worktrees/%s/gitdir", wt
->id
),
381 "%s/.git", path
.buf
);
383 wt
->path
= strbuf_detach(&path
, NULL
);
385 strbuf_release(&path
);
388 int is_worktree_being_rebased(const struct worktree
*wt
,
391 struct wt_status_state state
;
394 memset(&state
, 0, sizeof(state
));
395 found_rebase
= wt_status_check_rebase(wt
, &state
) &&
396 (state
.rebase_in_progress
||
397 state
.rebase_interactive_in_progress
) &&
399 skip_prefix(target
, "refs/heads/", &target
) &&
400 !strcmp(state
.branch
, target
);
401 wt_status_state_free_buffers(&state
);
405 int is_worktree_being_bisected(const struct worktree
*wt
,
408 struct wt_status_state state
;
411 memset(&state
, 0, sizeof(state
));
412 found_bisect
= wt_status_check_bisect(wt
, &state
) &&
413 state
.bisecting_from
&&
414 skip_prefix(target
, "refs/heads/", &target
) &&
415 !strcmp(state
.bisecting_from
, target
);
416 wt_status_state_free_buffers(&state
);
421 * note: this function should be able to detect shared symref even if
422 * HEAD is temporarily detached (e.g. in the middle of rebase or
423 * bisect). New commands that do similar things should update this
426 int is_shared_symref(const struct worktree
*wt
, const char *symref
,
429 const char *symref_target
;
430 struct ref_store
*refs
;
436 if (wt
->is_detached
&& !strcmp(symref
, "HEAD")) {
437 if (is_worktree_being_rebased(wt
, target
))
439 if (is_worktree_being_bisected(wt
, target
))
443 refs
= get_worktree_ref_store(wt
);
444 symref_target
= refs_resolve_ref_unsafe(refs
, symref
, 0,
446 if ((flags
& REF_ISSYMREF
) &&
447 symref_target
&& !strcmp(symref_target
, target
))
453 const struct worktree
*find_shared_symref(struct worktree
**worktrees
,
458 for (int i
= 0; worktrees
[i
]; i
++)
459 if (is_shared_symref(worktrees
[i
], symref
, target
))
465 int submodule_uses_worktrees(const char *path
)
467 char *submodule_gitdir
;
468 struct strbuf sb
= STRBUF_INIT
, err
= STRBUF_INIT
;
472 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
474 submodule_gitdir
= git_pathdup_submodule(path
, "%s", "");
475 if (!submodule_gitdir
)
478 /* The env would be set for the superproject. */
479 get_common_dir_noenv(&sb
, submodule_gitdir
);
480 free(submodule_gitdir
);
482 strbuf_addstr(&sb
, "/config");
483 read_repository_format(&format
, sb
.buf
);
484 if (verify_repository_format(&format
, &err
)) {
485 strbuf_release(&err
);
487 clear_repository_format(&format
);
490 clear_repository_format(&format
);
491 strbuf_release(&err
);
493 /* Replace config by worktrees. */
494 strbuf_setlen(&sb
, sb
.len
- strlen("config"));
495 strbuf_addstr(&sb
, "worktrees");
497 /* See if there is any file inside the worktrees directory. */
498 dir
= opendir(sb
.buf
);
504 d
= readdir_skip_dot_and_dotdot(dir
);
511 void strbuf_worktree_ref(const struct worktree
*wt
,
515 if (parse_worktree_ref(refname
, NULL
, NULL
, NULL
) ==
516 REF_WORKTREE_CURRENT
&&
517 wt
&& !wt
->is_current
) {
518 if (is_main_worktree(wt
))
519 strbuf_addstr(sb
, "main-worktree/");
521 strbuf_addf(sb
, "worktrees/%s/", wt
->id
);
523 strbuf_addstr(sb
, refname
);
526 int other_head_refs(each_ref_fn fn
, void *cb_data
)
528 struct worktree
**worktrees
, **p
;
529 struct strbuf refname
= STRBUF_INIT
;
532 worktrees
= get_worktrees();
533 for (p
= worktrees
; *p
; p
++) {
534 struct worktree
*wt
= *p
;
535 struct object_id oid
;
541 strbuf_reset(&refname
);
542 strbuf_worktree_ref(wt
, &refname
, "HEAD");
543 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
547 ret
= fn(refname
.buf
, &oid
, flag
, cb_data
);
551 free_worktrees(worktrees
);
552 strbuf_release(&refname
);
557 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
558 * pointing at <repo>/worktrees/<id>.
560 static void repair_gitfile(struct worktree
*wt
,
561 worktree_repair_fn fn
, void *cb_data
)
563 struct strbuf dotgit
= STRBUF_INIT
;
564 struct strbuf repo
= STRBUF_INIT
;
566 const char *repair
= NULL
;
569 /* missing worktree can't be repaired */
570 if (!file_exists(wt
->path
))
573 if (!is_directory(wt
->path
)) {
574 fn(1, wt
->path
, _("not a directory"), cb_data
);
578 strbuf_realpath(&repo
, git_common_path("worktrees/%s", wt
->id
), 1);
579 strbuf_addf(&dotgit
, "%s/.git", wt
->path
);
580 backlink
= xstrdup_or_null(read_gitfile_gently(dotgit
.buf
, &err
));
582 if (err
== READ_GITFILE_ERR_NOT_A_FILE
)
583 fn(1, wt
->path
, _(".git is not a file"), cb_data
);
585 repair
= _(".git file broken");
586 else if (fspathcmp(backlink
, repo
.buf
))
587 repair
= _(".git file incorrect");
590 fn(0, wt
->path
, repair
, cb_data
);
591 write_file(dotgit
.buf
, "gitdir: %s", repo
.buf
);
595 strbuf_release(&repo
);
596 strbuf_release(&dotgit
);
599 static void repair_noop(int iserr UNUSED
,
600 const char *path UNUSED
,
601 const char *msg UNUSED
,
602 void *cb_data UNUSED
)
607 void repair_worktrees(worktree_repair_fn fn
, void *cb_data
)
609 struct worktree
**worktrees
= get_worktrees_internal(1);
610 struct worktree
**wt
= worktrees
+ 1; /* +1 skips main worktree */
615 repair_gitfile(*wt
, fn
, cb_data
);
616 free_worktrees(worktrees
);
619 static int is_main_worktree_path(const char *path
)
621 struct strbuf target
= STRBUF_INIT
;
622 struct strbuf maindir
= STRBUF_INIT
;
625 strbuf_add_real_path(&target
, path
);
626 strbuf_strip_suffix(&target
, "/.git");
627 strbuf_add_real_path(&maindir
, get_git_common_dir());
628 strbuf_strip_suffix(&maindir
, "/.git");
629 cmp
= fspathcmp(maindir
.buf
, target
.buf
);
631 strbuf_release(&maindir
);
632 strbuf_release(&target
);
637 * If both the main worktree and linked worktree have been moved, then the
638 * gitfile /path/to/worktree/.git won't point into the repository, thus we
639 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
640 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
641 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
643 static char *infer_backlink(const char *gitfile
)
645 struct strbuf actual
= STRBUF_INIT
;
646 struct strbuf inferred
= STRBUF_INIT
;
649 if (strbuf_read_file(&actual
, gitfile
, 0) < 0)
651 if (!starts_with(actual
.buf
, "gitdir:"))
653 if (!(id
= find_last_dir_sep(actual
.buf
)))
655 strbuf_trim(&actual
);
656 id
++; /* advance past '/' to point at <id> */
659 strbuf_git_common_path(&inferred
, the_repository
, "worktrees/%s", id
);
660 if (!is_directory(inferred
.buf
))
663 strbuf_release(&actual
);
664 return strbuf_detach(&inferred
, NULL
);
667 strbuf_release(&actual
);
668 strbuf_release(&inferred
);
673 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
674 * the worktree's path.
676 void repair_worktree_at_path(const char *path
,
677 worktree_repair_fn fn
, void *cb_data
)
679 struct strbuf dotgit
= STRBUF_INIT
;
680 struct strbuf realdotgit
= STRBUF_INIT
;
681 struct strbuf gitdir
= STRBUF_INIT
;
682 struct strbuf olddotgit
= STRBUF_INIT
;
683 char *backlink
= NULL
;
684 const char *repair
= NULL
;
690 if (is_main_worktree_path(path
))
693 strbuf_addf(&dotgit
, "%s/.git", path
);
694 if (!strbuf_realpath(&realdotgit
, dotgit
.buf
, 0)) {
695 fn(1, path
, _("not a valid path"), cb_data
);
699 backlink
= xstrdup_or_null(read_gitfile_gently(realdotgit
.buf
, &err
));
700 if (err
== READ_GITFILE_ERR_NOT_A_FILE
) {
701 fn(1, realdotgit
.buf
, _("unable to locate repository; .git is not a file"), cb_data
);
703 } else if (err
== READ_GITFILE_ERR_NOT_A_REPO
) {
704 if (!(backlink
= infer_backlink(realdotgit
.buf
))) {
705 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file does not reference a repository"), cb_data
);
709 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file broken"), cb_data
);
713 strbuf_addf(&gitdir
, "%s/gitdir", backlink
);
714 if (strbuf_read_file(&olddotgit
, gitdir
.buf
, 0) < 0)
715 repair
= _("gitdir unreadable");
717 strbuf_rtrim(&olddotgit
);
718 if (fspathcmp(olddotgit
.buf
, realdotgit
.buf
))
719 repair
= _("gitdir incorrect");
723 fn(0, gitdir
.buf
, repair
, cb_data
);
724 write_file(gitdir
.buf
, "%s", realdotgit
.buf
);
728 strbuf_release(&olddotgit
);
729 strbuf_release(&gitdir
);
730 strbuf_release(&realdotgit
);
731 strbuf_release(&dotgit
);
734 int should_prune_worktree(const char *id
, struct strbuf
*reason
, char **wtpath
, timestamp_t expire
)
743 if (!is_directory(git_path("worktrees/%s", id
))) {
744 strbuf_addstr(reason
, _("not a valid directory"));
747 if (file_exists(git_path("worktrees/%s/locked", id
)))
749 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
750 strbuf_addstr(reason
, _("gitdir file does not exist"));
753 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
755 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
759 len
= xsize_t(st
.st_size
);
760 path
= xmallocz(len
);
762 read_result
= read_in_full(fd
, path
, len
);
763 if (read_result
< 0) {
764 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
772 if (read_result
!= len
) {
774 _("short read (expected %"PRIuMAX
" bytes, read %"PRIuMAX
")"),
775 (uintmax_t)len
, (uintmax_t)read_result
);
779 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
782 strbuf_addstr(reason
, _("invalid gitdir file"));
787 if (!file_exists(path
)) {
788 if (stat(git_path("worktrees/%s/index", id
), &st
) ||
789 st
.st_mtime
<= expire
) {
790 strbuf_addstr(reason
, _("gitdir file points to non-existent location"));
802 static int move_config_setting(const char *key
, const char *value
,
803 const char *from_file
, const char *to_file
)
805 if (git_config_set_in_file_gently(to_file
, key
, NULL
, value
))
806 return error(_("unable to set %s in '%s'"), key
, to_file
);
807 if (git_config_set_in_file_gently(from_file
, key
, NULL
, NULL
))
808 return error(_("unable to unset %s in '%s'"), key
, from_file
);
812 int init_worktree_config(struct repository
*r
)
816 struct config_set cs
= { { 0 } };
817 const char *core_worktree
;
818 char *common_config_file
;
819 char *main_worktree_file
;
822 * If the extension is already enabled, then we can skip the
825 if (r
->repository_format_worktree_config
)
827 if ((res
= git_config_set_gently("extensions.worktreeConfig", "true")))
828 return error(_("failed to set extensions.worktreeConfig setting"));
830 common_config_file
= xstrfmt("%s/config", r
->commondir
);
831 main_worktree_file
= xstrfmt("%s/config.worktree", r
->commondir
);
833 git_configset_init(&cs
);
834 git_configset_add_file(&cs
, common_config_file
);
837 * If core.bare is true in the common config file, then we need to
838 * move it to the main worktree's config file or it will break all
839 * worktrees. If it is false, then leave it in place because it
840 * _could_ be negating a global core.bare=true.
842 if (!git_configset_get_bool(&cs
, "core.bare", &bare
) && bare
) {
843 if ((res
= move_config_setting("core.bare", "true",
845 main_worktree_file
)))
849 * If core.worktree is set, then the main worktree is located
850 * somewhere different than the parent of the common Git dir.
851 * Relocate that value to avoid breaking all worktrees with this
852 * upgrade to worktree config.
854 if (!git_configset_get_value(&cs
, "core.worktree", &core_worktree
, NULL
)) {
855 if ((res
= move_config_setting("core.worktree", core_worktree
,
857 main_worktree_file
)))
862 * Ensure that we use worktree config for the remaining lifetime
863 * of the current process.
865 r
->repository_format_worktree_config
= 1;
868 git_configset_clear(&cs
);
869 free(common_config_file
);
870 free(main_worktree_file
);