wt-status.c: make wt_status_check_rebase() work on any 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"
ac6c561b 6
51934904
MR
7void free_worktrees(struct worktree **worktrees)
8{
9 int i = 0;
10
11 for (i = 0; worktrees[i]; i++) {
12 free(worktrees[i]->path);
69dfe3b9 13 free(worktrees[i]->id);
92718b74 14 free(worktrees[i]->head_ref);
51934904
MR
15 free(worktrees[i]);
16 }
17 free (worktrees);
18}
19
1ceb7f90
MR
20/*
21 * read 'path_to_ref' into 'ref'. Also if is_detached is not NULL,
22 * set is_detached to 1 (0) if the ref is detatched (is not detached).
23 *
24 * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside $GIT_DIR so
25 * for linked worktrees, `resolve_ref_unsafe()` won't work (it uses
26 * git_path). Parse the ref ourselves.
27 *
28 * return -1 if the ref is not a proper ref, 0 otherwise (success)
29 */
30static int parse_ref(char *path_to_ref, struct strbuf *ref, int *is_detached)
31{
32 if (is_detached)
33 *is_detached = 0;
34 if (!strbuf_readlink(ref, path_to_ref, 0)) {
35 /* HEAD is symbolic link */
36 if (!starts_with(ref->buf, "refs/") ||
37 check_refname_format(ref->buf, 0))
38 return -1;
39 } else if (strbuf_read_file(ref, path_to_ref, 0) >= 0) {
40 /* textual symref or detached */
41 if (!starts_with(ref->buf, "ref:")) {
42 if (is_detached)
43 *is_detached = 1;
44 } else {
45 strbuf_remove(ref, 0, strlen("ref:"));
46 strbuf_trim(ref);
47 if (check_refname_format(ref->buf, 0))
48 return -1;
49 }
50 } else
51 return -1;
52 return 0;
53}
54
92718b74
MR
55/**
56 * Add the head_sha1 and head_ref (if not detached) to the given worktree
57 */
58static void add_head_info(struct strbuf *head_ref, struct worktree *worktree)
59{
60 if (head_ref->len) {
61 if (worktree->is_detached) {
62 get_sha1_hex(head_ref->buf, worktree->head_sha1);
63 } else {
64 resolve_ref_unsafe(head_ref->buf, 0, worktree->head_sha1, NULL);
65 worktree->head_ref = strbuf_detach(head_ref, NULL);
66 }
67 }
68}
69
51934904
MR
70/**
71 * get the main worktree
72 */
73static struct worktree *get_main_worktree(void)
1ceb7f90 74{
51934904 75 struct worktree *worktree = NULL;
1ceb7f90 76 struct strbuf path = STRBUF_INIT;
51934904 77 struct strbuf worktree_path = STRBUF_INIT;
51934904 78 struct strbuf head_ref = STRBUF_INIT;
92718b74
MR
79 int is_bare = 0;
80 int is_detached = 0;
1ceb7f90 81
69dfe3b9 82 strbuf_addstr(&worktree_path, absolute_path(get_git_common_dir()));
92718b74
MR
83 is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
84 if (is_bare)
51934904
MR
85 strbuf_strip_suffix(&worktree_path, "/.");
86
87 strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
88
92718b74
MR
89 if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
90 goto done;
91
92 worktree = xmalloc(sizeof(struct worktree));
93 worktree->path = strbuf_detach(&worktree_path, NULL);
69dfe3b9 94 worktree->id = NULL;
92718b74
MR
95 worktree->is_bare = is_bare;
96 worktree->head_ref = NULL;
97 worktree->is_detached = is_detached;
750e8a60 98 worktree->is_current = 0;
92718b74
MR
99 add_head_info(&head_ref, worktree);
100
101done:
1ceb7f90 102 strbuf_release(&path);
51934904
MR
103 strbuf_release(&worktree_path);
104 strbuf_release(&head_ref);
105 return worktree;
1ceb7f90
MR
106}
107
51934904 108static struct worktree *get_linked_worktree(const char *id)
ac6c561b 109{
51934904 110 struct worktree *worktree = NULL;
ac6c561b 111 struct strbuf path = STRBUF_INIT;
51934904 112 struct strbuf worktree_path = STRBUF_INIT;
51934904 113 struct strbuf head_ref = STRBUF_INIT;
92718b74 114 int is_detached = 0;
ac6c561b 115
1ceb7f90
MR
116 if (!id)
117 die("Missing linked worktree name");
ac6c561b 118
69dfe3b9 119 strbuf_git_common_path(&path, "worktrees/%s/gitdir", id);
51934904
MR
120 if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
121 /* invalid gitdir file */
ac6c561b 122 goto done;
51934904
MR
123
124 strbuf_rtrim(&worktree_path);
125 if (!strbuf_strip_suffix(&worktree_path, "/.git")) {
126 strbuf_reset(&worktree_path);
127 strbuf_addstr(&worktree_path, absolute_path("."));
128 strbuf_strip_suffix(&worktree_path, "/.");
129 }
130
1ceb7f90 131 strbuf_reset(&path);
51934904
MR
132 strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id);
133
92718b74
MR
134 if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
135 goto done;
136
137 worktree = xmalloc(sizeof(struct worktree));
138 worktree->path = strbuf_detach(&worktree_path, NULL);
69dfe3b9 139 worktree->id = xstrdup(id);
92718b74
MR
140 worktree->is_bare = 0;
141 worktree->head_ref = NULL;
142 worktree->is_detached = is_detached;
750e8a60 143 worktree->is_current = 0;
92718b74 144 add_head_info(&head_ref, worktree);
ac6c561b 145
ac6c561b
MR
146done:
147 strbuf_release(&path);
51934904
MR
148 strbuf_release(&worktree_path);
149 strbuf_release(&head_ref);
150 return worktree;
ac6c561b
MR
151}
152
750e8a60
NTND
153static void mark_current_worktree(struct worktree **worktrees)
154{
155 struct strbuf git_dir = STRBUF_INIT;
156 struct strbuf path = STRBUF_INIT;
157 int i;
158
159 strbuf_addstr(&git_dir, absolute_path(get_git_dir()));
160 for (i = 0; worktrees[i]; i++) {
161 struct worktree *wt = worktrees[i];
162 strbuf_addstr(&path, absolute_path(get_worktree_git_dir(wt)));
163 wt->is_current = !fspathcmp(git_dir.buf, path.buf);
164 strbuf_reset(&path);
165 if (wt->is_current)
166 break;
167 }
168 strbuf_release(&git_dir);
169 strbuf_release(&path);
170}
171
51934904 172struct worktree **get_worktrees(void)
ac6c561b 173{
51934904 174 struct worktree **list = NULL;
ac6c561b
MR
175 struct strbuf path = STRBUF_INIT;
176 DIR *dir;
177 struct dirent *d;
51934904
MR
178 int counter = 0, alloc = 2;
179
180 list = xmalloc(alloc * sizeof(struct worktree *));
ac6c561b 181
51934904
MR
182 if ((list[counter] = get_main_worktree()))
183 counter++;
ac6c561b
MR
184
185 strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
186 dir = opendir(path.buf);
187 strbuf_release(&path);
51934904
MR
188 if (dir) {
189 while ((d = readdir(dir)) != NULL) {
190 struct worktree *linked = NULL;
191 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
192 continue;
ac6c561b 193
d4cddd66
NTND
194 if ((linked = get_linked_worktree(d->d_name))) {
195 ALLOC_GROW(list, counter + 1, alloc);
196 list[counter++] = linked;
197 }
51934904
MR
198 }
199 closedir(dir);
200 }
201 ALLOC_GROW(list, counter + 1, alloc);
202 list[counter] = NULL;
750e8a60
NTND
203
204 mark_current_worktree(list);
51934904
MR
205 return list;
206}
207
69dfe3b9
NTND
208const char *get_worktree_git_dir(const struct worktree *wt)
209{
210 if (!wt)
211 return get_git_dir();
212 else if (!wt->id)
213 return get_git_common_dir();
214 else
215 return git_common_path("worktrees/%s", wt->id);
216}
217
d3b9ac07
NTND
218const struct worktree *find_shared_symref(const char *symref,
219 const char *target)
51934904 220{
d3b9ac07 221 const struct worktree *existing = NULL;
51934904
MR
222 struct strbuf path = STRBUF_INIT;
223 struct strbuf sb = STRBUF_INIT;
d3b9ac07 224 static struct worktree **worktrees;
51934904
MR
225 int i = 0;
226
d3b9ac07
NTND
227 if (worktrees)
228 free_worktrees(worktrees);
229 worktrees = get_worktrees();
230
51934904
MR
231 for (i = 0; worktrees[i]; i++) {
232 strbuf_reset(&path);
233 strbuf_reset(&sb);
69dfe3b9
NTND
234 strbuf_addf(&path, "%s/%s",
235 get_worktree_git_dir(worktrees[i]),
236 symref);
51934904
MR
237
238 if (parse_ref(path.buf, &sb, NULL)) {
ac6c561b 239 continue;
51934904
MR
240 }
241
242 if (!strcmp(sb.buf, target)) {
d3b9ac07 243 existing = worktrees[i];
51934904
MR
244 break;
245 }
ac6c561b 246 }
51934904
MR
247
248 strbuf_release(&path);
249 strbuf_release(&sb);
ac6c561b
MR
250
251 return existing;
252}