worktree.c: add find_worktree()
[git/git.git] / worktree.c
CommitLineData
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
8void 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);
51934904
MR
16 free(worktrees[i]);
17 }
18 free (worktrees);
19}
20
1ceb7f90
MR
21/*
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 detatched (is not detached).
24 *
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.
28 *
29 * return -1 if the ref is not a proper ref, 0 otherwise (success)
30 */
31static int parse_ref(char *path_to_ref, struct strbuf *ref, int *is_detached)
32{
33 if (is_detached)
34 *is_detached = 0;
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))
39 return -1;
40 } else if (strbuf_read_file(ref, path_to_ref, 0) >= 0) {
41 /* textual symref or detached */
42 if (!starts_with(ref->buf, "ref:")) {
43 if (is_detached)
44 *is_detached = 1;
45 } else {
46 strbuf_remove(ref, 0, strlen("ref:"));
47 strbuf_trim(ref);
48 if (check_refname_format(ref->buf, 0))
49 return -1;
50 }
51 } else
52 return -1;
53 return 0;
54}
55
92718b74
MR
56/**
57 * Add the head_sha1 and head_ref (if not detached) to the given worktree
58 */
59static void add_head_info(struct strbuf *head_ref, struct worktree *worktree)
60{
61 if (head_ref->len) {
62 if (worktree->is_detached) {
63 get_sha1_hex(head_ref->buf, worktree->head_sha1);
64 } else {
65 resolve_ref_unsafe(head_ref->buf, 0, worktree->head_sha1, NULL);
66 worktree->head_ref = strbuf_detach(head_ref, NULL);
67 }
68 }
69}
70
51934904
MR
71/**
72 * get the main worktree
73 */
74static struct worktree *get_main_worktree(void)
1ceb7f90 75{
51934904 76 struct worktree *worktree = NULL;
1ceb7f90 77 struct strbuf path = STRBUF_INIT;
51934904 78 struct strbuf worktree_path = STRBUF_INIT;
51934904 79 struct strbuf head_ref = STRBUF_INIT;
92718b74
MR
80 int is_bare = 0;
81 int is_detached = 0;
1ceb7f90 82
69dfe3b9 83 strbuf_addstr(&worktree_path, absolute_path(get_git_common_dir()));
92718b74
MR
84 is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
85 if (is_bare)
51934904
MR
86 strbuf_strip_suffix(&worktree_path, "/.");
87
88 strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
89
92718b74
MR
90 if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
91 goto done;
92
93 worktree = xmalloc(sizeof(struct worktree));
94 worktree->path = strbuf_detach(&worktree_path, NULL);
69dfe3b9 95 worktree->id = NULL;
92718b74
MR
96 worktree->is_bare = is_bare;
97 worktree->head_ref = NULL;
98 worktree->is_detached = is_detached;
750e8a60 99 worktree->is_current = 0;
92718b74
MR
100 add_head_info(&head_ref, worktree);
101
102done:
1ceb7f90 103 strbuf_release(&path);
51934904
MR
104 strbuf_release(&worktree_path);
105 strbuf_release(&head_ref);
106 return worktree;
1ceb7f90
MR
107}
108
51934904 109static struct worktree *get_linked_worktree(const char *id)
ac6c561b 110{
51934904 111 struct worktree *worktree = NULL;
ac6c561b 112 struct strbuf path = STRBUF_INIT;
51934904 113 struct strbuf worktree_path = STRBUF_INIT;
51934904 114 struct strbuf head_ref = STRBUF_INIT;
92718b74 115 int is_detached = 0;
ac6c561b 116
1ceb7f90
MR
117 if (!id)
118 die("Missing linked worktree name");
ac6c561b 119
69dfe3b9 120 strbuf_git_common_path(&path, "worktrees/%s/gitdir", id);
51934904
MR
121 if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
122 /* invalid gitdir file */
ac6c561b 123 goto done;
51934904
MR
124
125 strbuf_rtrim(&worktree_path);
126 if (!strbuf_strip_suffix(&worktree_path, "/.git")) {
127 strbuf_reset(&worktree_path);
128 strbuf_addstr(&worktree_path, absolute_path("."));
129 strbuf_strip_suffix(&worktree_path, "/.");
130 }
131
1ceb7f90 132 strbuf_reset(&path);
51934904
MR
133 strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id);
134
92718b74
MR
135 if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
136 goto done;
137
138 worktree = xmalloc(sizeof(struct worktree));
139 worktree->path = strbuf_detach(&worktree_path, NULL);
69dfe3b9 140 worktree->id = xstrdup(id);
92718b74
MR
141 worktree->is_bare = 0;
142 worktree->head_ref = NULL;
143 worktree->is_detached = is_detached;
750e8a60 144 worktree->is_current = 0;
92718b74 145 add_head_info(&head_ref, worktree);
ac6c561b 146
ac6c561b
MR
147done:
148 strbuf_release(&path);
51934904
MR
149 strbuf_release(&worktree_path);
150 strbuf_release(&head_ref);
151 return worktree;
ac6c561b
MR
152}
153
750e8a60
NTND
154static void mark_current_worktree(struct worktree **worktrees)
155{
360af2da 156 char *git_dir = xstrdup(absolute_path(get_git_dir()));
750e8a60
NTND
157 int i;
158
750e8a60
NTND
159 for (i = 0; worktrees[i]; i++) {
160 struct worktree *wt = worktrees[i];
360af2da
NTND
161 const char *wt_git_dir = get_worktree_git_dir(wt);
162
163 if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
164 wt->is_current = 1;
750e8a60 165 break;
360af2da 166 }
750e8a60 167 }
360af2da 168 free(git_dir);
750e8a60
NTND
169}
170
51934904 171struct worktree **get_worktrees(void)
ac6c561b 172{
51934904 173 struct worktree **list = NULL;
ac6c561b
MR
174 struct strbuf path = STRBUF_INIT;
175 DIR *dir;
176 struct dirent *d;
51934904
MR
177 int counter = 0, alloc = 2;
178
179 list = xmalloc(alloc * sizeof(struct worktree *));
ac6c561b 180
51934904
MR
181 if ((list[counter] = get_main_worktree()))
182 counter++;
ac6c561b
MR
183
184 strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
185 dir = opendir(path.buf);
186 strbuf_release(&path);
51934904
MR
187 if (dir) {
188 while ((d = readdir(dir)) != NULL) {
189 struct worktree *linked = NULL;
afb9e30b 190 if (is_dot_or_dotdot(d->d_name))
51934904 191 continue;
ac6c561b 192
d4cddd66
NTND
193 if ((linked = get_linked_worktree(d->d_name))) {
194 ALLOC_GROW(list, counter + 1, alloc);
195 list[counter++] = linked;
196 }
51934904
MR
197 }
198 closedir(dir);
199 }
200 ALLOC_GROW(list, counter + 1, alloc);
201 list[counter] = NULL;
750e8a60
NTND
202
203 mark_current_worktree(list);
51934904
MR
204 return list;
205}
206
69dfe3b9
NTND
207const char *get_worktree_git_dir(const struct worktree *wt)
208{
209 if (!wt)
210 return get_git_dir();
211 else if (!wt->id)
212 return get_git_common_dir();
213 else
214 return git_common_path("worktrees/%s", wt->id);
215}
216
68353144
NTND
217struct worktree *find_worktree(struct worktree **list,
218 const char *prefix,
219 const char *arg)
220{
221 char *path;
222
223 arg = prefix_filename(prefix, strlen(prefix), arg);
224 path = xstrdup(real_path(arg));
225 for (; *list; list++)
226 if (!fspathcmp(path, real_path((*list)->path)))
227 break;
228 free(path);
229 return *list;
230}
231
14ace5b7
NTND
232int is_worktree_being_rebased(const struct worktree *wt,
233 const char *target)
8d9fdd70
NTND
234{
235 struct wt_status_state state;
236 int found_rebase;
237
238 memset(&state, 0, sizeof(state));
239 found_rebase = wt_status_check_rebase(wt, &state) &&
240 ((state.rebase_in_progress ||
241 state.rebase_interactive_in_progress) &&
242 state.branch &&
243 starts_with(target, "refs/heads/") &&
244 !strcmp(state.branch, target + strlen("refs/heads/")));
245 free(state.branch);
246 free(state.onto);
247 return found_rebase;
248}
249
14ace5b7
NTND
250int is_worktree_being_bisected(const struct worktree *wt,
251 const char *target)
04a3dfb8
NTND
252{
253 struct wt_status_state state;
254 int found_rebase;
255
256 memset(&state, 0, sizeof(state));
257 found_rebase = wt_status_check_bisect(wt, &state) &&
258 state.branch &&
259 starts_with(target, "refs/heads/") &&
260 !strcmp(state.branch, target + strlen("refs/heads/"));
261 free(state.branch);
262 return found_rebase;
263}
264
8d9fdd70
NTND
265/*
266 * note: this function should be able to detect shared symref even if
267 * HEAD is temporarily detached (e.g. in the middle of rebase or
268 * bisect). New commands that do similar things should update this
269 * function as well.
270 */
d3b9ac07
NTND
271const struct worktree *find_shared_symref(const char *symref,
272 const char *target)
51934904 273{
d3b9ac07 274 const struct worktree *existing = NULL;
51934904
MR
275 struct strbuf path = STRBUF_INIT;
276 struct strbuf sb = STRBUF_INIT;
d3b9ac07 277 static struct worktree **worktrees;
51934904
MR
278 int i = 0;
279
d3b9ac07
NTND
280 if (worktrees)
281 free_worktrees(worktrees);
282 worktrees = get_worktrees();
283
51934904 284 for (i = 0; worktrees[i]; i++) {
c8717148
NTND
285 struct worktree *wt = worktrees[i];
286
8d9fdd70
NTND
287 if (wt->is_detached && !strcmp(symref, "HEAD")) {
288 if (is_worktree_being_rebased(wt, target)) {
289 existing = wt;
290 break;
291 }
04a3dfb8
NTND
292 if (is_worktree_being_bisected(wt, target)) {
293 existing = wt;
294 break;
295 }
8d9fdd70
NTND
296 }
297
51934904
MR
298 strbuf_reset(&path);
299 strbuf_reset(&sb);
69dfe3b9 300 strbuf_addf(&path, "%s/%s",
c8717148 301 get_worktree_git_dir(wt),
69dfe3b9 302 symref);
51934904
MR
303
304 if (parse_ref(path.buf, &sb, NULL)) {
ac6c561b 305 continue;
51934904
MR
306 }
307
308 if (!strcmp(sb.buf, target)) {
c8717148 309 existing = wt;
51934904
MR
310 break;
311 }
ac6c561b 312 }
51934904
MR
313
314 strbuf_release(&path);
315 strbuf_release(&sb);
ac6c561b
MR
316
317 return existing;
318}