8 void free_worktrees(struct worktree
**worktrees
)
12 for (i
= 0; worktrees
[i
]; i
++) {
13 free(worktrees
[i
]->path
);
14 free(worktrees
[i
]->id
);
15 free(worktrees
[i
]->head_ref
);
22 * read 'path_to_ref' into 'ref'. Also if is_detached is not NULL,
23 * set is_detached to 1 (0) if the ref is detached (is not detached).
25 * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside $GIT_DIR so
26 * for linked worktrees, `resolve_ref_unsafe()` won't work (it uses
27 * git_path). Parse the ref ourselves.
29 * return -1 if the ref is not a proper ref, 0 otherwise (success)
31 static int parse_ref(char *path_to_ref
, struct strbuf
*ref
, int *is_detached
)
35 if (!strbuf_readlink(ref
, path_to_ref
, 0)) {
36 /* HEAD is symbolic link */
37 if (!starts_with(ref
->buf
, "refs/") ||
38 check_refname_format(ref
->buf
, 0))
40 } else if (strbuf_read_file(ref
, path_to_ref
, 0) >= 0) {
41 /* textual symref or detached */
42 if (!starts_with(ref
->buf
, "ref:")) {
46 strbuf_remove(ref
, 0, strlen("ref:"));
48 if (check_refname_format(ref
->buf
, 0))
57 * Add the head_sha1 and head_ref (if not detached) to the given worktree
59 static void add_head_info(struct strbuf
*head_ref
, struct worktree
*worktree
)
62 if (worktree
->is_detached
) {
63 get_sha1_hex(head_ref
->buf
, worktree
->head_sha1
);
65 resolve_ref_unsafe(head_ref
->buf
, 0, worktree
->head_sha1
, NULL
);
66 worktree
->head_ref
= strbuf_detach(head_ref
, NULL
);
72 * get the main worktree
74 static struct worktree
*get_main_worktree(void)
76 struct worktree
*worktree
= NULL
;
77 struct strbuf path
= STRBUF_INIT
;
78 struct strbuf worktree_path
= STRBUF_INIT
;
79 struct strbuf head_ref
= STRBUF_INIT
;
83 strbuf_add_absolute_path(&worktree_path
, get_git_common_dir());
84 is_bare
= !strbuf_strip_suffix(&worktree_path
, "/.git");
86 strbuf_strip_suffix(&worktree_path
, "/.");
88 strbuf_addf(&path
, "%s/HEAD", get_git_common_dir());
90 if (parse_ref(path
.buf
, &head_ref
, &is_detached
) < 0)
93 worktree
= xmalloc(sizeof(struct worktree
));
94 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
96 worktree
->is_bare
= is_bare
;
97 worktree
->head_ref
= NULL
;
98 worktree
->is_detached
= is_detached
;
99 worktree
->is_current
= 0;
100 add_head_info(&head_ref
, worktree
);
103 strbuf_release(&path
);
104 strbuf_release(&worktree_path
);
105 strbuf_release(&head_ref
);
109 static struct worktree
*get_linked_worktree(const char *id
)
111 struct worktree
*worktree
= NULL
;
112 struct strbuf path
= STRBUF_INIT
;
113 struct strbuf worktree_path
= STRBUF_INIT
;
114 struct strbuf head_ref
= STRBUF_INIT
;
118 die("Missing linked worktree name");
120 strbuf_git_common_path(&path
, "worktrees/%s/gitdir", id
);
121 if (strbuf_read_file(&worktree_path
, path
.buf
, 0) <= 0)
122 /* invalid gitdir file */
125 strbuf_rtrim(&worktree_path
);
126 if (!strbuf_strip_suffix(&worktree_path
, "/.git")) {
127 strbuf_reset(&worktree_path
);
128 strbuf_add_absolute_path(&worktree_path
, ".");
129 strbuf_strip_suffix(&worktree_path
, "/.");
133 strbuf_addf(&path
, "%s/worktrees/%s/HEAD", get_git_common_dir(), id
);
135 if (parse_ref(path
.buf
, &head_ref
, &is_detached
) < 0)
138 worktree
= xmalloc(sizeof(struct worktree
));
139 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
140 worktree
->id
= xstrdup(id
);
141 worktree
->is_bare
= 0;
142 worktree
->head_ref
= NULL
;
143 worktree
->is_detached
= is_detached
;
144 worktree
->is_current
= 0;
145 add_head_info(&head_ref
, worktree
);
148 strbuf_release(&path
);
149 strbuf_release(&worktree_path
);
150 strbuf_release(&head_ref
);
154 static void mark_current_worktree(struct worktree
**worktrees
)
156 struct strbuf git_dir
= STRBUF_INIT
;
157 struct strbuf path
= STRBUF_INIT
;
160 strbuf_addstr(&git_dir
, absolute_path(get_git_dir()));
161 for (i
= 0; worktrees
[i
]; i
++) {
162 struct worktree
*wt
= worktrees
[i
];
163 strbuf_addstr(&path
, absolute_path(get_worktree_git_dir(wt
)));
164 wt
->is_current
= !fspathcmp(git_dir
.buf
, path
.buf
);
169 strbuf_release(&git_dir
);
170 strbuf_release(&path
);
173 struct worktree
**get_worktrees(void)
175 struct worktree
**list
= NULL
;
176 struct strbuf path
= STRBUF_INIT
;
179 int counter
= 0, alloc
= 2;
181 list
= xmalloc(alloc
* sizeof(struct worktree
*));
183 if ((list
[counter
] = get_main_worktree()))
186 strbuf_addf(&path
, "%s/worktrees", get_git_common_dir());
187 dir
= opendir(path
.buf
);
188 strbuf_release(&path
);
190 while ((d
= readdir(dir
)) != NULL
) {
191 struct worktree
*linked
= NULL
;
192 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
195 if ((linked
= get_linked_worktree(d
->d_name
))) {
196 ALLOC_GROW(list
, counter
+ 1, alloc
);
197 list
[counter
++] = linked
;
202 ALLOC_GROW(list
, counter
+ 1, alloc
);
203 list
[counter
] = NULL
;
205 mark_current_worktree(list
);
209 const char *get_worktree_git_dir(const struct worktree
*wt
)
212 return get_git_dir();
214 return get_git_common_dir();
216 return git_common_path("worktrees/%s", wt
->id
);
219 int is_worktree_being_rebased(const struct worktree
*wt
,
222 struct wt_status_state state
;
225 memset(&state
, 0, sizeof(state
));
226 found_rebase
= wt_status_check_rebase(wt
, &state
) &&
227 ((state
.rebase_in_progress
||
228 state
.rebase_interactive_in_progress
) &&
230 starts_with(target
, "refs/heads/") &&
231 !strcmp(state
.branch
, target
+ strlen("refs/heads/")));
237 int is_worktree_being_bisected(const struct worktree
*wt
,
240 struct wt_status_state state
;
243 memset(&state
, 0, sizeof(state
));
244 found_rebase
= wt_status_check_bisect(wt
, &state
) &&
246 starts_with(target
, "refs/heads/") &&
247 !strcmp(state
.branch
, target
+ strlen("refs/heads/"));
253 * note: this function should be able to detect shared symref even if
254 * HEAD is temporarily detached (e.g. in the middle of rebase or
255 * bisect). New commands that do similar things should update this
258 const struct worktree
*find_shared_symref(const char *symref
,
261 const struct worktree
*existing
= NULL
;
262 struct strbuf path
= STRBUF_INIT
;
263 struct strbuf sb
= STRBUF_INIT
;
264 static struct worktree
**worktrees
;
268 free_worktrees(worktrees
);
269 worktrees
= get_worktrees();
271 for (i
= 0; worktrees
[i
]; i
++) {
272 struct worktree
*wt
= worktrees
[i
];
274 if (wt
->is_detached
&& !strcmp(symref
, "HEAD")) {
275 if (is_worktree_being_rebased(wt
, target
)) {
279 if (is_worktree_being_bisected(wt
, target
)) {
287 strbuf_addf(&path
, "%s/%s",
288 get_worktree_git_dir(wt
),
291 if (parse_ref(path
.buf
, &sb
, NULL
)) {
295 if (!strcmp(sb
.buf
, target
)) {
301 strbuf_release(&path
);