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