2 #include "repository.h"
9 void free_worktrees(struct worktree
**worktrees
)
13 for (i
= 0; worktrees
[i
]; i
++) {
14 free(worktrees
[i
]->path
);
15 free(worktrees
[i
]->id
);
16 free(worktrees
[i
]->head_ref
);
17 free(worktrees
[i
]->lock_reason
);
18 free(worktrees
[i
]->prune_reason
);
25 * Update head_oid, head_ref and is_detached of the given worktree
27 static void add_head_info(struct worktree
*wt
)
32 target
= refs_resolve_ref_unsafe(get_worktree_ref_store(wt
),
35 &wt
->head_oid
, &flags
);
39 if (flags
& REF_ISSYMREF
)
40 wt
->head_ref
= xstrdup(target
);
46 * get the main worktree
48 static struct worktree
*get_main_worktree(void)
50 struct worktree
*worktree
= NULL
;
51 struct strbuf worktree_path
= STRBUF_INIT
;
53 strbuf_add_real_path(&worktree_path
, get_git_common_dir());
54 strbuf_strip_suffix(&worktree_path
, "/.git");
56 CALLOC_ARRAY(worktree
, 1);
57 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
59 * NEEDSWORK: If this function is called from a secondary worktree and
60 * config.worktree is present, is_bare_repository_cfg will reflect the
61 * contents of config.worktree, not the contents of the main worktree.
62 * This means that worktree->is_bare may be set to 0 even if the main
63 * worktree is configured to be bare.
65 worktree
->is_bare
= (is_bare_repository_cfg
== 1) ||
67 add_head_info(worktree
);
71 static struct worktree
*get_linked_worktree(const char *id
)
73 struct worktree
*worktree
= NULL
;
74 struct strbuf path
= STRBUF_INIT
;
75 struct strbuf worktree_path
= STRBUF_INIT
;
78 die("Missing linked worktree name");
80 strbuf_git_common_path(&path
, the_repository
, "worktrees/%s/gitdir", id
);
81 if (strbuf_read_file(&worktree_path
, path
.buf
, 0) <= 0)
82 /* invalid gitdir file */
84 strbuf_rtrim(&worktree_path
);
85 strbuf_strip_suffix(&worktree_path
, "/.git");
87 CALLOC_ARRAY(worktree
, 1);
88 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
89 worktree
->id
= xstrdup(id
);
90 add_head_info(worktree
);
93 strbuf_release(&path
);
94 strbuf_release(&worktree_path
);
98 static void mark_current_worktree(struct worktree
**worktrees
)
100 char *git_dir
= absolute_pathdup(get_git_dir());
103 for (i
= 0; worktrees
[i
]; i
++) {
104 struct worktree
*wt
= worktrees
[i
];
105 const char *wt_git_dir
= get_worktree_git_dir(wt
);
107 if (!fspathcmp(git_dir
, absolute_path(wt_git_dir
))) {
115 struct worktree
**get_worktrees(void)
117 struct worktree
**list
= NULL
;
118 struct strbuf path
= STRBUF_INIT
;
121 int counter
= 0, alloc
= 2;
123 ALLOC_ARRAY(list
, alloc
);
125 list
[counter
++] = get_main_worktree();
127 strbuf_addf(&path
, "%s/worktrees", get_git_common_dir());
128 dir
= opendir(path
.buf
);
129 strbuf_release(&path
);
131 while ((d
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
132 struct worktree
*linked
= NULL
;
134 if ((linked
= get_linked_worktree(d
->d_name
))) {
135 ALLOC_GROW(list
, counter
+ 1, alloc
);
136 list
[counter
++] = linked
;
141 ALLOC_GROW(list
, counter
+ 1, alloc
);
142 list
[counter
] = NULL
;
144 mark_current_worktree(list
);
148 const char *get_worktree_git_dir(const struct worktree
*wt
)
151 return get_git_dir();
153 return get_git_common_dir();
155 return git_common_path("worktrees/%s", wt
->id
);
158 static struct worktree
*find_worktree_by_suffix(struct worktree
**list
,
161 struct worktree
*found
= NULL
;
162 int nr_found
= 0, suffixlen
;
164 suffixlen
= strlen(suffix
);
168 for (; *list
&& nr_found
< 2; list
++) {
169 const char *path
= (*list
)->path
;
170 int pathlen
= strlen(path
);
171 int start
= pathlen
- suffixlen
;
173 /* suffix must start at directory boundary */
174 if ((!start
|| (start
> 0 && is_dir_sep(path
[start
- 1]))) &&
175 !fspathcmp(suffix
, path
+ start
)) {
180 return nr_found
== 1 ? found
: NULL
;
183 struct worktree
*find_worktree(struct worktree
**list
,
188 char *to_free
= NULL
;
190 if ((wt
= find_worktree_by_suffix(list
, arg
)))
194 arg
= to_free
= prefix_filename(prefix
, arg
);
195 wt
= find_worktree_by_path(list
, arg
);
200 struct worktree
*find_worktree_by_path(struct worktree
**list
, const char *p
)
202 struct strbuf wt_path
= STRBUF_INIT
;
203 char *path
= real_pathdup(p
, 0);
207 for (; *list
; list
++) {
208 if (!strbuf_realpath(&wt_path
, (*list
)->path
, 0))
211 if (!fspathcmp(path
, wt_path
.buf
))
215 strbuf_release(&wt_path
);
219 int is_main_worktree(const struct worktree
*wt
)
224 const char *worktree_lock_reason(struct worktree
*wt
)
226 if (is_main_worktree(wt
))
229 if (!wt
->lock_reason_valid
) {
230 struct strbuf path
= STRBUF_INIT
;
232 strbuf_addstr(&path
, worktree_git_path(wt
, "locked"));
233 if (file_exists(path
.buf
)) {
234 struct strbuf lock_reason
= STRBUF_INIT
;
235 if (strbuf_read_file(&lock_reason
, path
.buf
, 0) < 0)
236 die_errno(_("failed to read '%s'"), path
.buf
);
237 strbuf_trim(&lock_reason
);
238 wt
->lock_reason
= strbuf_detach(&lock_reason
, NULL
);
240 wt
->lock_reason
= NULL
;
241 wt
->lock_reason_valid
= 1;
242 strbuf_release(&path
);
245 return wt
->lock_reason
;
248 const char *worktree_prune_reason(struct worktree
*wt
, timestamp_t expire
)
250 struct strbuf reason
= STRBUF_INIT
;
253 if (is_main_worktree(wt
))
255 if (wt
->prune_reason_valid
)
256 return wt
->prune_reason
;
258 if (should_prune_worktree(wt
->id
, &reason
, &path
, expire
))
259 wt
->prune_reason
= strbuf_detach(&reason
, NULL
);
260 wt
->prune_reason_valid
= 1;
262 strbuf_release(&reason
);
264 return wt
->prune_reason
;
267 /* convenient wrapper to deal with NULL strbuf */
268 static void strbuf_addf_gently(struct strbuf
*buf
, const char *fmt
, ...)
275 va_start(params
, fmt
);
276 strbuf_vaddf(buf
, fmt
, params
);
280 int validate_worktree(const struct worktree
*wt
, struct strbuf
*errmsg
,
283 struct strbuf wt_path
= STRBUF_INIT
;
284 struct strbuf realpath
= STRBUF_INIT
;
288 strbuf_addf(&wt_path
, "%s/.git", wt
->path
);
290 if (is_main_worktree(wt
)) {
291 if (is_directory(wt_path
.buf
)) {
296 * Main worktree using .git file to point to the
297 * repository would make it impossible to know where
298 * the actual worktree is if this function is executed
299 * from another worktree. No .git file support for now.
301 strbuf_addf_gently(errmsg
,
302 _("'%s' at main working tree is not the repository directory"),
308 * Make sure "gitdir" file points to a real .git file and that
309 * file points back here.
311 if (!is_absolute_path(wt
->path
)) {
312 strbuf_addf_gently(errmsg
,
313 _("'%s' file does not contain absolute path to the working tree location"),
314 git_common_path("worktrees/%s/gitdir", wt
->id
));
318 if (flags
& WT_VALIDATE_WORKTREE_MISSING_OK
&&
319 !file_exists(wt
->path
)) {
324 if (!file_exists(wt_path
.buf
)) {
325 strbuf_addf_gently(errmsg
, _("'%s' does not exist"), wt_path
.buf
);
329 path
= xstrdup_or_null(read_gitfile_gently(wt_path
.buf
, &err
));
331 strbuf_addf_gently(errmsg
, _("'%s' is not a .git file, error code %d"),
336 strbuf_realpath(&realpath
, git_common_path("worktrees/%s", wt
->id
), 1);
337 ret
= fspathcmp(path
, realpath
.buf
);
340 strbuf_addf_gently(errmsg
, _("'%s' does not point back to '%s'"),
341 wt
->path
, git_common_path("worktrees/%s", wt
->id
));
344 strbuf_release(&wt_path
);
345 strbuf_release(&realpath
);
349 void update_worktree_location(struct worktree
*wt
, const char *path_
)
351 struct strbuf path
= STRBUF_INIT
;
353 if (is_main_worktree(wt
))
354 BUG("can't relocate main worktree");
356 strbuf_realpath(&path
, path_
, 1);
357 if (fspathcmp(wt
->path
, path
.buf
)) {
358 write_file(git_common_path("worktrees/%s/gitdir", wt
->id
),
359 "%s/.git", path
.buf
);
361 wt
->path
= strbuf_detach(&path
, NULL
);
363 strbuf_release(&path
);
366 int is_worktree_being_rebased(const struct worktree
*wt
,
369 struct wt_status_state state
;
372 memset(&state
, 0, sizeof(state
));
373 found_rebase
= wt_status_check_rebase(wt
, &state
) &&
374 (state
.rebase_in_progress
||
375 state
.rebase_interactive_in_progress
) &&
377 skip_prefix(target
, "refs/heads/", &target
) &&
378 !strcmp(state
.branch
, target
);
379 wt_status_state_free_buffers(&state
);
383 int is_worktree_being_bisected(const struct worktree
*wt
,
386 struct wt_status_state state
;
389 memset(&state
, 0, sizeof(state
));
390 found_bisect
= wt_status_check_bisect(wt
, &state
) &&
392 skip_prefix(target
, "refs/heads/", &target
) &&
393 !strcmp(state
.branch
, target
);
394 wt_status_state_free_buffers(&state
);
399 * note: this function should be able to detect shared symref even if
400 * HEAD is temporarily detached (e.g. in the middle of rebase or
401 * bisect). New commands that do similar things should update this
404 const struct worktree
*find_shared_symref(const char *symref
,
407 const struct worktree
*existing
= NULL
;
408 static struct worktree
**worktrees
;
412 free_worktrees(worktrees
);
413 worktrees
= get_worktrees();
415 for (i
= 0; worktrees
[i
]; i
++) {
416 struct worktree
*wt
= worktrees
[i
];
417 const char *symref_target
;
418 struct ref_store
*refs
;
424 if (wt
->is_detached
&& !strcmp(symref
, "HEAD")) {
425 if (is_worktree_being_rebased(wt
, target
)) {
429 if (is_worktree_being_bisected(wt
, target
)) {
435 refs
= get_worktree_ref_store(wt
);
436 symref_target
= refs_resolve_ref_unsafe(refs
, symref
, 0,
438 if ((flags
& REF_ISSYMREF
) &&
439 symref_target
&& !strcmp(symref_target
, target
)) {
448 int submodule_uses_worktrees(const char *path
)
450 char *submodule_gitdir
;
451 struct strbuf sb
= STRBUF_INIT
, err
= STRBUF_INIT
;
455 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
457 submodule_gitdir
= git_pathdup_submodule(path
, "%s", "");
458 if (!submodule_gitdir
)
461 /* The env would be set for the superproject. */
462 get_common_dir_noenv(&sb
, submodule_gitdir
);
463 free(submodule_gitdir
);
465 strbuf_addstr(&sb
, "/config");
466 read_repository_format(&format
, sb
.buf
);
467 if (verify_repository_format(&format
, &err
)) {
468 strbuf_release(&err
);
470 clear_repository_format(&format
);
473 clear_repository_format(&format
);
474 strbuf_release(&err
);
476 /* Replace config by worktrees. */
477 strbuf_setlen(&sb
, sb
.len
- strlen("config"));
478 strbuf_addstr(&sb
, "worktrees");
480 /* See if there is any file inside the worktrees directory. */
481 dir
= opendir(sb
.buf
);
487 d
= readdir_skip_dot_and_dotdot(dir
);
494 int parse_worktree_ref(const char *worktree_ref
, const char **name
,
495 int *name_length
, const char **ref
)
497 if (skip_prefix(worktree_ref
, "main-worktree/", &worktree_ref
)) {
508 if (skip_prefix(worktree_ref
, "worktrees/", &worktree_ref
)) {
509 const char *slash
= strchr(worktree_ref
, '/');
511 if (!slash
|| slash
== worktree_ref
|| !slash
[1])
514 *name
= worktree_ref
;
516 *name_length
= slash
- worktree_ref
;
524 void strbuf_worktree_ref(const struct worktree
*wt
,
528 switch (ref_type(refname
)) {
529 case REF_TYPE_PSEUDOREF
:
530 case REF_TYPE_PER_WORKTREE
:
531 if (wt
&& !wt
->is_current
) {
532 if (is_main_worktree(wt
))
533 strbuf_addstr(sb
, "main-worktree/");
535 strbuf_addf(sb
, "worktrees/%s/", wt
->id
);
539 case REF_TYPE_MAIN_PSEUDOREF
:
540 case REF_TYPE_OTHER_PSEUDOREF
:
543 case REF_TYPE_NORMAL
:
545 * For shared refs, don't prefix worktrees/ or
546 * main-worktree/. It's not necessary and
547 * files-backend.c can't handle it anyway.
551 strbuf_addstr(sb
, refname
);
554 int other_head_refs(each_ref_fn fn
, void *cb_data
)
556 struct worktree
**worktrees
, **p
;
557 struct strbuf refname
= STRBUF_INIT
;
560 worktrees
= get_worktrees();
561 for (p
= worktrees
; *p
; p
++) {
562 struct worktree
*wt
= *p
;
563 struct object_id oid
;
569 strbuf_reset(&refname
);
570 strbuf_worktree_ref(wt
, &refname
, "HEAD");
571 if (!refs_read_ref_full(get_main_ref_store(the_repository
),
575 ret
= fn(refname
.buf
, &oid
, flag
, cb_data
);
579 free_worktrees(worktrees
);
580 strbuf_release(&refname
);
585 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
586 * pointing at <repo>/worktrees/<id>.
588 static void repair_gitfile(struct worktree
*wt
,
589 worktree_repair_fn fn
, void *cb_data
)
591 struct strbuf dotgit
= STRBUF_INIT
;
592 struct strbuf repo
= STRBUF_INIT
;
594 const char *repair
= NULL
;
597 /* missing worktree can't be repaired */
598 if (!file_exists(wt
->path
))
601 if (!is_directory(wt
->path
)) {
602 fn(1, wt
->path
, _("not a directory"), cb_data
);
606 strbuf_realpath(&repo
, git_common_path("worktrees/%s", wt
->id
), 1);
607 strbuf_addf(&dotgit
, "%s/.git", wt
->path
);
608 backlink
= xstrdup_or_null(read_gitfile_gently(dotgit
.buf
, &err
));
610 if (err
== READ_GITFILE_ERR_NOT_A_FILE
)
611 fn(1, wt
->path
, _(".git is not a file"), cb_data
);
613 repair
= _(".git file broken");
614 else if (fspathcmp(backlink
, repo
.buf
))
615 repair
= _(".git file incorrect");
618 fn(0, wt
->path
, repair
, cb_data
);
619 write_file(dotgit
.buf
, "gitdir: %s", repo
.buf
);
623 strbuf_release(&repo
);
624 strbuf_release(&dotgit
);
627 static void repair_noop(int iserr
, const char *path
, const char *msg
,
633 void repair_worktrees(worktree_repair_fn fn
, void *cb_data
)
635 struct worktree
**worktrees
= get_worktrees();
636 struct worktree
**wt
= worktrees
+ 1; /* +1 skips main worktree */
641 repair_gitfile(*wt
, fn
, cb_data
);
642 free_worktrees(worktrees
);
645 static int is_main_worktree_path(const char *path
)
647 struct strbuf target
= STRBUF_INIT
;
648 struct strbuf maindir
= STRBUF_INIT
;
651 strbuf_add_real_path(&target
, path
);
652 strbuf_strip_suffix(&target
, "/.git");
653 strbuf_add_real_path(&maindir
, get_git_common_dir());
654 strbuf_strip_suffix(&maindir
, "/.git");
655 cmp
= fspathcmp(maindir
.buf
, target
.buf
);
657 strbuf_release(&maindir
);
658 strbuf_release(&target
);
663 * If both the main worktree and linked worktree have been moved, then the
664 * gitfile /path/to/worktree/.git won't point into the repository, thus we
665 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
666 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
667 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
669 static char *infer_backlink(const char *gitfile
)
671 struct strbuf actual
= STRBUF_INIT
;
672 struct strbuf inferred
= STRBUF_INIT
;
675 if (strbuf_read_file(&actual
, gitfile
, 0) < 0)
677 if (!starts_with(actual
.buf
, "gitdir:"))
679 if (!(id
= find_last_dir_sep(actual
.buf
)))
681 strbuf_trim(&actual
);
682 id
++; /* advance past '/' to point at <id> */
685 strbuf_git_common_path(&inferred
, the_repository
, "worktrees/%s", id
);
686 if (!is_directory(inferred
.buf
))
689 strbuf_release(&actual
);
690 return strbuf_detach(&inferred
, NULL
);
693 strbuf_release(&actual
);
694 strbuf_release(&inferred
);
699 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
700 * the worktree's path.
702 void repair_worktree_at_path(const char *path
,
703 worktree_repair_fn fn
, void *cb_data
)
705 struct strbuf dotgit
= STRBUF_INIT
;
706 struct strbuf realdotgit
= STRBUF_INIT
;
707 struct strbuf gitdir
= STRBUF_INIT
;
708 struct strbuf olddotgit
= STRBUF_INIT
;
709 char *backlink
= NULL
;
710 const char *repair
= NULL
;
716 if (is_main_worktree_path(path
))
719 strbuf_addf(&dotgit
, "%s/.git", path
);
720 if (!strbuf_realpath(&realdotgit
, dotgit
.buf
, 0)) {
721 fn(1, path
, _("not a valid path"), cb_data
);
725 backlink
= xstrdup_or_null(read_gitfile_gently(realdotgit
.buf
, &err
));
726 if (err
== READ_GITFILE_ERR_NOT_A_FILE
) {
727 fn(1, realdotgit
.buf
, _("unable to locate repository; .git is not a file"), cb_data
);
729 } else if (err
== READ_GITFILE_ERR_NOT_A_REPO
) {
730 if (!(backlink
= infer_backlink(realdotgit
.buf
))) {
731 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file does not reference a repository"), cb_data
);
735 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file broken"), cb_data
);
739 strbuf_addf(&gitdir
, "%s/gitdir", backlink
);
740 if (strbuf_read_file(&olddotgit
, gitdir
.buf
, 0) < 0)
741 repair
= _("gitdir unreadable");
743 strbuf_rtrim(&olddotgit
);
744 if (fspathcmp(olddotgit
.buf
, realdotgit
.buf
))
745 repair
= _("gitdir incorrect");
749 fn(0, gitdir
.buf
, repair
, cb_data
);
750 write_file(gitdir
.buf
, "%s", realdotgit
.buf
);
754 strbuf_release(&olddotgit
);
755 strbuf_release(&gitdir
);
756 strbuf_release(&realdotgit
);
757 strbuf_release(&dotgit
);
760 int should_prune_worktree(const char *id
, struct strbuf
*reason
, char **wtpath
, timestamp_t expire
)
769 if (!is_directory(git_path("worktrees/%s", id
))) {
770 strbuf_addstr(reason
, _("not a valid directory"));
773 if (file_exists(git_path("worktrees/%s/locked", id
)))
775 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
776 strbuf_addstr(reason
, _("gitdir file does not exist"));
779 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
781 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
785 len
= xsize_t(st
.st_size
);
786 path
= xmallocz(len
);
788 read_result
= read_in_full(fd
, path
, len
);
789 if (read_result
< 0) {
790 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
798 if (read_result
!= len
) {
800 _("short read (expected %"PRIuMAX
" bytes, read %"PRIuMAX
")"),
801 (uintmax_t)len
, (uintmax_t)read_result
);
805 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
808 strbuf_addstr(reason
, _("invalid gitdir file"));
813 if (!file_exists(path
)) {
814 if (stat(git_path("worktrees/%s/index", id
), &st
) ||
815 st
.st_mtime
<= expire
) {
816 strbuf_addstr(reason
, _("gitdir file points to non-existent location"));