2 #include "repository.h"
10 void free_worktrees(struct worktree
**worktrees
)
14 for (i
= 0; worktrees
[i
]; i
++) {
15 free(worktrees
[i
]->path
);
16 free(worktrees
[i
]->id
);
17 free(worktrees
[i
]->head_ref
);
18 free(worktrees
[i
]->lock_reason
);
19 free(worktrees
[i
]->prune_reason
);
26 * Update head_oid, head_ref and is_detached of the given worktree
28 static void add_head_info(struct worktree
*wt
)
33 target
= refs_resolve_ref_unsafe(get_worktree_ref_store(wt
),
36 &wt
->head_oid
, &flags
);
40 if (flags
& REF_ISSYMREF
)
41 wt
->head_ref
= xstrdup(target
);
47 * get the main worktree
49 static struct worktree
*get_main_worktree(void)
51 struct worktree
*worktree
= NULL
;
52 struct strbuf worktree_path
= STRBUF_INIT
;
54 strbuf_add_real_path(&worktree_path
, get_git_common_dir());
55 strbuf_strip_suffix(&worktree_path
, "/.git");
57 CALLOC_ARRAY(worktree
, 1);
58 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
60 * NEEDSWORK: If this function is called from a secondary worktree and
61 * config.worktree is present, is_bare_repository_cfg will reflect the
62 * contents of config.worktree, not the contents of the main worktree.
63 * This means that worktree->is_bare may be set to 0 even if the main
64 * worktree is configured to be bare.
66 worktree
->is_bare
= (is_bare_repository_cfg
== 1) ||
68 add_head_info(worktree
);
72 static struct worktree
*get_linked_worktree(const char *id
)
74 struct worktree
*worktree
= NULL
;
75 struct strbuf path
= STRBUF_INIT
;
76 struct strbuf worktree_path
= STRBUF_INIT
;
79 die("Missing linked worktree name");
81 strbuf_git_common_path(&path
, the_repository
, "worktrees/%s/gitdir", id
);
82 if (strbuf_read_file(&worktree_path
, path
.buf
, 0) <= 0)
83 /* invalid gitdir file */
85 strbuf_rtrim(&worktree_path
);
86 strbuf_strip_suffix(&worktree_path
, "/.git");
88 CALLOC_ARRAY(worktree
, 1);
89 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
90 worktree
->id
= xstrdup(id
);
91 add_head_info(worktree
);
94 strbuf_release(&path
);
95 strbuf_release(&worktree_path
);
99 static void mark_current_worktree(struct worktree
**worktrees
)
101 char *git_dir
= absolute_pathdup(get_git_dir());
104 for (i
= 0; worktrees
[i
]; i
++) {
105 struct worktree
*wt
= worktrees
[i
];
106 const char *wt_git_dir
= get_worktree_git_dir(wt
);
108 if (!fspathcmp(git_dir
, absolute_path(wt_git_dir
))) {
116 struct worktree
**get_worktrees(void)
118 struct worktree
**list
= NULL
;
119 struct strbuf path
= STRBUF_INIT
;
122 int counter
= 0, alloc
= 2;
124 ALLOC_ARRAY(list
, alloc
);
126 list
[counter
++] = get_main_worktree();
128 strbuf_addf(&path
, "%s/worktrees", get_git_common_dir());
129 dir
= opendir(path
.buf
);
130 strbuf_release(&path
);
132 while ((d
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
133 struct worktree
*linked
= NULL
;
135 if ((linked
= get_linked_worktree(d
->d_name
))) {
136 ALLOC_GROW(list
, counter
+ 1, alloc
);
137 list
[counter
++] = linked
;
142 ALLOC_GROW(list
, counter
+ 1, alloc
);
143 list
[counter
] = NULL
;
145 mark_current_worktree(list
);
149 const char *get_worktree_git_dir(const struct worktree
*wt
)
152 return get_git_dir();
154 return get_git_common_dir();
156 return git_common_path("worktrees/%s", wt
->id
);
159 static struct worktree
*find_worktree_by_suffix(struct worktree
**list
,
162 struct worktree
*found
= NULL
;
163 int nr_found
= 0, suffixlen
;
165 suffixlen
= strlen(suffix
);
169 for (; *list
&& nr_found
< 2; list
++) {
170 const char *path
= (*list
)->path
;
171 int pathlen
= strlen(path
);
172 int start
= pathlen
- suffixlen
;
174 /* suffix must start at directory boundary */
175 if ((!start
|| (start
> 0 && is_dir_sep(path
[start
- 1]))) &&
176 !fspathcmp(suffix
, path
+ start
)) {
181 return nr_found
== 1 ? found
: NULL
;
184 struct worktree
*find_worktree(struct worktree
**list
,
189 char *to_free
= NULL
;
191 if ((wt
= find_worktree_by_suffix(list
, arg
)))
195 arg
= to_free
= prefix_filename(prefix
, arg
);
196 wt
= find_worktree_by_path(list
, arg
);
201 struct worktree
*find_worktree_by_path(struct worktree
**list
, const char *p
)
203 struct strbuf wt_path
= STRBUF_INIT
;
204 char *path
= real_pathdup(p
, 0);
208 for (; *list
; list
++) {
209 if (!strbuf_realpath(&wt_path
, (*list
)->path
, 0))
212 if (!fspathcmp(path
, wt_path
.buf
))
216 strbuf_release(&wt_path
);
220 int is_main_worktree(const struct worktree
*wt
)
225 const char *worktree_lock_reason(struct worktree
*wt
)
227 if (is_main_worktree(wt
))
230 if (!wt
->lock_reason_valid
) {
231 struct strbuf path
= STRBUF_INIT
;
233 strbuf_addstr(&path
, worktree_git_path(wt
, "locked"));
234 if (file_exists(path
.buf
)) {
235 struct strbuf lock_reason
= STRBUF_INIT
;
236 if (strbuf_read_file(&lock_reason
, path
.buf
, 0) < 0)
237 die_errno(_("failed to read '%s'"), path
.buf
);
238 strbuf_trim(&lock_reason
);
239 wt
->lock_reason
= strbuf_detach(&lock_reason
, NULL
);
241 wt
->lock_reason
= NULL
;
242 wt
->lock_reason_valid
= 1;
243 strbuf_release(&path
);
246 return wt
->lock_reason
;
249 const char *worktree_prune_reason(struct worktree
*wt
, timestamp_t expire
)
251 struct strbuf reason
= STRBUF_INIT
;
254 if (is_main_worktree(wt
))
256 if (wt
->prune_reason_valid
)
257 return wt
->prune_reason
;
259 if (should_prune_worktree(wt
->id
, &reason
, &path
, expire
))
260 wt
->prune_reason
= strbuf_detach(&reason
, NULL
);
261 wt
->prune_reason_valid
= 1;
263 strbuf_release(&reason
);
265 return wt
->prune_reason
;
268 /* convenient wrapper to deal with NULL strbuf */
269 __attribute__((format (printf
, 2, 3)))
270 static void strbuf_addf_gently(struct strbuf
*buf
, const char *fmt
, ...)
277 va_start(params
, fmt
);
278 strbuf_vaddf(buf
, fmt
, params
);
282 int validate_worktree(const struct worktree
*wt
, struct strbuf
*errmsg
,
285 struct strbuf wt_path
= STRBUF_INIT
;
286 struct strbuf realpath
= STRBUF_INIT
;
290 strbuf_addf(&wt_path
, "%s/.git", wt
->path
);
292 if (is_main_worktree(wt
)) {
293 if (is_directory(wt_path
.buf
)) {
298 * Main worktree using .git file to point to the
299 * repository would make it impossible to know where
300 * the actual worktree is if this function is executed
301 * from another worktree. No .git file support for now.
303 strbuf_addf_gently(errmsg
,
304 _("'%s' at main working tree is not the repository directory"),
310 * Make sure "gitdir" file points to a real .git file and that
311 * file points back here.
313 if (!is_absolute_path(wt
->path
)) {
314 strbuf_addf_gently(errmsg
,
315 _("'%s' file does not contain absolute path to the working tree location"),
316 git_common_path("worktrees/%s/gitdir", wt
->id
));
320 if (flags
& WT_VALIDATE_WORKTREE_MISSING_OK
&&
321 !file_exists(wt
->path
)) {
326 if (!file_exists(wt_path
.buf
)) {
327 strbuf_addf_gently(errmsg
, _("'%s' does not exist"), wt_path
.buf
);
331 path
= xstrdup_or_null(read_gitfile_gently(wt_path
.buf
, &err
));
333 strbuf_addf_gently(errmsg
, _("'%s' is not a .git file, error code %d"),
338 strbuf_realpath(&realpath
, git_common_path("worktrees/%s", wt
->id
), 1);
339 ret
= fspathcmp(path
, realpath
.buf
);
342 strbuf_addf_gently(errmsg
, _("'%s' does not point back to '%s'"),
343 wt
->path
, git_common_path("worktrees/%s", wt
->id
));
346 strbuf_release(&wt_path
);
347 strbuf_release(&realpath
);
351 void update_worktree_location(struct worktree
*wt
, const char *path_
)
353 struct strbuf path
= STRBUF_INIT
;
355 if (is_main_worktree(wt
))
356 BUG("can't relocate main worktree");
358 strbuf_realpath(&path
, path_
, 1);
359 if (fspathcmp(wt
->path
, path
.buf
)) {
360 write_file(git_common_path("worktrees/%s/gitdir", wt
->id
),
361 "%s/.git", path
.buf
);
363 wt
->path
= strbuf_detach(&path
, NULL
);
365 strbuf_release(&path
);
368 int is_worktree_being_rebased(const struct worktree
*wt
,
371 struct wt_status_state state
;
374 memset(&state
, 0, sizeof(state
));
375 found_rebase
= wt_status_check_rebase(wt
, &state
) &&
376 (state
.rebase_in_progress
||
377 state
.rebase_interactive_in_progress
) &&
379 skip_prefix(target
, "refs/heads/", &target
) &&
380 !strcmp(state
.branch
, target
);
381 wt_status_state_free_buffers(&state
);
385 int is_worktree_being_bisected(const struct worktree
*wt
,
388 struct wt_status_state state
;
391 memset(&state
, 0, sizeof(state
));
392 found_bisect
= wt_status_check_bisect(wt
, &state
) &&
394 skip_prefix(target
, "refs/heads/", &target
) &&
395 !strcmp(state
.branch
, target
);
396 wt_status_state_free_buffers(&state
);
401 * note: this function should be able to detect shared symref even if
402 * HEAD is temporarily detached (e.g. in the middle of rebase or
403 * bisect). New commands that do similar things should update this
406 const struct worktree
*find_shared_symref(struct worktree
**worktrees
,
410 const struct worktree
*existing
= NULL
;
413 for (i
= 0; worktrees
[i
]; i
++) {
414 struct worktree
*wt
= worktrees
[i
];
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
)) {
427 if (is_worktree_being_bisected(wt
, target
)) {
433 refs
= get_worktree_ref_store(wt
);
434 symref_target
= refs_resolve_ref_unsafe(refs
, symref
, 0,
436 if ((flags
& REF_ISSYMREF
) &&
437 symref_target
&& !strcmp(symref_target
, target
)) {
446 int submodule_uses_worktrees(const char *path
)
448 char *submodule_gitdir
;
449 struct strbuf sb
= STRBUF_INIT
, err
= STRBUF_INIT
;
453 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
455 submodule_gitdir
= git_pathdup_submodule(path
, "%s", "");
456 if (!submodule_gitdir
)
459 /* The env would be set for the superproject. */
460 get_common_dir_noenv(&sb
, submodule_gitdir
);
461 free(submodule_gitdir
);
463 strbuf_addstr(&sb
, "/config");
464 read_repository_format(&format
, sb
.buf
);
465 if (verify_repository_format(&format
, &err
)) {
466 strbuf_release(&err
);
468 clear_repository_format(&format
);
471 clear_repository_format(&format
);
472 strbuf_release(&err
);
474 /* Replace config by worktrees. */
475 strbuf_setlen(&sb
, sb
.len
- strlen("config"));
476 strbuf_addstr(&sb
, "worktrees");
478 /* See if there is any file inside the worktrees directory. */
479 dir
= opendir(sb
.buf
);
485 d
= readdir_skip_dot_and_dotdot(dir
);
492 int parse_worktree_ref(const char *worktree_ref
, const char **name
,
493 int *name_length
, const char **ref
)
495 if (skip_prefix(worktree_ref
, "main-worktree/", &worktree_ref
)) {
506 if (skip_prefix(worktree_ref
, "worktrees/", &worktree_ref
)) {
507 const char *slash
= strchr(worktree_ref
, '/');
509 if (!slash
|| slash
== worktree_ref
|| !slash
[1])
512 *name
= worktree_ref
;
514 *name_length
= slash
- worktree_ref
;
522 void strbuf_worktree_ref(const struct worktree
*wt
,
526 switch (ref_type(refname
)) {
527 case REF_TYPE_PSEUDOREF
:
528 case REF_TYPE_PER_WORKTREE
:
529 if (wt
&& !wt
->is_current
) {
530 if (is_main_worktree(wt
))
531 strbuf_addstr(sb
, "main-worktree/");
533 strbuf_addf(sb
, "worktrees/%s/", wt
->id
);
537 case REF_TYPE_MAIN_PSEUDOREF
:
538 case REF_TYPE_OTHER_PSEUDOREF
:
541 case REF_TYPE_NORMAL
:
543 * For shared refs, don't prefix worktrees/ or
544 * main-worktree/. It's not necessary and
545 * files-backend.c can't handle it anyway.
549 strbuf_addstr(sb
, refname
);
552 int other_head_refs(each_ref_fn fn
, void *cb_data
)
554 struct worktree
**worktrees
, **p
;
555 struct strbuf refname
= STRBUF_INIT
;
558 worktrees
= get_worktrees();
559 for (p
= worktrees
; *p
; p
++) {
560 struct worktree
*wt
= *p
;
561 struct object_id oid
;
567 strbuf_reset(&refname
);
568 strbuf_worktree_ref(wt
, &refname
, "HEAD");
569 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
573 ret
= fn(refname
.buf
, &oid
, flag
, cb_data
);
577 free_worktrees(worktrees
);
578 strbuf_release(&refname
);
583 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
584 * pointing at <repo>/worktrees/<id>.
586 static void repair_gitfile(struct worktree
*wt
,
587 worktree_repair_fn fn
, void *cb_data
)
589 struct strbuf dotgit
= STRBUF_INIT
;
590 struct strbuf repo
= STRBUF_INIT
;
592 const char *repair
= NULL
;
595 /* missing worktree can't be repaired */
596 if (!file_exists(wt
->path
))
599 if (!is_directory(wt
->path
)) {
600 fn(1, wt
->path
, _("not a directory"), cb_data
);
604 strbuf_realpath(&repo
, git_common_path("worktrees/%s", wt
->id
), 1);
605 strbuf_addf(&dotgit
, "%s/.git", wt
->path
);
606 backlink
= xstrdup_or_null(read_gitfile_gently(dotgit
.buf
, &err
));
608 if (err
== READ_GITFILE_ERR_NOT_A_FILE
)
609 fn(1, wt
->path
, _(".git is not a file"), cb_data
);
611 repair
= _(".git file broken");
612 else if (fspathcmp(backlink
, repo
.buf
))
613 repair
= _(".git file incorrect");
616 fn(0, wt
->path
, repair
, cb_data
);
617 write_file(dotgit
.buf
, "gitdir: %s", repo
.buf
);
621 strbuf_release(&repo
);
622 strbuf_release(&dotgit
);
625 static void repair_noop(int iserr
, const char *path
, const char *msg
,
631 void repair_worktrees(worktree_repair_fn fn
, void *cb_data
)
633 struct worktree
**worktrees
= get_worktrees();
634 struct worktree
**wt
= worktrees
+ 1; /* +1 skips main worktree */
639 repair_gitfile(*wt
, fn
, cb_data
);
640 free_worktrees(worktrees
);
643 static int is_main_worktree_path(const char *path
)
645 struct strbuf target
= STRBUF_INIT
;
646 struct strbuf maindir
= STRBUF_INIT
;
649 strbuf_add_real_path(&target
, path
);
650 strbuf_strip_suffix(&target
, "/.git");
651 strbuf_add_real_path(&maindir
, get_git_common_dir());
652 strbuf_strip_suffix(&maindir
, "/.git");
653 cmp
= fspathcmp(maindir
.buf
, target
.buf
);
655 strbuf_release(&maindir
);
656 strbuf_release(&target
);
661 * If both the main worktree and linked worktree have been moved, then the
662 * gitfile /path/to/worktree/.git won't point into the repository, thus we
663 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
664 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
665 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
667 static char *infer_backlink(const char *gitfile
)
669 struct strbuf actual
= STRBUF_INIT
;
670 struct strbuf inferred
= STRBUF_INIT
;
673 if (strbuf_read_file(&actual
, gitfile
, 0) < 0)
675 if (!starts_with(actual
.buf
, "gitdir:"))
677 if (!(id
= find_last_dir_sep(actual
.buf
)))
679 strbuf_trim(&actual
);
680 id
++; /* advance past '/' to point at <id> */
683 strbuf_git_common_path(&inferred
, the_repository
, "worktrees/%s", id
);
684 if (!is_directory(inferred
.buf
))
687 strbuf_release(&actual
);
688 return strbuf_detach(&inferred
, NULL
);
691 strbuf_release(&actual
);
692 strbuf_release(&inferred
);
697 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
698 * the worktree's path.
700 void repair_worktree_at_path(const char *path
,
701 worktree_repair_fn fn
, void *cb_data
)
703 struct strbuf dotgit
= STRBUF_INIT
;
704 struct strbuf realdotgit
= STRBUF_INIT
;
705 struct strbuf gitdir
= STRBUF_INIT
;
706 struct strbuf olddotgit
= STRBUF_INIT
;
707 char *backlink
= NULL
;
708 const char *repair
= NULL
;
714 if (is_main_worktree_path(path
))
717 strbuf_addf(&dotgit
, "%s/.git", path
);
718 if (!strbuf_realpath(&realdotgit
, dotgit
.buf
, 0)) {
719 fn(1, path
, _("not a valid path"), cb_data
);
723 backlink
= xstrdup_or_null(read_gitfile_gently(realdotgit
.buf
, &err
));
724 if (err
== READ_GITFILE_ERR_NOT_A_FILE
) {
725 fn(1, realdotgit
.buf
, _("unable to locate repository; .git is not a file"), cb_data
);
727 } else if (err
== READ_GITFILE_ERR_NOT_A_REPO
) {
728 if (!(backlink
= infer_backlink(realdotgit
.buf
))) {
729 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file does not reference a repository"), cb_data
);
733 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file broken"), cb_data
);
737 strbuf_addf(&gitdir
, "%s/gitdir", backlink
);
738 if (strbuf_read_file(&olddotgit
, gitdir
.buf
, 0) < 0)
739 repair
= _("gitdir unreadable");
741 strbuf_rtrim(&olddotgit
);
742 if (fspathcmp(olddotgit
.buf
, realdotgit
.buf
))
743 repair
= _("gitdir incorrect");
747 fn(0, gitdir
.buf
, repair
, cb_data
);
748 write_file(gitdir
.buf
, "%s", realdotgit
.buf
);
752 strbuf_release(&olddotgit
);
753 strbuf_release(&gitdir
);
754 strbuf_release(&realdotgit
);
755 strbuf_release(&dotgit
);
758 int should_prune_worktree(const char *id
, struct strbuf
*reason
, char **wtpath
, timestamp_t expire
)
767 if (!is_directory(git_path("worktrees/%s", id
))) {
768 strbuf_addstr(reason
, _("not a valid directory"));
771 if (file_exists(git_path("worktrees/%s/locked", id
)))
773 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
774 strbuf_addstr(reason
, _("gitdir file does not exist"));
777 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
779 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
783 len
= xsize_t(st
.st_size
);
784 path
= xmallocz(len
);
786 read_result
= read_in_full(fd
, path
, len
);
787 if (read_result
< 0) {
788 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
796 if (read_result
!= len
) {
798 _("short read (expected %"PRIuMAX
" bytes, read %"PRIuMAX
")"),
799 (uintmax_t)len
, (uintmax_t)read_result
);
803 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
806 strbuf_addstr(reason
, _("invalid gitdir file"));
811 if (!file_exists(path
)) {
812 if (stat(git_path("worktrees/%s/index", id
), &st
) ||
813 st
.st_mtime
<= expire
) {
814 strbuf_addstr(reason
, _("gitdir file points to non-existent location"));
826 static int move_config_setting(const char *key
, const char *value
,
827 const char *from_file
, const char *to_file
)
829 if (git_config_set_in_file_gently(to_file
, key
, value
))
830 return error(_("unable to set %s in '%s'"), key
, to_file
);
831 if (git_config_set_in_file_gently(from_file
, key
, NULL
))
832 return error(_("unable to unset %s in '%s'"), key
, from_file
);
836 int init_worktree_config(struct repository
*r
)
840 struct config_set cs
= { { 0 } };
841 const char *core_worktree
;
842 char *common_config_file
;
843 char *main_worktree_file
;
846 * If the extension is already enabled, then we can skip the
849 if (repository_format_worktree_config
)
851 if ((res
= git_config_set_gently("extensions.worktreeConfig", "true")))
852 return error(_("failed to set extensions.worktreeConfig setting"));
854 common_config_file
= xstrfmt("%s/config", r
->commondir
);
855 main_worktree_file
= xstrfmt("%s/config.worktree", r
->commondir
);
857 git_configset_init(&cs
);
858 git_configset_add_file(&cs
, common_config_file
);
861 * If core.bare is true in the common config file, then we need to
862 * move it to the main worktree's config file or it will break all
863 * worktrees. If it is false, then leave it in place because it
864 * _could_ be negating a global core.bare=true.
866 if (!git_configset_get_bool(&cs
, "core.bare", &bare
) && bare
) {
867 if ((res
= move_config_setting("core.bare", "true",
869 main_worktree_file
)))
873 * If core.worktree is set, then the main worktree is located
874 * somewhere different than the parent of the common Git dir.
875 * Relocate that value to avoid breaking all worktrees with this
876 * upgrade to worktree config.
878 if (!git_configset_get_value(&cs
, "core.worktree", &core_worktree
)) {
879 if ((res
= move_config_setting("core.worktree", core_worktree
,
881 main_worktree_file
)))
886 * Ensure that we use worktree config for the remaining lifetime
887 * of the current process.
889 repository_format_worktree_config
= 1;
892 git_configset_clear(&cs
);
893 free(common_config_file
);
894 free(main_worktree_file
);