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
);
24 * Update head_oid, head_ref and is_detached of the given worktree
26 static void add_head_info(struct worktree
*wt
)
31 target
= refs_resolve_ref_unsafe(get_worktree_ref_store(wt
),
34 &wt
->head_oid
, &flags
);
38 if (flags
& REF_ISSYMREF
)
39 wt
->head_ref
= xstrdup(target
);
45 * get the main worktree
47 static struct worktree
*get_main_worktree(void)
49 struct worktree
*worktree
= NULL
;
50 struct strbuf worktree_path
= STRBUF_INIT
;
52 strbuf_add_real_path(&worktree_path
, get_git_common_dir());
53 strbuf_strip_suffix(&worktree_path
, "/.git");
55 worktree
= xcalloc(1, sizeof(*worktree
));
56 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
58 * NEEDSWORK: If this function is called from a secondary worktree and
59 * config.worktree is present, is_bare_repository_cfg will reflect the
60 * contents of config.worktree, not the contents of the main worktree.
61 * This means that worktree->is_bare may be set to 0 even if the main
62 * worktree is configured to be bare.
64 worktree
->is_bare
= (is_bare_repository_cfg
== 1) ||
66 add_head_info(worktree
);
70 static struct worktree
*get_linked_worktree(const char *id
)
72 struct worktree
*worktree
= NULL
;
73 struct strbuf path
= STRBUF_INIT
;
74 struct strbuf worktree_path
= STRBUF_INIT
;
77 die("Missing linked worktree name");
79 strbuf_git_common_path(&path
, the_repository
, "worktrees/%s/gitdir", id
);
80 if (strbuf_read_file(&worktree_path
, path
.buf
, 0) <= 0)
81 /* invalid gitdir file */
83 strbuf_rtrim(&worktree_path
);
84 strbuf_strip_suffix(&worktree_path
, "/.git");
86 worktree
= xcalloc(1, sizeof(*worktree
));
87 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
88 worktree
->id
= xstrdup(id
);
89 add_head_info(worktree
);
92 strbuf_release(&path
);
93 strbuf_release(&worktree_path
);
97 static void mark_current_worktree(struct worktree
**worktrees
)
99 char *git_dir
= absolute_pathdup(get_git_dir());
102 for (i
= 0; worktrees
[i
]; i
++) {
103 struct worktree
*wt
= worktrees
[i
];
104 const char *wt_git_dir
= get_worktree_git_dir(wt
);
106 if (!fspathcmp(git_dir
, absolute_path(wt_git_dir
))) {
114 struct worktree
**get_worktrees(void)
116 struct worktree
**list
= NULL
;
117 struct strbuf path
= STRBUF_INIT
;
120 int counter
= 0, alloc
= 2;
122 ALLOC_ARRAY(list
, alloc
);
124 list
[counter
++] = get_main_worktree();
126 strbuf_addf(&path
, "%s/worktrees", get_git_common_dir());
127 dir
= opendir(path
.buf
);
128 strbuf_release(&path
);
130 while ((d
= readdir(dir
)) != NULL
) {
131 struct worktree
*linked
= NULL
;
132 if (is_dot_or_dotdot(d
->d_name
))
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 assert(!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 /* convenient wrapper to deal with NULL strbuf */
249 static void strbuf_addf_gently(struct strbuf
*buf
, const char *fmt
, ...)
256 va_start(params
, fmt
);
257 strbuf_vaddf(buf
, fmt
, params
);
261 int validate_worktree(const struct worktree
*wt
, struct strbuf
*errmsg
,
264 struct strbuf wt_path
= STRBUF_INIT
;
265 struct strbuf realpath
= STRBUF_INIT
;
269 strbuf_addf(&wt_path
, "%s/.git", wt
->path
);
271 if (is_main_worktree(wt
)) {
272 if (is_directory(wt_path
.buf
)) {
277 * Main worktree using .git file to point to the
278 * repository would make it impossible to know where
279 * the actual worktree is if this function is executed
280 * from another worktree. No .git file support for now.
282 strbuf_addf_gently(errmsg
,
283 _("'%s' at main working tree is not the repository directory"),
289 * Make sure "gitdir" file points to a real .git file and that
290 * file points back here.
292 if (!is_absolute_path(wt
->path
)) {
293 strbuf_addf_gently(errmsg
,
294 _("'%s' file does not contain absolute path to the working tree location"),
295 git_common_path("worktrees/%s/gitdir", wt
->id
));
299 if (flags
& WT_VALIDATE_WORKTREE_MISSING_OK
&&
300 !file_exists(wt
->path
)) {
305 if (!file_exists(wt_path
.buf
)) {
306 strbuf_addf_gently(errmsg
, _("'%s' does not exist"), wt_path
.buf
);
310 path
= xstrdup_or_null(read_gitfile_gently(wt_path
.buf
, &err
));
312 strbuf_addf_gently(errmsg
, _("'%s' is not a .git file, error code %d"),
317 strbuf_realpath(&realpath
, git_common_path("worktrees/%s", wt
->id
), 1);
318 ret
= fspathcmp(path
, realpath
.buf
);
321 strbuf_addf_gently(errmsg
, _("'%s' does not point back to '%s'"),
322 wt
->path
, git_common_path("worktrees/%s", wt
->id
));
325 strbuf_release(&wt_path
);
326 strbuf_release(&realpath
);
330 void update_worktree_location(struct worktree
*wt
, const char *path_
)
332 struct strbuf path
= STRBUF_INIT
;
334 if (is_main_worktree(wt
))
335 BUG("can't relocate main worktree");
337 strbuf_realpath(&path
, path_
, 1);
338 if (fspathcmp(wt
->path
, path
.buf
)) {
339 write_file(git_common_path("worktrees/%s/gitdir", wt
->id
),
340 "%s/.git", path
.buf
);
342 wt
->path
= strbuf_detach(&path
, NULL
);
344 strbuf_release(&path
);
347 int is_worktree_being_rebased(const struct worktree
*wt
,
350 struct wt_status_state state
;
353 memset(&state
, 0, sizeof(state
));
354 found_rebase
= wt_status_check_rebase(wt
, &state
) &&
355 (state
.rebase_in_progress
||
356 state
.rebase_interactive_in_progress
) &&
358 skip_prefix(target
, "refs/heads/", &target
) &&
359 !strcmp(state
.branch
, target
);
360 wt_status_state_free_buffers(&state
);
364 int is_worktree_being_bisected(const struct worktree
*wt
,
367 struct wt_status_state state
;
370 memset(&state
, 0, sizeof(state
));
371 found_bisect
= wt_status_check_bisect(wt
, &state
) &&
373 skip_prefix(target
, "refs/heads/", &target
) &&
374 !strcmp(state
.branch
, target
);
375 wt_status_state_free_buffers(&state
);
380 * note: this function should be able to detect shared symref even if
381 * HEAD is temporarily detached (e.g. in the middle of rebase or
382 * bisect). New commands that do similar things should update this
385 const struct worktree
*find_shared_symref(const char *symref
,
388 const struct worktree
*existing
= NULL
;
389 static struct worktree
**worktrees
;
393 free_worktrees(worktrees
);
394 worktrees
= get_worktrees();
396 for (i
= 0; worktrees
[i
]; i
++) {
397 struct worktree
*wt
= worktrees
[i
];
398 const char *symref_target
;
399 struct ref_store
*refs
;
405 if (wt
->is_detached
&& !strcmp(symref
, "HEAD")) {
406 if (is_worktree_being_rebased(wt
, target
)) {
410 if (is_worktree_being_bisected(wt
, target
)) {
416 refs
= get_worktree_ref_store(wt
);
417 symref_target
= refs_resolve_ref_unsafe(refs
, symref
, 0,
419 if ((flags
& REF_ISSYMREF
) &&
420 symref_target
&& !strcmp(symref_target
, target
)) {
429 int submodule_uses_worktrees(const char *path
)
431 char *submodule_gitdir
;
432 struct strbuf sb
= STRBUF_INIT
, err
= STRBUF_INIT
;
436 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
438 submodule_gitdir
= git_pathdup_submodule(path
, "%s", "");
439 if (!submodule_gitdir
)
442 /* The env would be set for the superproject. */
443 get_common_dir_noenv(&sb
, submodule_gitdir
);
444 free(submodule_gitdir
);
446 strbuf_addstr(&sb
, "/config");
447 read_repository_format(&format
, sb
.buf
);
448 if (verify_repository_format(&format
, &err
)) {
449 strbuf_release(&err
);
451 clear_repository_format(&format
);
454 clear_repository_format(&format
);
455 strbuf_release(&err
);
457 /* Replace config by worktrees. */
458 strbuf_setlen(&sb
, sb
.len
- strlen("config"));
459 strbuf_addstr(&sb
, "worktrees");
461 /* See if there is any file inside the worktrees directory. */
462 dir
= opendir(sb
.buf
);
468 while ((d
= readdir(dir
)) != NULL
) {
469 if (is_dot_or_dotdot(d
->d_name
))
479 int parse_worktree_ref(const char *worktree_ref
, const char **name
,
480 int *name_length
, const char **ref
)
482 if (skip_prefix(worktree_ref
, "main-worktree/", &worktree_ref
)) {
493 if (skip_prefix(worktree_ref
, "worktrees/", &worktree_ref
)) {
494 const char *slash
= strchr(worktree_ref
, '/');
496 if (!slash
|| slash
== worktree_ref
|| !slash
[1])
499 *name
= worktree_ref
;
501 *name_length
= slash
- worktree_ref
;
509 void strbuf_worktree_ref(const struct worktree
*wt
,
513 switch (ref_type(refname
)) {
514 case REF_TYPE_PSEUDOREF
:
515 case REF_TYPE_PER_WORKTREE
:
516 if (wt
&& !wt
->is_current
) {
517 if (is_main_worktree(wt
))
518 strbuf_addstr(sb
, "main-worktree/");
520 strbuf_addf(sb
, "worktrees/%s/", wt
->id
);
524 case REF_TYPE_MAIN_PSEUDOREF
:
525 case REF_TYPE_OTHER_PSEUDOREF
:
528 case REF_TYPE_NORMAL
:
530 * For shared refs, don't prefix worktrees/ or
531 * main-worktree/. It's not necessary and
532 * files-backend.c can't handle it anyway.
536 strbuf_addstr(sb
, refname
);
539 int other_head_refs(each_ref_fn fn
, void *cb_data
)
541 struct worktree
**worktrees
, **p
;
542 struct strbuf refname
= STRBUF_INIT
;
545 worktrees
= get_worktrees();
546 for (p
= worktrees
; *p
; p
++) {
547 struct worktree
*wt
= *p
;
548 struct object_id oid
;
554 strbuf_reset(&refname
);
555 strbuf_worktree_ref(wt
, &refname
, "HEAD");
556 if (!refs_read_ref_full(get_main_ref_store(the_repository
),
560 ret
= fn(refname
.buf
, &oid
, flag
, cb_data
);
564 free_worktrees(worktrees
);
565 strbuf_release(&refname
);
570 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
571 * pointing at <repo>/worktrees/<id>.
573 static void repair_gitfile(struct worktree
*wt
,
574 worktree_repair_fn fn
, void *cb_data
)
576 struct strbuf dotgit
= STRBUF_INIT
;
577 struct strbuf repo
= STRBUF_INIT
;
579 const char *repair
= NULL
;
582 /* missing worktree can't be repaired */
583 if (!file_exists(wt
->path
))
586 if (!is_directory(wt
->path
)) {
587 fn(1, wt
->path
, _("not a directory"), cb_data
);
591 strbuf_realpath(&repo
, git_common_path("worktrees/%s", wt
->id
), 1);
592 strbuf_addf(&dotgit
, "%s/.git", wt
->path
);
593 backlink
= xstrdup_or_null(read_gitfile_gently(dotgit
.buf
, &err
));
595 if (err
== READ_GITFILE_ERR_NOT_A_FILE
)
596 fn(1, wt
->path
, _(".git is not a file"), cb_data
);
598 repair
= _(".git file broken");
599 else if (fspathcmp(backlink
, repo
.buf
))
600 repair
= _(".git file incorrect");
603 fn(0, wt
->path
, repair
, cb_data
);
604 write_file(dotgit
.buf
, "gitdir: %s", repo
.buf
);
608 strbuf_release(&repo
);
609 strbuf_release(&dotgit
);
612 static void repair_noop(int iserr
, const char *path
, const char *msg
,
618 void repair_worktrees(worktree_repair_fn fn
, void *cb_data
)
620 struct worktree
**worktrees
= get_worktrees();
621 struct worktree
**wt
= worktrees
+ 1; /* +1 skips main worktree */
626 repair_gitfile(*wt
, fn
, cb_data
);
627 free_worktrees(worktrees
);
630 static int is_main_worktree_path(const char *path
)
632 struct strbuf target
= STRBUF_INIT
;
633 struct strbuf maindir
= STRBUF_INIT
;
636 strbuf_add_real_path(&target
, path
);
637 strbuf_strip_suffix(&target
, "/.git");
638 strbuf_add_real_path(&maindir
, get_git_common_dir());
639 strbuf_strip_suffix(&maindir
, "/.git");
640 cmp
= fspathcmp(maindir
.buf
, target
.buf
);
642 strbuf_release(&maindir
);
643 strbuf_release(&target
);
648 * If both the main worktree and linked worktree have been moved, then the
649 * gitfile /path/to/worktree/.git won't point into the repository, thus we
650 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
651 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
652 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
654 static char *infer_backlink(const char *gitfile
)
656 struct strbuf actual
= STRBUF_INIT
;
657 struct strbuf inferred
= STRBUF_INIT
;
660 if (strbuf_read_file(&actual
, gitfile
, 0) < 0)
662 if (!starts_with(actual
.buf
, "gitdir:"))
664 if (!(id
= find_last_dir_sep(actual
.buf
)))
666 strbuf_trim(&actual
);
667 id
++; /* advance past '/' to point at <id> */
670 strbuf_git_common_path(&inferred
, the_repository
, "worktrees/%s", id
);
671 if (!is_directory(inferred
.buf
))
674 strbuf_release(&actual
);
675 return strbuf_detach(&inferred
, NULL
);
678 strbuf_release(&actual
);
679 strbuf_release(&inferred
);
684 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
685 * the worktree's path.
687 void repair_worktree_at_path(const char *path
,
688 worktree_repair_fn fn
, void *cb_data
)
690 struct strbuf dotgit
= STRBUF_INIT
;
691 struct strbuf realdotgit
= STRBUF_INIT
;
692 struct strbuf gitdir
= STRBUF_INIT
;
693 struct strbuf olddotgit
= STRBUF_INIT
;
694 char *backlink
= NULL
;
695 const char *repair
= NULL
;
701 if (is_main_worktree_path(path
))
704 strbuf_addf(&dotgit
, "%s/.git", path
);
705 if (!strbuf_realpath(&realdotgit
, dotgit
.buf
, 0)) {
706 fn(1, path
, _("not a valid path"), cb_data
);
710 backlink
= xstrdup_or_null(read_gitfile_gently(realdotgit
.buf
, &err
));
711 if (err
== READ_GITFILE_ERR_NOT_A_FILE
) {
712 fn(1, realdotgit
.buf
, _("unable to locate repository; .git is not a file"), cb_data
);
714 } else if (err
== READ_GITFILE_ERR_NOT_A_REPO
) {
715 if (!(backlink
= infer_backlink(realdotgit
.buf
))) {
716 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file does not reference a repository"), cb_data
);
720 fn(1, realdotgit
.buf
, _("unable to locate repository; .git file broken"), cb_data
);
724 strbuf_addf(&gitdir
, "%s/gitdir", backlink
);
725 if (strbuf_read_file(&olddotgit
, gitdir
.buf
, 0) < 0)
726 repair
= _("gitdir unreadable");
728 strbuf_rtrim(&olddotgit
);
729 if (fspathcmp(olddotgit
.buf
, realdotgit
.buf
))
730 repair
= _("gitdir incorrect");
734 fn(0, gitdir
.buf
, repair
, cb_data
);
735 write_file(gitdir
.buf
, "%s", realdotgit
.buf
);
739 strbuf_release(&olddotgit
);
740 strbuf_release(&gitdir
);
741 strbuf_release(&realdotgit
);
742 strbuf_release(&dotgit
);