]>
Commit | Line | Data |
---|---|---|
ac6c561b MR |
1 | #include "cache.h" |
2 | #include "refs.h" | |
3 | #include "strbuf.h" | |
4 | #include "worktree.h" | |
750e8a60 | 5 | #include "dir.h" |
8d9fdd70 | 6 | #include "wt-status.h" |
ac6c561b | 7 | |
51934904 MR |
8 | void free_worktrees(struct worktree **worktrees) |
9 | { | |
10 | int i = 0; | |
11 | ||
12 | for (i = 0; worktrees[i]; i++) { | |
13 | free(worktrees[i]->path); | |
69dfe3b9 | 14 | free(worktrees[i]->id); |
92718b74 | 15 | free(worktrees[i]->head_ref); |
346ef530 | 16 | free(worktrees[i]->lock_reason); |
51934904 MR |
17 | free(worktrees[i]); |
18 | } | |
19 | free (worktrees); | |
20 | } | |
21 | ||
92718b74 | 22 | /** |
fa099d23 | 23 | * Update head_sha1, head_ref and is_detached of the given worktree |
92718b74 | 24 | */ |
fa099d23 | 25 | static void add_head_info(struct worktree *wt) |
92718b74 | 26 | { |
fa099d23 NTND |
27 | int flags; |
28 | const char *target; | |
29 | ||
30 | target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt), | |
31 | "HEAD", | |
32 | RESOLVE_REF_READING, | |
33 | wt->head_sha1, &flags); | |
34 | if (!target) | |
35 | return; | |
36 | ||
37 | if (flags & REF_ISSYMREF) | |
38 | wt->head_ref = xstrdup(target); | |
39 | else | |
40 | wt->is_detached = 1; | |
92718b74 MR |
41 | } |
42 | ||
51934904 MR |
43 | /** |
44 | * get the main worktree | |
45 | */ | |
46 | static struct worktree *get_main_worktree(void) | |
1ceb7f90 | 47 | { |
51934904 | 48 | struct worktree *worktree = NULL; |
1ceb7f90 | 49 | struct strbuf path = STRBUF_INIT; |
51934904 | 50 | struct strbuf worktree_path = STRBUF_INIT; |
92718b74 | 51 | int is_bare = 0; |
1ceb7f90 | 52 | |
fd2e7daf | 53 | strbuf_add_absolute_path(&worktree_path, get_git_common_dir()); |
92718b74 MR |
54 | is_bare = !strbuf_strip_suffix(&worktree_path, "/.git"); |
55 | if (is_bare) | |
51934904 MR |
56 | strbuf_strip_suffix(&worktree_path, "/."); |
57 | ||
58 | strbuf_addf(&path, "%s/HEAD", get_git_common_dir()); | |
59 | ||
f054996d | 60 | worktree = xcalloc(1, sizeof(*worktree)); |
92718b74 | 61 | worktree->path = strbuf_detach(&worktree_path, NULL); |
92718b74 | 62 | worktree->is_bare = is_bare; |
fa099d23 | 63 | add_head_info(worktree); |
92718b74 | 64 | |
1ceb7f90 | 65 | strbuf_release(&path); |
51934904 | 66 | strbuf_release(&worktree_path); |
51934904 | 67 | return worktree; |
1ceb7f90 MR |
68 | } |
69 | ||
51934904 | 70 | static struct worktree *get_linked_worktree(const char *id) |
ac6c561b | 71 | { |
51934904 | 72 | struct worktree *worktree = NULL; |
ac6c561b | 73 | struct strbuf path = STRBUF_INIT; |
51934904 | 74 | struct strbuf worktree_path = STRBUF_INIT; |
ac6c561b | 75 | |
1ceb7f90 MR |
76 | if (!id) |
77 | die("Missing linked worktree name"); | |
ac6c561b | 78 | |
69dfe3b9 | 79 | strbuf_git_common_path(&path, "worktrees/%s/gitdir", id); |
51934904 MR |
80 | if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0) |
81 | /* invalid gitdir file */ | |
ac6c561b | 82 | goto done; |
51934904 MR |
83 | |
84 | strbuf_rtrim(&worktree_path); | |
85 | if (!strbuf_strip_suffix(&worktree_path, "/.git")) { | |
86 | strbuf_reset(&worktree_path); | |
fd2e7daf | 87 | strbuf_add_absolute_path(&worktree_path, "."); |
51934904 MR |
88 | strbuf_strip_suffix(&worktree_path, "/."); |
89 | } | |
90 | ||
1ceb7f90 | 91 | strbuf_reset(&path); |
51934904 MR |
92 | strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id); |
93 | ||
f054996d | 94 | worktree = xcalloc(1, sizeof(*worktree)); |
92718b74 | 95 | worktree->path = strbuf_detach(&worktree_path, NULL); |
69dfe3b9 | 96 | worktree->id = xstrdup(id); |
fa099d23 | 97 | add_head_info(worktree); |
ac6c561b | 98 | |
ac6c561b MR |
99 | done: |
100 | strbuf_release(&path); | |
51934904 | 101 | strbuf_release(&worktree_path); |
51934904 | 102 | return worktree; |
ac6c561b MR |
103 | } |
104 | ||
750e8a60 NTND |
105 | static void mark_current_worktree(struct worktree **worktrees) |
106 | { | |
0aaad415 | 107 | char *git_dir = absolute_pathdup(get_git_dir()); |
750e8a60 NTND |
108 | int i; |
109 | ||
750e8a60 NTND |
110 | for (i = 0; worktrees[i]; i++) { |
111 | struct worktree *wt = worktrees[i]; | |
360af2da NTND |
112 | const char *wt_git_dir = get_worktree_git_dir(wt); |
113 | ||
114 | if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) { | |
115 | wt->is_current = 1; | |
750e8a60 | 116 | break; |
360af2da | 117 | } |
750e8a60 | 118 | } |
360af2da | 119 | free(git_dir); |
750e8a60 NTND |
120 | } |
121 | ||
4df1d4d4 NTND |
122 | static int compare_worktree(const void *a_, const void *b_) |
123 | { | |
124 | const struct worktree *const *a = a_; | |
125 | const struct worktree *const *b = b_; | |
126 | return fspathcmp((*a)->path, (*b)->path); | |
127 | } | |
128 | ||
4fff1ef7 | 129 | struct worktree **get_worktrees(unsigned flags) |
ac6c561b | 130 | { |
51934904 | 131 | struct worktree **list = NULL; |
ac6c561b MR |
132 | struct strbuf path = STRBUF_INIT; |
133 | DIR *dir; | |
134 | struct dirent *d; | |
51934904 MR |
135 | int counter = 0, alloc = 2; |
136 | ||
3f64699f | 137 | ALLOC_ARRAY(list, alloc); |
ac6c561b | 138 | |
a234563a | 139 | list[counter++] = get_main_worktree(); |
ac6c561b MR |
140 | |
141 | strbuf_addf(&path, "%s/worktrees", get_git_common_dir()); | |
142 | dir = opendir(path.buf); | |
143 | strbuf_release(&path); | |
51934904 MR |
144 | if (dir) { |
145 | while ((d = readdir(dir)) != NULL) { | |
146 | struct worktree *linked = NULL; | |
afb9e30b | 147 | if (is_dot_or_dotdot(d->d_name)) |
51934904 | 148 | continue; |
ac6c561b | 149 | |
d4cddd66 NTND |
150 | if ((linked = get_linked_worktree(d->d_name))) { |
151 | ALLOC_GROW(list, counter + 1, alloc); | |
152 | list[counter++] = linked; | |
153 | } | |
51934904 MR |
154 | } |
155 | closedir(dir); | |
156 | } | |
157 | ALLOC_GROW(list, counter + 1, alloc); | |
158 | list[counter] = NULL; | |
750e8a60 | 159 | |
4df1d4d4 NTND |
160 | if (flags & GWT_SORT_LINKED) |
161 | /* | |
162 | * don't sort the first item (main worktree), which will | |
163 | * always be the first | |
164 | */ | |
165 | QSORT(list + 1, counter - 1, compare_worktree); | |
166 | ||
750e8a60 | 167 | mark_current_worktree(list); |
51934904 MR |
168 | return list; |
169 | } | |
170 | ||
69dfe3b9 NTND |
171 | const char *get_worktree_git_dir(const struct worktree *wt) |
172 | { | |
173 | if (!wt) | |
174 | return get_git_dir(); | |
175 | else if (!wt->id) | |
176 | return get_git_common_dir(); | |
177 | else | |
178 | return git_common_path("worktrees/%s", wt->id); | |
179 | } | |
180 | ||
080739ba NTND |
181 | static struct worktree *find_worktree_by_suffix(struct worktree **list, |
182 | const char *suffix) | |
183 | { | |
184 | struct worktree *found = NULL; | |
185 | int nr_found = 0, suffixlen; | |
186 | ||
187 | suffixlen = strlen(suffix); | |
188 | if (!suffixlen) | |
189 | return NULL; | |
190 | ||
191 | for (; *list && nr_found < 2; list++) { | |
192 | const char *path = (*list)->path; | |
193 | int pathlen = strlen(path); | |
194 | int start = pathlen - suffixlen; | |
195 | ||
196 | /* suffix must start at directory boundary */ | |
197 | if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) && | |
198 | !fspathcmp(suffix, path + start)) { | |
199 | found = *list; | |
200 | nr_found++; | |
201 | } | |
202 | } | |
203 | return nr_found == 1 ? found : NULL; | |
204 | } | |
205 | ||
68353144 NTND |
206 | struct worktree *find_worktree(struct worktree **list, |
207 | const char *prefix, | |
208 | const char *arg) | |
209 | { | |
080739ba | 210 | struct worktree *wt; |
68353144 | 211 | char *path; |
e4da43b1 | 212 | char *to_free = NULL; |
68353144 | 213 | |
080739ba NTND |
214 | if ((wt = find_worktree_by_suffix(list, arg))) |
215 | return wt; | |
216 | ||
e4da43b1 JK |
217 | if (prefix) |
218 | arg = to_free = prefix_filename(prefix, arg); | |
ce83eadd | 219 | path = real_pathdup(arg, 1); |
68353144 NTND |
220 | for (; *list; list++) |
221 | if (!fspathcmp(path, real_path((*list)->path))) | |
222 | break; | |
223 | free(path); | |
e4da43b1 | 224 | free(to_free); |
68353144 NTND |
225 | return *list; |
226 | } | |
227 | ||
984ad9e5 NTND |
228 | int is_main_worktree(const struct worktree *wt) |
229 | { | |
230 | return !wt->id; | |
231 | } | |
232 | ||
346ef530 NTND |
233 | const char *is_worktree_locked(struct worktree *wt) |
234 | { | |
235 | assert(!is_main_worktree(wt)); | |
236 | ||
237 | if (!wt->lock_reason_valid) { | |
238 | struct strbuf path = STRBUF_INIT; | |
239 | ||
240 | strbuf_addstr(&path, worktree_git_path(wt, "locked")); | |
241 | if (file_exists(path.buf)) { | |
242 | struct strbuf lock_reason = STRBUF_INIT; | |
243 | if (strbuf_read_file(&lock_reason, path.buf, 0) < 0) | |
244 | die_errno(_("failed to read '%s'"), path.buf); | |
245 | strbuf_trim(&lock_reason); | |
246 | wt->lock_reason = strbuf_detach(&lock_reason, NULL); | |
247 | } else | |
248 | wt->lock_reason = NULL; | |
249 | wt->lock_reason_valid = 1; | |
250 | strbuf_release(&path); | |
251 | } | |
252 | ||
253 | return wt->lock_reason; | |
254 | } | |
255 | ||
14ace5b7 NTND |
256 | int is_worktree_being_rebased(const struct worktree *wt, |
257 | const char *target) | |
8d9fdd70 NTND |
258 | { |
259 | struct wt_status_state state; | |
260 | int found_rebase; | |
261 | ||
262 | memset(&state, 0, sizeof(state)); | |
263 | found_rebase = wt_status_check_rebase(wt, &state) && | |
264 | ((state.rebase_in_progress || | |
265 | state.rebase_interactive_in_progress) && | |
266 | state.branch && | |
267 | starts_with(target, "refs/heads/") && | |
268 | !strcmp(state.branch, target + strlen("refs/heads/"))); | |
269 | free(state.branch); | |
270 | free(state.onto); | |
271 | return found_rebase; | |
272 | } | |
273 | ||
14ace5b7 NTND |
274 | int is_worktree_being_bisected(const struct worktree *wt, |
275 | const char *target) | |
51934904 | 276 | { |
04a3dfb8 NTND |
277 | struct wt_status_state state; |
278 | int found_rebase; | |
279 | ||
280 | memset(&state, 0, sizeof(state)); | |
281 | found_rebase = wt_status_check_bisect(wt, &state) && | |
282 | state.branch && | |
283 | starts_with(target, "refs/heads/") && | |
284 | !strcmp(state.branch, target + strlen("refs/heads/")); | |
285 | free(state.branch); | |
286 | return found_rebase; | |
287 | } | |
288 | ||
8d9fdd70 NTND |
289 | /* |
290 | * note: this function should be able to detect shared symref even if | |
291 | * HEAD is temporarily detached (e.g. in the middle of rebase or | |
292 | * bisect). New commands that do similar things should update this | |
293 | * function as well. | |
294 | */ | |
d3b9ac07 NTND |
295 | const struct worktree *find_shared_symref(const char *symref, |
296 | const char *target) | |
51934904 | 297 | { |
d3b9ac07 | 298 | const struct worktree *existing = NULL; |
d3b9ac07 | 299 | static struct worktree **worktrees; |
51934904 MR |
300 | int i = 0; |
301 | ||
d3b9ac07 NTND |
302 | if (worktrees) |
303 | free_worktrees(worktrees); | |
4fff1ef7 | 304 | worktrees = get_worktrees(0); |
d3b9ac07 | 305 | |
51934904 | 306 | for (i = 0; worktrees[i]; i++) { |
c8717148 | 307 | struct worktree *wt = worktrees[i]; |
fa099d23 NTND |
308 | const char *symref_target; |
309 | unsigned char sha1[20]; | |
310 | struct ref_store *refs; | |
311 | int flags; | |
312 | ||
171c646f DK |
313 | if (wt->is_bare) |
314 | continue; | |
c8717148 | 315 | |
8d9fdd70 NTND |
316 | if (wt->is_detached && !strcmp(symref, "HEAD")) { |
317 | if (is_worktree_being_rebased(wt, target)) { | |
318 | existing = wt; | |
319 | break; | |
320 | } | |
04a3dfb8 NTND |
321 | if (is_worktree_being_bisected(wt, target)) { |
322 | existing = wt; | |
323 | break; | |
324 | } | |
8d9fdd70 NTND |
325 | } |
326 | ||
fa099d23 NTND |
327 | refs = get_worktree_ref_store(wt); |
328 | symref_target = refs_resolve_ref_unsafe(refs, symref, 0, | |
329 | sha1, &flags); | |
330 | if ((flags & REF_ISSYMREF) && !strcmp(symref_target, target)) { | |
c8717148 | 331 | existing = wt; |
51934904 MR |
332 | break; |
333 | } | |
ac6c561b | 334 | } |
51934904 | 335 | |
ac6c561b MR |
336 | return existing; |
337 | } | |
1a248cf2 SB |
338 | |
339 | int submodule_uses_worktrees(const char *path) | |
340 | { | |
341 | char *submodule_gitdir; | |
342 | struct strbuf sb = STRBUF_INIT; | |
343 | DIR *dir; | |
344 | struct dirent *d; | |
7c4be458 | 345 | int ret = 0; |
1a248cf2 SB |
346 | struct repository_format format; |
347 | ||
348 | submodule_gitdir = git_pathdup_submodule(path, "%s", ""); | |
349 | if (!submodule_gitdir) | |
350 | return 0; | |
351 | ||
352 | /* The env would be set for the superproject. */ | |
353 | get_common_dir_noenv(&sb, submodule_gitdir); | |
d32de66a | 354 | free(submodule_gitdir); |
1a248cf2 SB |
355 | |
356 | /* | |
357 | * The check below is only known to be good for repository format | |
358 | * version 0 at the time of writing this code. | |
359 | */ | |
360 | strbuf_addstr(&sb, "/config"); | |
361 | read_repository_format(&format, sb.buf); | |
362 | if (format.version != 0) { | |
363 | strbuf_release(&sb); | |
364 | return 1; | |
365 | } | |
366 | ||
367 | /* Replace config by worktrees. */ | |
368 | strbuf_setlen(&sb, sb.len - strlen("config")); | |
369 | strbuf_addstr(&sb, "worktrees"); | |
370 | ||
371 | /* See if there is any file inside the worktrees directory. */ | |
372 | dir = opendir(sb.buf); | |
373 | strbuf_release(&sb); | |
1a248cf2 SB |
374 | |
375 | if (!dir) | |
376 | return 0; | |
377 | ||
378 | while ((d = readdir(dir)) != NULL) { | |
379 | if (is_dot_or_dotdot(d->d_name)) | |
380 | continue; | |
381 | ||
382 | ret = 1; | |
383 | break; | |
384 | } | |
385 | closedir(dir); | |
386 | return ret; | |
387 | } |