]> git.ipfire.org Git - thirdparty/git.git/blame - worktree.c
fast-import: use write_pack_header()
[thirdparty/git.git] / worktree.c
CommitLineData
ac6c561b 1#include "cache.h"
b337172c 2#include "repository.h"
ac6c561b
MR
3#include "refs.h"
4#include "strbuf.h"
5#include "worktree.h"
750e8a60 6#include "dir.h"
8d9fdd70 7#include "wt-status.h"
ac6c561b 8
51934904
MR
9void free_worktrees(struct worktree **worktrees)
10{
11 int i = 0;
12
13 for (i = 0; worktrees[i]; i++) {
14 free(worktrees[i]->path);
69dfe3b9 15 free(worktrees[i]->id);
92718b74 16 free(worktrees[i]->head_ref);
346ef530 17 free(worktrees[i]->lock_reason);
51934904
MR
18 free(worktrees[i]);
19 }
20 free (worktrees);
21}
22
92718b74 23/**
fa099d23 24 * Update head_sha1, head_ref and is_detached of the given worktree
92718b74 25 */
fa099d23 26static void add_head_info(struct worktree *wt)
92718b74 27{
fa099d23
NTND
28 int flags;
29 const char *target;
30
31 target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
32 "HEAD",
31824d18 33 0,
49e61479 34 &wt->head_oid, &flags);
fa099d23
NTND
35 if (!target)
36 return;
37
38 if (flags & REF_ISSYMREF)
39 wt->head_ref = xstrdup(target);
40 else
41 wt->is_detached = 1;
92718b74
MR
42}
43
51934904
MR
44/**
45 * get the main worktree
46 */
47static struct worktree *get_main_worktree(void)
1ceb7f90 48{
51934904 49 struct worktree *worktree = NULL;
51934904 50 struct strbuf worktree_path = STRBUF_INIT;
1ceb7f90 51
918d8ff7
ES
52 strbuf_add_real_path(&worktree_path, get_git_common_dir());
53 strbuf_strip_suffix(&worktree_path, "/.git");
51934904 54
f054996d 55 worktree = xcalloc(1, sizeof(*worktree));
92718b74 56 worktree->path = strbuf_detach(&worktree_path, NULL);
f3534c98
JT
57 /*
58 * NEEDSWORK: If this function is called from a secondary worktree and
59 * config.worktree is present, is_bare_repository_cfg will reflect the
60 * contents of config.worktree, not the contents of the main worktree.
61 * This means that worktree->is_bare may be set to 0 even if the main
62 * worktree is configured to be bare.
63 */
64 worktree->is_bare = (is_bare_repository_cfg == 1) ||
65 is_bare_repository();
fa099d23 66 add_head_info(worktree);
51934904 67 return worktree;
1ceb7f90
MR
68}
69
51934904 70static 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
b337172c 79 strbuf_git_common_path(&path, the_repository, "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 83 strbuf_rtrim(&worktree_path);
1c4854ec 84 strbuf_strip_suffix(&worktree_path, "/.git");
51934904 85
f054996d 86 worktree = xcalloc(1, sizeof(*worktree));
92718b74 87 worktree->path = strbuf_detach(&worktree_path, NULL);
69dfe3b9 88 worktree->id = xstrdup(id);
fa099d23 89 add_head_info(worktree);
ac6c561b 90
ac6c561b
MR
91done:
92 strbuf_release(&path);
51934904 93 strbuf_release(&worktree_path);
51934904 94 return worktree;
ac6c561b
MR
95}
96
750e8a60
NTND
97static void mark_current_worktree(struct worktree **worktrees)
98{
0aaad415 99 char *git_dir = absolute_pathdup(get_git_dir());
750e8a60
NTND
100 int i;
101
750e8a60
NTND
102 for (i = 0; worktrees[i]; i++) {
103 struct worktree *wt = worktrees[i];
360af2da
NTND
104 const char *wt_git_dir = get_worktree_git_dir(wt);
105
106 if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
107 wt->is_current = 1;
750e8a60 108 break;
360af2da 109 }
750e8a60 110 }
360af2da 111 free(git_dir);
750e8a60
NTND
112}
113
03f2465b 114struct worktree **get_worktrees(void)
ac6c561b 115{
51934904 116 struct worktree **list = NULL;
ac6c561b
MR
117 struct strbuf path = STRBUF_INIT;
118 DIR *dir;
119 struct dirent *d;
51934904
MR
120 int counter = 0, alloc = 2;
121
3f64699f 122 ALLOC_ARRAY(list, alloc);
ac6c561b 123
a234563a 124 list[counter++] = get_main_worktree();
ac6c561b
MR
125
126 strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
127 dir = opendir(path.buf);
128 strbuf_release(&path);
51934904
MR
129 if (dir) {
130 while ((d = readdir(dir)) != NULL) {
131 struct worktree *linked = NULL;
afb9e30b 132 if (is_dot_or_dotdot(d->d_name))
51934904 133 continue;
ac6c561b 134
d4cddd66
NTND
135 if ((linked = get_linked_worktree(d->d_name))) {
136 ALLOC_GROW(list, counter + 1, alloc);
137 list[counter++] = linked;
138 }
51934904
MR
139 }
140 closedir(dir);
141 }
142 ALLOC_GROW(list, counter + 1, alloc);
143 list[counter] = NULL;
750e8a60
NTND
144
145 mark_current_worktree(list);
51934904
MR
146 return list;
147}
148
69dfe3b9
NTND
149const char *get_worktree_git_dir(const struct worktree *wt)
150{
151 if (!wt)
152 return get_git_dir();
153 else if (!wt->id)
154 return get_git_common_dir();
155 else
156 return git_common_path("worktrees/%s", wt->id);
157}
158
080739ba
NTND
159static struct worktree *find_worktree_by_suffix(struct worktree **list,
160 const char *suffix)
161{
162 struct worktree *found = NULL;
163 int nr_found = 0, suffixlen;
164
165 suffixlen = strlen(suffix);
166 if (!suffixlen)
167 return NULL;
168
169 for (; *list && nr_found < 2; list++) {
170 const char *path = (*list)->path;
171 int pathlen = strlen(path);
172 int start = pathlen - suffixlen;
173
174 /* suffix must start at directory boundary */
175 if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) &&
176 !fspathcmp(suffix, path + start)) {
177 found = *list;
178 nr_found++;
179 }
180 }
181 return nr_found == 1 ? found : NULL;
182}
183
68353144
NTND
184struct worktree *find_worktree(struct worktree **list,
185 const char *prefix,
186 const char *arg)
187{
080739ba 188 struct worktree *wt;
e4da43b1 189 char *to_free = NULL;
68353144 190
080739ba
NTND
191 if ((wt = find_worktree_by_suffix(list, arg)))
192 return wt;
193
e4da43b1
JK
194 if (prefix)
195 arg = to_free = prefix_filename(prefix, arg);
bb4995fc
ES
196 wt = find_worktree_by_path(list, arg);
197 free(to_free);
198 return wt;
199}
200
201struct worktree *find_worktree_by_path(struct worktree **list, const char *p)
202{
4530a85b 203 struct strbuf wt_path = STRBUF_INIT;
bb4995fc
ES
204 char *path = real_pathdup(p, 0);
205
206 if (!path)
4c5fa9e6 207 return NULL;
105df73e 208 for (; *list; list++) {
4530a85b
AM
209 if (!strbuf_realpath(&wt_path, (*list)->path, 0))
210 continue;
105df73e 211
4530a85b 212 if (!fspathcmp(path, wt_path.buf))
68353144 213 break;
105df73e 214 }
68353144 215 free(path);
4530a85b 216 strbuf_release(&wt_path);
68353144
NTND
217 return *list;
218}
219
984ad9e5
NTND
220int is_main_worktree(const struct worktree *wt)
221{
222 return !wt->id;
223}
224
d236f12b 225const char *worktree_lock_reason(struct worktree *wt)
346ef530
NTND
226{
227 assert(!is_main_worktree(wt));
228
229 if (!wt->lock_reason_valid) {
230 struct strbuf path = STRBUF_INIT;
231
232 strbuf_addstr(&path, worktree_git_path(wt, "locked"));
233 if (file_exists(path.buf)) {
234 struct strbuf lock_reason = STRBUF_INIT;
235 if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
236 die_errno(_("failed to read '%s'"), path.buf);
237 strbuf_trim(&lock_reason);
238 wt->lock_reason = strbuf_detach(&lock_reason, NULL);
239 } else
240 wt->lock_reason = NULL;
241 wt->lock_reason_valid = 1;
242 strbuf_release(&path);
243 }
244
245 return wt->lock_reason;
246}
247
4ddddc1f
NTND
248/* convenient wrapper to deal with NULL strbuf */
249static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...)
250{
251 va_list params;
252
253 if (!buf)
254 return;
255
256 va_start(params, fmt);
257 strbuf_vaddf(buf, fmt, params);
258 va_end(params);
259}
260
ee6763af
NTND
261int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
262 unsigned flags)
4ddddc1f
NTND
263{
264 struct strbuf wt_path = STRBUF_INIT;
3d7747e3 265 struct strbuf realpath = STRBUF_INIT;
4ddddc1f
NTND
266 char *path = NULL;
267 int err, ret = -1;
268
269 strbuf_addf(&wt_path, "%s/.git", wt->path);
270
271 if (is_main_worktree(wt)) {
272 if (is_directory(wt_path.buf)) {
273 ret = 0;
274 goto done;
275 }
276 /*
277 * Main worktree using .git file to point to the
278 * repository would make it impossible to know where
279 * the actual worktree is if this function is executed
280 * from another worktree. No .git file support for now.
281 */
282 strbuf_addf_gently(errmsg,
283 _("'%s' at main working tree is not the repository directory"),
284 wt_path.buf);
285 goto done;
286 }
287
288 /*
289 * Make sure "gitdir" file points to a real .git file and that
290 * file points back here.
291 */
292 if (!is_absolute_path(wt->path)) {
293 strbuf_addf_gently(errmsg,
294 _("'%s' file does not contain absolute path to the working tree location"),
295 git_common_path("worktrees/%s/gitdir", wt->id));
296 goto done;
297 }
298
ee6763af
NTND
299 if (flags & WT_VALIDATE_WORKTREE_MISSING_OK &&
300 !file_exists(wt->path)) {
301 ret = 0;
302 goto done;
303 }
304
4ddddc1f
NTND
305 if (!file_exists(wt_path.buf)) {
306 strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf);
307 goto done;
308 }
309
310 path = xstrdup_or_null(read_gitfile_gently(wt_path.buf, &err));
311 if (!path) {
312 strbuf_addf_gently(errmsg, _("'%s' is not a .git file, error code %d"),
313 wt_path.buf, err);
314 goto done;
315 }
316
3d7747e3
AM
317 strbuf_realpath(&realpath, git_common_path("worktrees/%s", wt->id), 1);
318 ret = fspathcmp(path, realpath.buf);
4ddddc1f
NTND
319
320 if (ret)
321 strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"),
322 wt->path, git_common_path("worktrees/%s", wt->id));
323done:
324 free(path);
325 strbuf_release(&wt_path);
3d7747e3 326 strbuf_release(&realpath);
4ddddc1f
NTND
327 return ret;
328}
329
9c620fc7
NTND
330void update_worktree_location(struct worktree *wt, const char *path_)
331{
332 struct strbuf path = STRBUF_INIT;
333
334 if (is_main_worktree(wt))
033abf97 335 BUG("can't relocate main worktree");
9c620fc7
NTND
336
337 strbuf_realpath(&path, path_, 1);
338 if (fspathcmp(wt->path, path.buf)) {
339 write_file(git_common_path("worktrees/%s/gitdir", wt->id),
340 "%s/.git", path.buf);
341 free(wt->path);
342 wt->path = strbuf_detach(&path, NULL);
343 }
344 strbuf_release(&path);
345}
346
14ace5b7
NTND
347int is_worktree_being_rebased(const struct worktree *wt,
348 const char *target)
8d9fdd70
NTND
349{
350 struct wt_status_state state;
351 int found_rebase;
352
353 memset(&state, 0, sizeof(state));
354 found_rebase = wt_status_check_rebase(wt, &state) &&
355 ((state.rebase_in_progress ||
356 state.rebase_interactive_in_progress) &&
357 state.branch &&
358 starts_with(target, "refs/heads/") &&
359 !strcmp(state.branch, target + strlen("refs/heads/")));
360 free(state.branch);
361 free(state.onto);
362 return found_rebase;
363}
364
14ace5b7
NTND
365int is_worktree_being_bisected(const struct worktree *wt,
366 const char *target)
51934904 367{
04a3dfb8
NTND
368 struct wt_status_state state;
369 int found_rebase;
370
371 memset(&state, 0, sizeof(state));
372 found_rebase = wt_status_check_bisect(wt, &state) &&
373 state.branch &&
374 starts_with(target, "refs/heads/") &&
375 !strcmp(state.branch, target + strlen("refs/heads/"));
376 free(state.branch);
377 return found_rebase;
378}
379
8d9fdd70
NTND
380/*
381 * note: this function should be able to detect shared symref even if
382 * HEAD is temporarily detached (e.g. in the middle of rebase or
383 * bisect). New commands that do similar things should update this
384 * function as well.
385 */
d3b9ac07
NTND
386const struct worktree *find_shared_symref(const char *symref,
387 const char *target)
51934904 388{
d3b9ac07 389 const struct worktree *existing = NULL;
d3b9ac07 390 static struct worktree **worktrees;
51934904
MR
391 int i = 0;
392
d3b9ac07
NTND
393 if (worktrees)
394 free_worktrees(worktrees);
03f2465b 395 worktrees = get_worktrees();
d3b9ac07 396
51934904 397 for (i = 0; worktrees[i]; i++) {
c8717148 398 struct worktree *wt = worktrees[i];
fa099d23 399 const char *symref_target;
fa099d23
NTND
400 struct ref_store *refs;
401 int flags;
402
171c646f
DK
403 if (wt->is_bare)
404 continue;
c8717148 405
8d9fdd70
NTND
406 if (wt->is_detached && !strcmp(symref, "HEAD")) {
407 if (is_worktree_being_rebased(wt, target)) {
408 existing = wt;
409 break;
410 }
04a3dfb8
NTND
411 if (is_worktree_being_bisected(wt, target)) {
412 existing = wt;
413 break;
414 }
8d9fdd70
NTND
415 }
416
fa099d23
NTND
417 refs = get_worktree_ref_store(wt);
418 symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
e691b027 419 NULL, &flags);
dbd2b55c
JK
420 if ((flags & REF_ISSYMREF) &&
421 symref_target && !strcmp(symref_target, target)) {
c8717148 422 existing = wt;
51934904
MR
423 break;
424 }
ac6c561b 425 }
51934904 426
ac6c561b
MR
427 return existing;
428}
1a248cf2
SB
429
430int submodule_uses_worktrees(const char *path)
431{
432 char *submodule_gitdir;
e02a7141 433 struct strbuf sb = STRBUF_INIT, err = STRBUF_INIT;
1a248cf2
SB
434 DIR *dir;
435 struct dirent *d;
7c4be458 436 int ret = 0;
e8805af1 437 struct repository_format format = REPOSITORY_FORMAT_INIT;
1a248cf2
SB
438
439 submodule_gitdir = git_pathdup_submodule(path, "%s", "");
440 if (!submodule_gitdir)
441 return 0;
442
443 /* The env would be set for the superproject. */
444 get_common_dir_noenv(&sb, submodule_gitdir);
d32de66a 445 free(submodule_gitdir);
1a248cf2 446
1a248cf2
SB
447 strbuf_addstr(&sb, "/config");
448 read_repository_format(&format, sb.buf);
e02a7141 449 if (verify_repository_format(&format, &err)) {
450 strbuf_release(&err);
1a248cf2 451 strbuf_release(&sb);
e8805af1 452 clear_repository_format(&format);
1a248cf2
SB
453 return 1;
454 }
e8805af1 455 clear_repository_format(&format);
e02a7141 456 strbuf_release(&err);
1a248cf2
SB
457
458 /* Replace config by worktrees. */
459 strbuf_setlen(&sb, sb.len - strlen("config"));
460 strbuf_addstr(&sb, "worktrees");
461
462 /* See if there is any file inside the worktrees directory. */
463 dir = opendir(sb.buf);
464 strbuf_release(&sb);
1a248cf2
SB
465
466 if (!dir)
467 return 0;
468
469 while ((d = readdir(dir)) != NULL) {
470 if (is_dot_or_dotdot(d->d_name))
471 continue;
472
473 ret = 1;
474 break;
475 }
476 closedir(dir);
477 return ret;
478}
d0c39a49 479
3a3b9d8c
NTND
480int parse_worktree_ref(const char *worktree_ref, const char **name,
481 int *name_length, const char **ref)
482{
483 if (skip_prefix(worktree_ref, "main-worktree/", &worktree_ref)) {
484 if (!*worktree_ref)
485 return -1;
486 if (name)
487 *name = NULL;
488 if (name_length)
489 *name_length = 0;
490 if (ref)
491 *ref = worktree_ref;
492 return 0;
493 }
494 if (skip_prefix(worktree_ref, "worktrees/", &worktree_ref)) {
495 const char *slash = strchr(worktree_ref, '/');
496
497 if (!slash || slash == worktree_ref || !slash[1])
498 return -1;
499 if (name)
500 *name = worktree_ref;
501 if (name_length)
502 *name_length = slash - worktree_ref;
503 if (ref)
504 *ref = slash + 1;
505 return 0;
506 }
507 return -1;
508}
509
ab3e1f78
NTND
510void strbuf_worktree_ref(const struct worktree *wt,
511 struct strbuf *sb,
512 const char *refname)
513{
514 switch (ref_type(refname)) {
515 case REF_TYPE_PSEUDOREF:
516 case REF_TYPE_PER_WORKTREE:
517 if (wt && !wt->is_current) {
518 if (is_main_worktree(wt))
519 strbuf_addstr(sb, "main-worktree/");
520 else
521 strbuf_addf(sb, "worktrees/%s/", wt->id);
522 }
523 break;
524
525 case REF_TYPE_MAIN_PSEUDOREF:
526 case REF_TYPE_OTHER_PSEUDOREF:
527 break;
528
529 case REF_TYPE_NORMAL:
530 /*
531 * For shared refs, don't prefix worktrees/ or
532 * main-worktree/. It's not necessary and
533 * files-backend.c can't handle it anyway.
534 */
535 break;
536 }
537 strbuf_addstr(sb, refname);
538}
539
540const char *worktree_ref(const struct worktree *wt, const char *refname)
541{
542 static struct strbuf sb = STRBUF_INIT;
543
544 strbuf_reset(&sb);
545 strbuf_worktree_ref(wt, &sb, refname);
546 return sb.buf;
547}
548
d0c39a49
NTND
549int other_head_refs(each_ref_fn fn, void *cb_data)
550{
551 struct worktree **worktrees, **p;
552 int ret = 0;
553
03f2465b 554 worktrees = get_worktrees();
d0c39a49
NTND
555 for (p = worktrees; *p; p++) {
556 struct worktree *wt = *p;
ab3e1f78
NTND
557 struct object_id oid;
558 int flag;
d0c39a49
NTND
559
560 if (wt->is_current)
561 continue;
562
ab3e1f78
NTND
563 if (!refs_read_ref_full(get_main_ref_store(the_repository),
564 worktree_ref(wt, "HEAD"),
565 RESOLVE_REF_READING,
566 &oid, &flag))
567 ret = fn(worktree_ref(wt, "HEAD"), &oid, flag, cb_data);
d0c39a49
NTND
568 if (ret)
569 break;
570 }
571 free_worktrees(worktrees);
572 return ret;
573}