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 worktree
= xcalloc(1, sizeof(*worktree
));
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 worktree
= xcalloc(1, sizeof(*worktree
));
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(dir
)) != NULL
) {
132 struct worktree
*linked
= NULL
;
133 if (is_dot_or_dotdot(d
->d_name
))
136 if ((linked
= get_linked_worktree(d
->d_name
))) {
137 ALLOC_GROW(list
, counter
+ 1, alloc
);
138 list
[counter
++] = linked
;
143 ALLOC_GROW(list
, counter
+ 1, alloc
);
144 list
[counter
] = NULL
;
146 mark_current_worktree(list
);
150 const char *get_worktree_git_dir(const struct worktree
*wt
)
153 return get_git_dir();
155 return get_git_common_dir();
157 return git_common_path("worktrees/%s", wt
->id
);
160 static struct worktree
*find_worktree_by_suffix(struct worktree
**list
,
163 struct worktree
*found
= NULL
;
164 int nr_found
= 0, suffixlen
;
166 suffixlen
= strlen(suffix
);
170 for (; *list
&& nr_found
< 2; list
++) {
171 const char *path
= (*list
)->path
;
172 int pathlen
= strlen(path
);
173 int start
= pathlen
- suffixlen
;
175 /* suffix must start at directory boundary */
176 if ((!start
|| (start
> 0 && is_dir_sep(path
[start
- 1]))) &&
177 !fspathcmp(suffix
, path
+ start
)) {
182 return nr_found
== 1 ? found
: NULL
;
185 struct worktree
*find_worktree(struct worktree
**list
,
190 char *to_free
= NULL
;
192 if ((wt
= find_worktree_by_suffix(list
, arg
)))
196 arg
= to_free
= prefix_filename(prefix
, arg
);
197 wt
= find_worktree_by_path(list
, arg
);
202 struct worktree
*find_worktree_by_path(struct worktree
**list
, const char *p
)
204 struct strbuf wt_path
= STRBUF_INIT
;
205 char *path
= real_pathdup(p
, 0);
209 for (; *list
; list
++) {
210 if (!strbuf_realpath(&wt_path
, (*list
)->path
, 0))
213 if (!fspathcmp(path
, wt_path
.buf
))
217 strbuf_release(&wt_path
);
221 int is_main_worktree(const struct worktree
*wt
)
226 const char *worktree_lock_reason(struct worktree
*wt
)
228 if (is_main_worktree(wt
))
231 if (!wt
->lock_reason_valid
) {
232 struct strbuf path
= STRBUF_INIT
;
234 strbuf_addstr(&path
, worktree_git_path(wt
, "locked"));
235 if (file_exists(path
.buf
)) {
236 struct strbuf lock_reason
= STRBUF_INIT
;
237 if (strbuf_read_file(&lock_reason
, path
.buf
, 0) < 0)
238 die_errno(_("failed to read '%s'"), path
.buf
);
239 strbuf_trim(&lock_reason
);
240 wt
->lock_reason
= strbuf_detach(&lock_reason
, NULL
);
242 wt
->lock_reason
= NULL
;
243 wt
->lock_reason_valid
= 1;
244 strbuf_release(&path
);
247 return wt
->lock_reason
;
250 const char *worktree_prune_reason(struct worktree
*wt
, timestamp_t expire
)
252 struct strbuf reason
= STRBUF_INIT
;
255 if (is_main_worktree(wt
))
257 if (wt
->prune_reason_valid
)
258 return wt
->prune_reason
;
260 if (should_prune_worktree(wt
->id
, &reason
, &path
, expire
))
261 wt
->prune_reason
= strbuf_detach(&reason
, NULL
);
262 wt
->prune_reason_valid
= 1;
264 strbuf_release(&reason
);
266 return wt
->prune_reason
;
269 /* convenient wrapper to deal with NULL strbuf */
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(const char *symref
,
409 const struct worktree
*existing
= NULL
;
410 static struct worktree
**worktrees
;
414 free_worktrees(worktrees
);
415 worktrees
= get_worktrees();
417 for (i
= 0; worktrees
[i
]; i
++) {
418 struct worktree
*wt
= worktrees
[i
];
419 const char *symref_target
;
420 struct ref_store
*refs
;
426 if (wt
->is_detached
&& !strcmp(symref
, "HEAD")) {
427 if (is_worktree_being_rebased(wt
, target
)) {
431 if (is_worktree_being_bisected(wt
, target
)) {
437 refs
= get_worktree_ref_store(wt
);
438 symref_target
= refs_resolve_ref_unsafe(refs
, symref
, 0,
440 if ((flags
& REF_ISSYMREF
) &&
441 symref_target
&& !strcmp(symref_target
, 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 while ((d
= readdir(dir
)) != NULL
) {
490 if (is_dot_or_dotdot(d
->d_name
))
500 int parse_worktree_ref(const char *worktree_ref
, const char **name
,
501 int *name_length
, const char **ref
)
503 if (skip_prefix(worktree_ref
, "main-worktree/", &worktree_ref
)) {
514 if (skip_prefix(worktree_ref
, "worktrees/", &worktree_ref
)) {
515 const char *slash
= strchr(worktree_ref
, '/');
517 if (!slash
|| slash
== worktree_ref
|| !slash
[1])
520 *name
= worktree_ref
;
522 *name_length
= slash
- worktree_ref
;
530 void strbuf_worktree_ref(const struct worktree
*wt
,
534 switch (ref_type(refname
)) {
535 case REF_TYPE_PSEUDOREF
:
536 case REF_TYPE_PER_WORKTREE
:
537 if (wt
&& !wt
->is_current
) {
538 if (is_main_worktree(wt
))
539 strbuf_addstr(sb
, "main-worktree/");
541 strbuf_addf(sb
, "worktrees/%s/", wt
->id
);
545 case REF_TYPE_MAIN_PSEUDOREF
:
546 case REF_TYPE_OTHER_PSEUDOREF
:
549 case REF_TYPE_NORMAL
:
551 * For shared refs, don't prefix worktrees/ or
552 * main-worktree/. It's not necessary and
553 * files-backend.c can't handle it anyway.
557 strbuf_addstr(sb
, refname
);
560 int other_head_refs(each_ref_fn fn
, void *cb_data
)
562 struct worktree
**worktrees
, **p
;
563 struct strbuf refname
= STRBUF_INIT
;
566 worktrees
= get_worktrees();
567 for (p
= worktrees
; *p
; p
++) {
568 struct worktree
*wt
= *p
;
569 struct object_id oid
;
575 strbuf_reset(&refname
);
576 strbuf_worktree_ref(wt
, &refname
, "HEAD");
577 if (!refs_read_ref_full(get_main_ref_store(the_repository
),
581 ret
= fn(refname
.buf
, &oid
, flag
, cb_data
);
585 free_worktrees(worktrees
);
586 strbuf_release(&refname
);
591 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
592 * pointing at <repo>/worktrees/<id>.
594 static void repair_gitfile(struct worktree
*wt
,
595 worktree_repair_fn fn
, void *cb_data
)
597 struct strbuf dotgit
= STRBUF_INIT
;
598 struct strbuf repo
= STRBUF_INIT
;
600 const char *repair
= NULL
;
603 /* missing worktree can't be repaired */
604 if (!file_exists(wt
->path
))
607 if (!is_directory(wt
->path
)) {
608 fn(1, wt
->path
, _("not a directory"), cb_data
);
612 strbuf_realpath(&repo
, git_common_path("worktrees/%s", wt
->id
), 1);
613 strbuf_addf(&dotgit
, "%s/.git", wt
->path
);
614 backlink
= xstrdup_or_null(read_gitfile_gently(dotgit
.buf
, &err
));
616 if (err
== READ_GITFILE_ERR_NOT_A_FILE
)
617 fn(1, wt
->path
, _(".git is not a file"), cb_data
);
619 repair
= _(".git file broken");
620 else if (fspathcmp(backlink
, repo
.buf
))
621 repair
= _(".git file incorrect");
624 fn(0, wt
->path
, repair
, cb_data
);
625 write_file(dotgit
.buf
, "gitdir: %s", repo
.buf
);
629 strbuf_release(&repo
);
630 strbuf_release(&dotgit
);
633 static void repair_noop(int iserr
, const char *path
, const char *msg
,
639 void repair_worktrees(worktree_repair_fn fn
, void *cb_data
)
641 struct worktree
**worktrees
= get_worktrees();
642 struct worktree
**wt
= worktrees
+ 1; /* +1 skips main worktree */
647 repair_gitfile(*wt
, fn
, cb_data
);
648 free_worktrees(worktrees
);
651 static int is_main_worktree_path(const char *path
)
653 struct strbuf target
= STRBUF_INIT
;
654 struct strbuf maindir
= STRBUF_INIT
;
657 strbuf_add_real_path(&target
, path
);
658 strbuf_strip_suffix(&target
, "/.git");
659 strbuf_add_real_path(&maindir
, get_git_common_dir());
660 strbuf_strip_suffix(&maindir
, "/.git");
661 cmp
= fspathcmp(maindir
.buf
, target
.buf
);
663 strbuf_release(&maindir
);
664 strbuf_release(&target
);
669 * If both the main worktree and linked worktree have been moved, then the
670 * gitfile /path/to/worktree/.git won't point into the repository, thus we
671 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
672 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
673 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
675 static char *infer_backlink(const char *gitfile
)
677 struct strbuf actual
= STRBUF_INIT
;
678 struct strbuf inferred
= STRBUF_INIT
;
681 if (strbuf_read_file(&actual
, gitfile
, 0) < 0)
683 if (!starts_with(actual
.buf
, "gitdir:"))
685 if (!(id
= find_last_dir_sep(actual
.buf
)))
687 strbuf_trim(&actual
);
688 id
++; /* advance past '/' to point at <id> */
691 strbuf_git_common_path(&inferred
, the_repository
, "worktrees/%s", id
);
692 if (!is_directory(inferred
.buf
))
695 strbuf_release(&actual
);
696 return strbuf_detach(&inferred
, NULL
);
699 strbuf_release(&actual
);
700 strbuf_release(&inferred
);
705 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
706 * the worktree's path.
708 void repair_worktree_at_path(const char *path
,
709 worktree_repair_fn fn
, void *cb_data
)
711 struct strbuf dotgit
= STRBUF_INIT
;
712 struct strbuf realdotgit
= STRBUF_INIT
;
713 struct strbuf gitdir
= STRBUF_INIT
;
714 struct strbuf olddotgit
= STRBUF_INIT
;
715 char *backlink
= NULL
;
716 const char *repair
= NULL
;
722 if (is_main_worktree_path(path
))
725 strbuf_addf(&dotgit
, "%s/.git", path
);
726 if (!strbuf_realpath(&realdotgit
, dotgit
.buf
, 0)) {
727 fn(1, path
, _("not a valid path"), cb_data
);
731 backlink
= xstrdup_or_null(read_gitfile_gently(realdotgit
.buf
, &err
));
732 if (err
== READ_GITFILE_ERR_NOT_A_FILE
) {
733 fn(1, realdotgit
.buf
, _("unable to locate repository; .git is not a file"), cb_data
);
735 } else if (err
== READ_GITFILE_ERR_NOT_A_REPO
) {
736 if (!(backlink
= infer_backlink(realdotgit
.buf
))) {
737 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file does not reference a repository"), cb_data
);
741 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file broken"), cb_data
);
745 strbuf_addf(&gitdir
, "%s/gitdir", backlink
);
746 if (strbuf_read_file(&olddotgit
, gitdir
.buf
, 0) < 0)
747 repair
= _("gitdir unreadable");
749 strbuf_rtrim(&olddotgit
);
750 if (fspathcmp(olddotgit
.buf
, realdotgit
.buf
))
751 repair
= _("gitdir incorrect");
755 fn(0, gitdir
.buf
, repair
, cb_data
);
756 write_file(gitdir
.buf
, "%s", realdotgit
.buf
);
760 strbuf_release(&olddotgit
);
761 strbuf_release(&gitdir
);
762 strbuf_release(&realdotgit
);
763 strbuf_release(&dotgit
);
766 int should_prune_worktree(const char *id
, struct strbuf
*reason
, char **wtpath
, timestamp_t expire
)
775 if (!is_directory(git_path("worktrees/%s", id
))) {
776 strbuf_addstr(reason
, _("not a valid directory"));
779 if (file_exists(git_path("worktrees/%s/locked", id
)))
781 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
782 strbuf_addstr(reason
, _("gitdir file does not exist"));
785 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
787 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
791 len
= xsize_t(st
.st_size
);
792 path
= xmallocz(len
);
794 read_result
= read_in_full(fd
, path
, len
);
795 if (read_result
< 0) {
796 strbuf_addf(reason
, _("unable to read gitdir file (%s)"),
804 if (read_result
!= len
) {
806 _("short read (expected %"PRIuMAX
" bytes, read %"PRIuMAX
")"),
807 (uintmax_t)len
, (uintmax_t)read_result
);
811 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
814 strbuf_addstr(reason
, _("invalid gitdir file"));
819 if (!file_exists(path
)) {
820 if (stat(git_path("worktrees/%s/index", id
), &st
) ||
821 st
.st_mtime
<= expire
) {
822 strbuf_addstr(reason
, _("gitdir file points to non-existent location"));