1 #include "git-compat-util.h"
3 #include "environment.h"
6 #include "repository.h"
12 #include "wt-status.h"
15 void free_worktrees(struct worktree
**worktrees
)
19 for (i
= 0; worktrees
[i
]; i
++) {
20 free(worktrees
[i
]->path
);
21 free(worktrees
[i
]->id
);
22 free(worktrees
[i
]->head_ref
);
23 free(worktrees
[i
]->lock_reason
);
24 free(worktrees
[i
]->prune_reason
);
31 * Update head_oid, head_ref and is_detached of the given worktree
33 static void add_head_info(struct worktree
*wt
)
38 target
= refs_resolve_ref_unsafe(get_worktree_ref_store(wt
),
41 &wt
->head_oid
, &flags
);
45 if (flags
& REF_ISSYMREF
)
46 wt
->head_ref
= xstrdup(target
);
52 * get the main worktree
54 static struct worktree
*get_main_worktree(void)
56 struct worktree
*worktree
= NULL
;
57 struct strbuf worktree_path
= STRBUF_INIT
;
59 strbuf_add_real_path(&worktree_path
, get_git_common_dir());
60 strbuf_strip_suffix(&worktree_path
, "/.git");
62 CALLOC_ARRAY(worktree
, 1);
63 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
65 * NEEDSWORK: If this function is called from a secondary worktree and
66 * config.worktree is present, is_bare_repository_cfg will reflect the
67 * contents of config.worktree, not the contents of the main worktree.
68 * This means that worktree->is_bare may be set to 0 even if the main
69 * worktree is configured to be bare.
71 worktree
->is_bare
= (is_bare_repository_cfg
== 1) ||
73 add_head_info(worktree
);
77 static struct worktree
*get_linked_worktree(const char *id
)
79 struct worktree
*worktree
= NULL
;
80 struct strbuf path
= STRBUF_INIT
;
81 struct strbuf worktree_path
= STRBUF_INIT
;
84 die("Missing linked worktree name");
86 strbuf_git_common_path(&path
, the_repository
, "worktrees/%s/gitdir", id
);
87 if (strbuf_read_file(&worktree_path
, path
.buf
, 0) <= 0)
88 /* invalid gitdir file */
90 strbuf_rtrim(&worktree_path
);
91 strbuf_strip_suffix(&worktree_path
, "/.git");
93 CALLOC_ARRAY(worktree
, 1);
94 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
95 worktree
->id
= xstrdup(id
);
96 add_head_info(worktree
);
99 strbuf_release(&path
);
100 strbuf_release(&worktree_path
);
104 static void mark_current_worktree(struct worktree
**worktrees
)
106 char *git_dir
= absolute_pathdup(get_git_dir());
109 for (i
= 0; worktrees
[i
]; i
++) {
110 struct worktree
*wt
= worktrees
[i
];
111 const char *wt_git_dir
= get_worktree_git_dir(wt
);
113 if (!fspathcmp(git_dir
, absolute_path(wt_git_dir
))) {
121 struct worktree
**get_worktrees(void)
123 struct worktree
**list
= NULL
;
124 struct strbuf path
= STRBUF_INIT
;
127 int counter
= 0, alloc
= 2;
129 ALLOC_ARRAY(list
, alloc
);
131 list
[counter
++] = get_main_worktree();
133 strbuf_addf(&path
, "%s/worktrees", get_git_common_dir());
134 dir
= opendir(path
.buf
);
135 strbuf_release(&path
);
137 while ((d
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
138 struct worktree
*linked
= NULL
;
140 if ((linked
= get_linked_worktree(d
->d_name
))) {
141 ALLOC_GROW(list
, counter
+ 1, alloc
);
142 list
[counter
++] = linked
;
147 ALLOC_GROW(list
, counter
+ 1, alloc
);
148 list
[counter
] = NULL
;
150 mark_current_worktree(list
);
154 const char *get_worktree_git_dir(const struct worktree
*wt
)
157 return get_git_dir();
159 return get_git_common_dir();
161 return git_common_path("worktrees/%s", wt
->id
);
164 static struct worktree
*find_worktree_by_suffix(struct worktree
**list
,
167 struct worktree
*found
= NULL
;
168 int nr_found
= 0, suffixlen
;
170 suffixlen
= strlen(suffix
);
174 for (; *list
&& nr_found
< 2; list
++) {
175 const char *path
= (*list
)->path
;
176 int pathlen
= strlen(path
);
177 int start
= pathlen
- suffixlen
;
179 /* suffix must start at directory boundary */
180 if ((!start
|| (start
> 0 && is_dir_sep(path
[start
- 1]))) &&
181 !fspathcmp(suffix
, path
+ start
)) {
186 return nr_found
== 1 ? found
: NULL
;
189 struct worktree
*find_worktree(struct worktree
**list
,
194 char *to_free
= NULL
;
196 if ((wt
= find_worktree_by_suffix(list
, arg
)))
200 arg
= to_free
= prefix_filename(prefix
, arg
);
201 wt
= find_worktree_by_path(list
, arg
);
206 struct worktree
*find_worktree_by_path(struct worktree
**list
, const char *p
)
208 struct strbuf wt_path
= STRBUF_INIT
;
209 char *path
= real_pathdup(p
, 0);
213 for (; *list
; list
++) {
214 if (!strbuf_realpath(&wt_path
, (*list
)->path
, 0))
217 if (!fspathcmp(path
, wt_path
.buf
))
221 strbuf_release(&wt_path
);
225 int is_main_worktree(const struct worktree
*wt
)
230 const char *worktree_lock_reason(struct worktree
*wt
)
232 if (is_main_worktree(wt
))
235 if (!wt
->lock_reason_valid
) {
236 struct strbuf path
= STRBUF_INIT
;
238 strbuf_addstr(&path
, worktree_git_path(wt
, "locked"));
239 if (file_exists(path
.buf
)) {
240 struct strbuf lock_reason
= STRBUF_INIT
;
241 if (strbuf_read_file(&lock_reason
, path
.buf
, 0) < 0)
242 die_errno(_("failed to read '%s'"), path
.buf
);
243 strbuf_trim(&lock_reason
);
244 wt
->lock_reason
= strbuf_detach(&lock_reason
, NULL
);
246 wt
->lock_reason
= NULL
;
247 wt
->lock_reason_valid
= 1;
248 strbuf_release(&path
);
251 return wt
->lock_reason
;
254 const char *worktree_prune_reason(struct worktree
*wt
, timestamp_t expire
)
256 struct strbuf reason
= STRBUF_INIT
;
259 if (is_main_worktree(wt
))
261 if (wt
->prune_reason_valid
)
262 return wt
->prune_reason
;
264 if (should_prune_worktree(wt
->id
, &reason
, &path
, expire
))
265 wt
->prune_reason
= strbuf_detach(&reason
, NULL
);
266 wt
->prune_reason_valid
= 1;
268 strbuf_release(&reason
);
270 return wt
->prune_reason
;
273 /* convenient wrapper to deal with NULL strbuf */
274 __attribute__((format (printf
, 2, 3)))
275 static void strbuf_addf_gently(struct strbuf
*buf
, const char *fmt
, ...)
282 va_start(params
, fmt
);
283 strbuf_vaddf(buf
, fmt
, params
);
287 int validate_worktree(const struct worktree
*wt
, struct strbuf
*errmsg
,
290 struct strbuf wt_path
= STRBUF_INIT
;
291 struct strbuf realpath
= STRBUF_INIT
;
295 strbuf_addf(&wt_path
, "%s/.git", wt
->path
);
297 if (is_main_worktree(wt
)) {
298 if (is_directory(wt_path
.buf
)) {
303 * Main worktree using .git file to point to the
304 * repository would make it impossible to know where
305 * the actual worktree is if this function is executed
306 * from another worktree. No .git file support for now.
308 strbuf_addf_gently(errmsg
,
309 _("'%s' at main working tree is not the repository directory"),
315 * Make sure "gitdir" file points to a real .git file and that
316 * file points back here.
318 if (!is_absolute_path(wt
->path
)) {
319 strbuf_addf_gently(errmsg
,
320 _("'%s' file does not contain absolute path to the working tree location"),
321 git_common_path("worktrees/%s/gitdir", wt
->id
));
325 if (flags
& WT_VALIDATE_WORKTREE_MISSING_OK
&&
326 !file_exists(wt
->path
)) {
331 if (!file_exists(wt_path
.buf
)) {
332 strbuf_addf_gently(errmsg
, _("'%s' does not exist"), wt_path
.buf
);
336 path
= xstrdup_or_null(read_gitfile_gently(wt_path
.buf
, &err
));
338 strbuf_addf_gently(errmsg
, _("'%s' is not a .git file, error code %d"),
343 strbuf_realpath(&realpath
, git_common_path("worktrees/%s", wt
->id
), 1);
344 ret
= fspathcmp(path
, realpath
.buf
);
347 strbuf_addf_gently(errmsg
, _("'%s' does not point back to '%s'"),
348 wt
->path
, git_common_path("worktrees/%s", wt
->id
));
351 strbuf_release(&wt_path
);
352 strbuf_release(&realpath
);
356 void update_worktree_location(struct worktree
*wt
, const char *path_
)
358 struct strbuf path
= STRBUF_INIT
;
360 if (is_main_worktree(wt
))
361 BUG("can't relocate main worktree");
363 strbuf_realpath(&path
, path_
, 1);
364 if (fspathcmp(wt
->path
, path
.buf
)) {
365 write_file(git_common_path("worktrees/%s/gitdir", wt
->id
),
366 "%s/.git", path
.buf
);
368 wt
->path
= strbuf_detach(&path
, NULL
);
370 strbuf_release(&path
);
373 int is_worktree_being_rebased(const struct worktree
*wt
,
376 struct wt_status_state state
;
379 memset(&state
, 0, sizeof(state
));
380 found_rebase
= wt_status_check_rebase(wt
, &state
) &&
381 (state
.rebase_in_progress
||
382 state
.rebase_interactive_in_progress
) &&
384 skip_prefix(target
, "refs/heads/", &target
) &&
385 !strcmp(state
.branch
, target
);
386 wt_status_state_free_buffers(&state
);
390 int is_worktree_being_bisected(const struct worktree
*wt
,
393 struct wt_status_state state
;
396 memset(&state
, 0, sizeof(state
));
397 found_bisect
= wt_status_check_bisect(wt
, &state
) &&
399 skip_prefix(target
, "refs/heads/", &target
) &&
400 !strcmp(state
.branch
, target
);
401 wt_status_state_free_buffers(&state
);
406 * note: this function should be able to detect shared symref even if
407 * HEAD is temporarily detached (e.g. in the middle of rebase or
408 * bisect). New commands that do similar things should update this
411 int is_shared_symref(const struct worktree
*wt
, const char *symref
,
414 const char *symref_target
;
415 struct ref_store
*refs
;
421 if (wt
->is_detached
&& !strcmp(symref
, "HEAD")) {
422 if (is_worktree_being_rebased(wt
, target
))
424 if (is_worktree_being_bisected(wt
, target
))
428 refs
= get_worktree_ref_store(wt
);
429 symref_target
= refs_resolve_ref_unsafe(refs
, symref
, 0,
431 if ((flags
& REF_ISSYMREF
) &&
432 symref_target
&& !strcmp(symref_target
, target
))
438 const struct worktree
*find_shared_symref(struct worktree
**worktrees
,
443 for (int i
= 0; worktrees
[i
]; i
++)
444 if (is_shared_symref(worktrees
[i
], symref
, target
))
450 int submodule_uses_worktrees(const char *path
)
452 char *submodule_gitdir
;
453 struct strbuf sb
= STRBUF_INIT
, err
= STRBUF_INIT
;
457 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
459 submodule_gitdir
= git_pathdup_submodule(path
, "%s", "");
460 if (!submodule_gitdir
)
463 /* The env would be set for the superproject. */
464 get_common_dir_noenv(&sb
, submodule_gitdir
);
465 free(submodule_gitdir
);
467 strbuf_addstr(&sb
, "/config");
468 read_repository_format(&format
, sb
.buf
);
469 if (verify_repository_format(&format
, &err
)) {
470 strbuf_release(&err
);
472 clear_repository_format(&format
);
475 clear_repository_format(&format
);
476 strbuf_release(&err
);
478 /* Replace config by worktrees. */
479 strbuf_setlen(&sb
, sb
.len
- strlen("config"));
480 strbuf_addstr(&sb
, "worktrees");
482 /* See if there is any file inside the worktrees directory. */
483 dir
= opendir(sb
.buf
);
489 d
= readdir_skip_dot_and_dotdot(dir
);
496 void strbuf_worktree_ref(const struct worktree
*wt
,
500 if (parse_worktree_ref(refname
, NULL
, NULL
, NULL
) ==
501 REF_WORKTREE_CURRENT
&&
502 wt
&& !wt
->is_current
) {
503 if (is_main_worktree(wt
))
504 strbuf_addstr(sb
, "main-worktree/");
506 strbuf_addf(sb
, "worktrees/%s/", wt
->id
);
508 strbuf_addstr(sb
, refname
);
511 int other_head_refs(each_ref_fn fn
, void *cb_data
)
513 struct worktree
**worktrees
, **p
;
514 struct strbuf refname
= STRBUF_INIT
;
517 worktrees
= get_worktrees();
518 for (p
= worktrees
; *p
; p
++) {
519 struct worktree
*wt
= *p
;
520 struct object_id oid
;
526 strbuf_reset(&refname
);
527 strbuf_worktree_ref(wt
, &refname
, "HEAD");
528 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
532 ret
= fn(refname
.buf
, &oid
, flag
, cb_data
);
536 free_worktrees(worktrees
);
537 strbuf_release(&refname
);
542 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
543 * pointing at <repo>/worktrees/<id>.
545 static void repair_gitfile(struct worktree
*wt
,
546 worktree_repair_fn fn
, void *cb_data
)
548 struct strbuf dotgit
= STRBUF_INIT
;
549 struct strbuf repo
= STRBUF_INIT
;
551 const char *repair
= NULL
;
554 /* missing worktree can't be repaired */
555 if (!file_exists(wt
->path
))
558 if (!is_directory(wt
->path
)) {
559 fn(1, wt
->path
, _("not a directory"), cb_data
);
563 strbuf_realpath(&repo
, git_common_path("worktrees/%s", wt
->id
), 1);
564 strbuf_addf(&dotgit
, "%s/.git", wt
->path
);
565 backlink
= xstrdup_or_null(read_gitfile_gently(dotgit
.buf
, &err
));
567 if (err
== READ_GITFILE_ERR_NOT_A_FILE
)
568 fn(1, wt
->path
, _(".git is not a file"), cb_data
);
570 repair
= _(".git file broken");
571 else if (fspathcmp(backlink
, repo
.buf
))
572 repair
= _(".git file incorrect");
575 fn(0, wt
->path
, repair
, cb_data
);
576 write_file(dotgit
.buf
, "gitdir: %s", repo
.buf
);
580 strbuf_release(&repo
);
581 strbuf_release(&dotgit
);
584 static void repair_noop(int iserr
, const char *path
, const char *msg
,
590 void repair_worktrees(worktree_repair_fn fn
, void *cb_data
)
592 struct worktree
**worktrees
= get_worktrees();
593 struct worktree
**wt
= worktrees
+ 1; /* +1 skips main worktree */
598 repair_gitfile(*wt
, fn
, cb_data
);
599 free_worktrees(worktrees
);
602 static int is_main_worktree_path(const char *path
)
604 struct strbuf target
= STRBUF_INIT
;
605 struct strbuf maindir
= STRBUF_INIT
;
608 strbuf_add_real_path(&target
, path
);
609 strbuf_strip_suffix(&target
, "/.git");
610 strbuf_add_real_path(&maindir
, get_git_common_dir());
611 strbuf_strip_suffix(&maindir
, "/.git");
612 cmp
= fspathcmp(maindir
.buf
, target
.buf
);
614 strbuf_release(&maindir
);
615 strbuf_release(&target
);
620 * If both the main worktree and linked worktree have been moved, then the
621 * gitfile /path/to/worktree/.git won't point into the repository, thus we
622 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
623 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
624 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
626 static char *infer_backlink(const char *gitfile
)
628 struct strbuf actual
= STRBUF_INIT
;
629 struct strbuf inferred
= STRBUF_INIT
;
632 if (strbuf_read_file(&actual
, gitfile
, 0) < 0)
634 if (!starts_with(actual
.buf
, "gitdir:"))
636 if (!(id
= find_last_dir_sep(actual
.buf
)))
638 strbuf_trim(&actual
);
639 id
++; /* advance past '/' to point at <id> */
642 strbuf_git_common_path(&inferred
, the_repository
, "worktrees/%s", id
);
643 if (!is_directory(inferred
.buf
))
646 strbuf_release(&actual
);
647 return strbuf_detach(&inferred
, NULL
);
650 strbuf_release(&actual
);
651 strbuf_release(&inferred
);
656 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
657 * the worktree's path.
659 void repair_worktree_at_path(const char *path
,
660 worktree_repair_fn fn
, void *cb_data
)
662 struct strbuf dotgit
= STRBUF_INIT
;
663 struct strbuf realdotgit
= STRBUF_INIT
;
664 struct strbuf gitdir
= STRBUF_INIT
;
665 struct strbuf olddotgit
= STRBUF_INIT
;
666 char *backlink
= NULL
;
667 const char *repair
= NULL
;
673 if (is_main_worktree_path(path
))
676 strbuf_addf(&dotgit
, "%s/.git", path
);
677 if (!strbuf_realpath(&realdotgit
, dotgit
.buf
, 0)) {
678 fn(1, path
, _("not a valid path"), cb_data
);
682 backlink
= xstrdup_or_null(read_gitfile_gently(realdotgit
.buf
, &err
));
683 if (err
== READ_GITFILE_ERR_NOT_A_FILE
) {
684 fn(1, realdotgit
.buf
, _("unable to locate repository; .git is not a file"), cb_data
);
686 } else if (err
== READ_GITFILE_ERR_NOT_A_REPO
) {
687 if (!(backlink
= infer_backlink(realdotgit
.buf
))) {
688 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file does not reference a repository"), cb_data
);
692 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file broken"), cb_data
);
696 strbuf_addf(&gitdir
, "%s/gitdir", backlink
);
697 if (strbuf_read_file(&olddotgit
, gitdir
.buf
, 0) < 0)
698 repair
= _("gitdir unreadable");
700 strbuf_rtrim(&olddotgit
);
701 if (fspathcmp(olddotgit
.buf
, realdotgit
.buf
))
702 repair
= _("gitdir incorrect");
706 fn(0, gitdir
.buf
, repair
, cb_data
);
707 write_file(gitdir
.buf
, "%s", realdotgit
.buf
);
711 strbuf_release(&olddotgit
);
712 strbuf_release(&gitdir
);
713 strbuf_release(&realdotgit
);
714 strbuf_release(&dotgit
);
717 int should_prune_worktree(const char *id
, struct strbuf
*reason
, char **wtpath
, timestamp_t expire
)
726 if (!is_directory(git_path("worktrees/%s", id
))) {
727 strbuf_addstr(reason
, _("not a valid directory"));
730 if (file_exists(git_path("worktrees/%s/locked", id
)))
732 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
733 strbuf_addstr(reason
, _("gitdir file does not exist"));
736 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
738 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
742 len
= xsize_t(st
.st_size
);
743 path
= xmallocz(len
);
745 read_result
= read_in_full(fd
, path
, len
);
746 if (read_result
< 0) {
747 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
755 if (read_result
!= len
) {
757 _("short read (expected %"PRIuMAX
" bytes, read %"PRIuMAX
")"),
758 (uintmax_t)len
, (uintmax_t)read_result
);
762 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
765 strbuf_addstr(reason
, _("invalid gitdir file"));
770 if (!file_exists(path
)) {
771 if (stat(git_path("worktrees/%s/index", id
), &st
) ||
772 st
.st_mtime
<= expire
) {
773 strbuf_addstr(reason
, _("gitdir file points to non-existent location"));
785 static int move_config_setting(const char *key
, const char *value
,
786 const char *from_file
, const char *to_file
)
788 if (git_config_set_in_file_gently(to_file
, key
, value
))
789 return error(_("unable to set %s in '%s'"), key
, to_file
);
790 if (git_config_set_in_file_gently(from_file
, key
, NULL
))
791 return error(_("unable to unset %s in '%s'"), key
, from_file
);
795 int init_worktree_config(struct repository
*r
)
799 struct config_set cs
= { { 0 } };
800 const char *core_worktree
;
801 char *common_config_file
;
802 char *main_worktree_file
;
805 * If the extension is already enabled, then we can skip the
808 if (r
->repository_format_worktree_config
)
810 if ((res
= git_config_set_gently("extensions.worktreeConfig", "true")))
811 return error(_("failed to set extensions.worktreeConfig setting"));
813 common_config_file
= xstrfmt("%s/config", r
->commondir
);
814 main_worktree_file
= xstrfmt("%s/config.worktree", r
->commondir
);
816 git_configset_init(&cs
);
817 git_configset_add_file(&cs
, common_config_file
);
820 * If core.bare is true in the common config file, then we need to
821 * move it to the main worktree's config file or it will break all
822 * worktrees. If it is false, then leave it in place because it
823 * _could_ be negating a global core.bare=true.
825 if (!git_configset_get_bool(&cs
, "core.bare", &bare
) && bare
) {
826 if ((res
= move_config_setting("core.bare", "true",
828 main_worktree_file
)))
832 * If core.worktree is set, then the main worktree is located
833 * somewhere different than the parent of the common Git dir.
834 * Relocate that value to avoid breaking all worktrees with this
835 * upgrade to worktree config.
837 if (!git_configset_get_value(&cs
, "core.worktree", &core_worktree
, NULL
)) {
838 if ((res
= move_config_setting("core.worktree", core_worktree
,
840 main_worktree_file
)))
845 * Ensure that we use worktree config for the remaining lifetime
846 * of the current process.
848 r
->repository_format_worktree_config
= 1;
851 git_configset_clear(&cs
);
852 free(common_config_file
);
853 free(main_worktree_file
);