]> git.ipfire.org Git - thirdparty/git.git/blame - worktree.c
alloc.h: move ALLOC_GROW() functions from cache.h
[thirdparty/git.git] / worktree.c
CommitLineData
36bf1958
EN
1#include "git-compat-util.h"
2#include "alloc.h"
b337172c 3#include "repository.h"
ac6c561b
MR
4#include "refs.h"
5#include "strbuf.h"
6#include "worktree.h"
750e8a60 7#include "dir.h"
8d9fdd70 8#include "wt-status.h"
615a84ad 9#include "config.h"
ac6c561b 10
51934904
MR
11void free_worktrees(struct worktree **worktrees)
12{
13 int i = 0;
14
15 for (i = 0; worktrees[i]; i++) {
16 free(worktrees[i]->path);
69dfe3b9 17 free(worktrees[i]->id);
92718b74 18 free(worktrees[i]->head_ref);
346ef530 19 free(worktrees[i]->lock_reason);
fc0c7d5e 20 free(worktrees[i]->prune_reason);
51934904
MR
21 free(worktrees[i]);
22 }
23 free (worktrees);
24}
25
92718b74 26/**
cfaf9f05 27 * Update head_oid, head_ref and is_detached of the given worktree
92718b74 28 */
fa099d23 29static void add_head_info(struct worktree *wt)
92718b74 30{
fa099d23
NTND
31 int flags;
32 const char *target;
33
f1da24ca 34 target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
fa099d23 35 "HEAD",
31824d18 36 0,
ce14de03 37 &wt->head_oid, &flags);
fa099d23
NTND
38 if (!target)
39 return;
40
41 if (flags & REF_ISSYMREF)
42 wt->head_ref = xstrdup(target);
43 else
44 wt->is_detached = 1;
92718b74
MR
45}
46
51934904
MR
47/**
48 * get the main worktree
49 */
50static struct worktree *get_main_worktree(void)
1ceb7f90 51{
51934904 52 struct worktree *worktree = NULL;
51934904 53 struct strbuf worktree_path = STRBUF_INIT;
1ceb7f90 54
918d8ff7
ES
55 strbuf_add_real_path(&worktree_path, get_git_common_dir());
56 strbuf_strip_suffix(&worktree_path, "/.git");
51934904 57
ca56dadb 58 CALLOC_ARRAY(worktree, 1);
92718b74 59 worktree->path = strbuf_detach(&worktree_path, NULL);
f3534c98
JT
60 /*
61 * NEEDSWORK: If this function is called from a secondary worktree and
62 * config.worktree is present, is_bare_repository_cfg will reflect the
63 * contents of config.worktree, not the contents of the main worktree.
64 * This means that worktree->is_bare may be set to 0 even if the main
65 * worktree is configured to be bare.
66 */
67 worktree->is_bare = (is_bare_repository_cfg == 1) ||
68 is_bare_repository();
fa099d23 69 add_head_info(worktree);
51934904 70 return worktree;
1ceb7f90
MR
71}
72
51934904 73static struct worktree *get_linked_worktree(const char *id)
ac6c561b 74{
51934904 75 struct worktree *worktree = NULL;
ac6c561b 76 struct strbuf path = STRBUF_INIT;
51934904 77 struct strbuf worktree_path = STRBUF_INIT;
ac6c561b 78
1ceb7f90
MR
79 if (!id)
80 die("Missing linked worktree name");
ac6c561b 81
b337172c 82 strbuf_git_common_path(&path, the_repository, "worktrees/%s/gitdir", id);
51934904
MR
83 if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
84 /* invalid gitdir file */
ac6c561b 85 goto done;
51934904 86 strbuf_rtrim(&worktree_path);
1c4854ec 87 strbuf_strip_suffix(&worktree_path, "/.git");
51934904 88
ca56dadb 89 CALLOC_ARRAY(worktree, 1);
92718b74 90 worktree->path = strbuf_detach(&worktree_path, NULL);
69dfe3b9 91 worktree->id = xstrdup(id);
fa099d23 92 add_head_info(worktree);
ac6c561b 93
ac6c561b
MR
94done:
95 strbuf_release(&path);
51934904 96 strbuf_release(&worktree_path);
51934904 97 return worktree;
ac6c561b
MR
98}
99
750e8a60
NTND
100static void mark_current_worktree(struct worktree **worktrees)
101{
0aaad415 102 char *git_dir = absolute_pathdup(get_git_dir());
750e8a60
NTND
103 int i;
104
750e8a60
NTND
105 for (i = 0; worktrees[i]; i++) {
106 struct worktree *wt = worktrees[i];
360af2da
NTND
107 const char *wt_git_dir = get_worktree_git_dir(wt);
108
109 if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
110 wt->is_current = 1;
750e8a60 111 break;
360af2da 112 }
750e8a60 113 }
360af2da 114 free(git_dir);
750e8a60
NTND
115}
116
03f2465b 117struct worktree **get_worktrees(void)
ac6c561b 118{
51934904 119 struct worktree **list = NULL;
ac6c561b
MR
120 struct strbuf path = STRBUF_INIT;
121 DIR *dir;
122 struct dirent *d;
51934904
MR
123 int counter = 0, alloc = 2;
124
3f64699f 125 ALLOC_ARRAY(list, alloc);
ac6c561b 126
a234563a 127 list[counter++] = get_main_worktree();
ac6c561b
MR
128
129 strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
130 dir = opendir(path.buf);
131 strbuf_release(&path);
51934904 132 if (dir) {
b548f0f1 133 while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {
51934904 134 struct worktree *linked = NULL;
ac6c561b 135
d4cddd66
NTND
136 if ((linked = get_linked_worktree(d->d_name))) {
137 ALLOC_GROW(list, counter + 1, alloc);
138 list[counter++] = linked;
139 }
51934904
MR
140 }
141 closedir(dir);
142 }
143 ALLOC_GROW(list, counter + 1, alloc);
144 list[counter] = NULL;
750e8a60
NTND
145
146 mark_current_worktree(list);
51934904
MR
147 return list;
148}
149
69dfe3b9
NTND
150const char *get_worktree_git_dir(const struct worktree *wt)
151{
152 if (!wt)
153 return get_git_dir();
154 else if (!wt->id)
155 return get_git_common_dir();
156 else
157 return git_common_path("worktrees/%s", wt->id);
158}
159
080739ba
NTND
160static struct worktree *find_worktree_by_suffix(struct worktree **list,
161 const char *suffix)
162{
163 struct worktree *found = NULL;
164 int nr_found = 0, suffixlen;
165
166 suffixlen = strlen(suffix);
167 if (!suffixlen)
168 return NULL;
169
170 for (; *list && nr_found < 2; list++) {
171 const char *path = (*list)->path;
172 int pathlen = strlen(path);
173 int start = pathlen - suffixlen;
174
175 /* suffix must start at directory boundary */
176 if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) &&
177 !fspathcmp(suffix, path + start)) {
178 found = *list;
179 nr_found++;
180 }
181 }
182 return nr_found == 1 ? found : NULL;
183}
184
68353144
NTND
185struct worktree *find_worktree(struct worktree **list,
186 const char *prefix,
187 const char *arg)
188{
080739ba 189 struct worktree *wt;
e4da43b1 190 char *to_free = NULL;
68353144 191
080739ba
NTND
192 if ((wt = find_worktree_by_suffix(list, arg)))
193 return wt;
194
e4da43b1
JK
195 if (prefix)
196 arg = to_free = prefix_filename(prefix, arg);
bb4995fc
ES
197 wt = find_worktree_by_path(list, arg);
198 free(to_free);
199 return wt;
200}
201
202struct worktree *find_worktree_by_path(struct worktree **list, const char *p)
203{
4530a85b 204 struct strbuf wt_path = STRBUF_INIT;
bb4995fc
ES
205 char *path = real_pathdup(p, 0);
206
207 if (!path)
4c5fa9e6 208 return NULL;
105df73e 209 for (; *list; list++) {
4530a85b
AM
210 if (!strbuf_realpath(&wt_path, (*list)->path, 0))
211 continue;
105df73e 212
4530a85b 213 if (!fspathcmp(path, wt_path.buf))
68353144 214 break;
105df73e 215 }
68353144 216 free(path);
4530a85b 217 strbuf_release(&wt_path);
68353144
NTND
218 return *list;
219}
220
984ad9e5
NTND
221int is_main_worktree(const struct worktree *wt)
222{
223 return !wt->id;
224}
225
d236f12b 226const char *worktree_lock_reason(struct worktree *wt)
346ef530 227{
eb36135a
RS
228 if (is_main_worktree(wt))
229 return NULL;
346ef530
NTND
230
231 if (!wt->lock_reason_valid) {
232 struct strbuf path = STRBUF_INIT;
233
234 strbuf_addstr(&path, worktree_git_path(wt, "locked"));
235 if (file_exists(path.buf)) {
236 struct strbuf lock_reason = STRBUF_INIT;
237 if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
238 die_errno(_("failed to read '%s'"), path.buf);
239 strbuf_trim(&lock_reason);
240 wt->lock_reason = strbuf_detach(&lock_reason, NULL);
241 } else
242 wt->lock_reason = NULL;
243 wt->lock_reason_valid = 1;
244 strbuf_release(&path);
245 }
246
247 return wt->lock_reason;
248}
249
fc0c7d5e
RS
250const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire)
251{
252 struct strbuf reason = STRBUF_INIT;
253 char *path = NULL;
254
255 if (is_main_worktree(wt))
256 return NULL;
257 if (wt->prune_reason_valid)
258 return wt->prune_reason;
259
260 if (should_prune_worktree(wt->id, &reason, &path, expire))
261 wt->prune_reason = strbuf_detach(&reason, NULL);
262 wt->prune_reason_valid = 1;
263
264 strbuf_release(&reason);
265 free(path);
266 return wt->prune_reason;
267}
268
4ddddc1f 269/* convenient wrapper to deal with NULL strbuf */
48ca53ca 270__attribute__((format (printf, 2, 3)))
4ddddc1f
NTND
271static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...)
272{
273 va_list params;
274
275 if (!buf)
276 return;
277
278 va_start(params, fmt);
279 strbuf_vaddf(buf, fmt, params);
280 va_end(params);
281}
282
ee6763af
NTND
283int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
284 unsigned flags)
4ddddc1f
NTND
285{
286 struct strbuf wt_path = STRBUF_INIT;
3d7747e3 287 struct strbuf realpath = STRBUF_INIT;
4ddddc1f
NTND
288 char *path = NULL;
289 int err, ret = -1;
290
291 strbuf_addf(&wt_path, "%s/.git", wt->path);
292
293 if (is_main_worktree(wt)) {
294 if (is_directory(wt_path.buf)) {
295 ret = 0;
296 goto done;
297 }
298 /*
299 * Main worktree using .git file to point to the
300 * repository would make it impossible to know where
301 * the actual worktree is if this function is executed
302 * from another worktree. No .git file support for now.
303 */
304 strbuf_addf_gently(errmsg,
305 _("'%s' at main working tree is not the repository directory"),
306 wt_path.buf);
307 goto done;
308 }
309
310 /*
311 * Make sure "gitdir" file points to a real .git file and that
312 * file points back here.
313 */
314 if (!is_absolute_path(wt->path)) {
315 strbuf_addf_gently(errmsg,
316 _("'%s' file does not contain absolute path to the working tree location"),
317 git_common_path("worktrees/%s/gitdir", wt->id));
318 goto done;
319 }
320
ee6763af
NTND
321 if (flags & WT_VALIDATE_WORKTREE_MISSING_OK &&
322 !file_exists(wt->path)) {
323 ret = 0;
324 goto done;
325 }
326
4ddddc1f
NTND
327 if (!file_exists(wt_path.buf)) {
328 strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf);
329 goto done;
330 }
331
332 path = xstrdup_or_null(read_gitfile_gently(wt_path.buf, &err));
333 if (!path) {
334 strbuf_addf_gently(errmsg, _("'%s' is not a .git file, error code %d"),
335 wt_path.buf, err);
336 goto done;
337 }
338
3d7747e3
AM
339 strbuf_realpath(&realpath, git_common_path("worktrees/%s", wt->id), 1);
340 ret = fspathcmp(path, realpath.buf);
4ddddc1f
NTND
341
342 if (ret)
343 strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"),
344 wt->path, git_common_path("worktrees/%s", wt->id));
345done:
346 free(path);
347 strbuf_release(&wt_path);
3d7747e3 348 strbuf_release(&realpath);
4ddddc1f
NTND
349 return ret;
350}
351
9c620fc7
NTND
352void update_worktree_location(struct worktree *wt, const char *path_)
353{
354 struct strbuf path = STRBUF_INIT;
355
356 if (is_main_worktree(wt))
033abf97 357 BUG("can't relocate main worktree");
9c620fc7
NTND
358
359 strbuf_realpath(&path, path_, 1);
360 if (fspathcmp(wt->path, path.buf)) {
361 write_file(git_common_path("worktrees/%s/gitdir", wt->id),
362 "%s/.git", path.buf);
363 free(wt->path);
364 wt->path = strbuf_detach(&path, NULL);
365 }
366 strbuf_release(&path);
367}
368
14ace5b7
NTND
369int is_worktree_being_rebased(const struct worktree *wt,
370 const char *target)
8d9fdd70
NTND
371{
372 struct wt_status_state state;
373 int found_rebase;
374
375 memset(&state, 0, sizeof(state));
376 found_rebase = wt_status_check_rebase(wt, &state) &&
a46d1f73
377 (state.rebase_in_progress ||
378 state.rebase_interactive_in_progress) &&
379 state.branch &&
380 skip_prefix(target, "refs/heads/", &target) &&
381 !strcmp(state.branch, target);
962dd7eb 382 wt_status_state_free_buffers(&state);
8d9fdd70
NTND
383 return found_rebase;
384}
385
14ace5b7
NTND
386int is_worktree_being_bisected(const struct worktree *wt,
387 const char *target)
51934904 388{
04a3dfb8 389 struct wt_status_state state;
fb07bd42 390 int found_bisect;
04a3dfb8
NTND
391
392 memset(&state, 0, sizeof(state));
fb07bd42
393 found_bisect = wt_status_check_bisect(wt, &state) &&
394 state.branch &&
a46d1f73
395 skip_prefix(target, "refs/heads/", &target) &&
396 !strcmp(state.branch, target);
962dd7eb 397 wt_status_state_free_buffers(&state);
fb07bd42 398 return found_bisect;
04a3dfb8
NTND
399}
400
8d9fdd70
NTND
401/*
402 * note: this function should be able to detect shared symref even if
403 * HEAD is temporarily detached (e.g. in the middle of rebase or
404 * bisect). New commands that do similar things should update this
405 * function as well.
406 */
c8dd491f
AK
407const struct worktree *find_shared_symref(struct worktree **worktrees,
408 const char *symref,
d3b9ac07 409 const char *target)
51934904 410{
d3b9ac07 411 const struct worktree *existing = NULL;
51934904
MR
412 int i = 0;
413
414 for (i = 0; worktrees[i]; i++) {
c8717148 415 struct worktree *wt = worktrees[i];
fa099d23 416 const char *symref_target;
fa099d23
NTND
417 struct ref_store *refs;
418 int flags;
419
171c646f
DK
420 if (wt->is_bare)
421 continue;
c8717148 422
8d9fdd70
NTND
423 if (wt->is_detached && !strcmp(symref, "HEAD")) {
424 if (is_worktree_being_rebased(wt, target)) {
425 existing = wt;
426 break;
427 }
04a3dfb8
NTND
428 if (is_worktree_being_bisected(wt, target)) {
429 existing = wt;
430 break;
431 }
8d9fdd70
NTND
432 }
433
fa099d23 434 refs = get_worktree_ref_store(wt);
f1da24ca 435 symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
ce14de03 436 NULL, &flags);
dbd2b55c
JK
437 if ((flags & REF_ISSYMREF) &&
438 symref_target && !strcmp(symref_target, target)) {
c8717148 439 existing = wt;
51934904
MR
440 break;
441 }
ac6c561b 442 }
51934904 443
ac6c561b
MR
444 return existing;
445}
1a248cf2
SB
446
447int submodule_uses_worktrees(const char *path)
448{
449 char *submodule_gitdir;
e02a7141 450 struct strbuf sb = STRBUF_INIT, err = STRBUF_INIT;
1a248cf2
SB
451 DIR *dir;
452 struct dirent *d;
7c4be458 453 int ret = 0;
e8805af1 454 struct repository_format format = REPOSITORY_FORMAT_INIT;
1a248cf2
SB
455
456 submodule_gitdir = git_pathdup_submodule(path, "%s", "");
457 if (!submodule_gitdir)
458 return 0;
459
460 /* The env would be set for the superproject. */
461 get_common_dir_noenv(&sb, submodule_gitdir);
d32de66a 462 free(submodule_gitdir);
1a248cf2 463
1a248cf2
SB
464 strbuf_addstr(&sb, "/config");
465 read_repository_format(&format, sb.buf);
e02a7141 466 if (verify_repository_format(&format, &err)) {
467 strbuf_release(&err);
1a248cf2 468 strbuf_release(&sb);
e8805af1 469 clear_repository_format(&format);
1a248cf2
SB
470 return 1;
471 }
e8805af1 472 clear_repository_format(&format);
e02a7141 473 strbuf_release(&err);
1a248cf2
SB
474
475 /* Replace config by worktrees. */
476 strbuf_setlen(&sb, sb.len - strlen("config"));
477 strbuf_addstr(&sb, "worktrees");
478
479 /* See if there is any file inside the worktrees directory. */
480 dir = opendir(sb.buf);
481 strbuf_release(&sb);
1a248cf2
SB
482
483 if (!dir)
484 return 0;
485
b548f0f1 486 d = readdir_skip_dot_and_dotdot(dir);
afe8a907 487 if (d)
1a248cf2 488 ret = 1;
1a248cf2
SB
489 closedir(dir);
490 return ret;
491}
d0c39a49 492
ab3e1f78
NTND
493void strbuf_worktree_ref(const struct worktree *wt,
494 struct strbuf *sb,
495 const char *refname)
496{
71e54734
HWN
497 if (parse_worktree_ref(refname, NULL, NULL, NULL) ==
498 REF_WORKTREE_CURRENT &&
499 wt && !wt->is_current) {
500 if (is_main_worktree(wt))
501 strbuf_addstr(sb, "main-worktree/");
502 else
503 strbuf_addf(sb, "worktrees/%s/", wt->id);
ab3e1f78
NTND
504 }
505 strbuf_addstr(sb, refname);
506}
507
d0c39a49
NTND
508int other_head_refs(each_ref_fn fn, void *cb_data)
509{
510 struct worktree **worktrees, **p;
ef2d5547 511 struct strbuf refname = STRBUF_INIT;
d0c39a49
NTND
512 int ret = 0;
513
03f2465b 514 worktrees = get_worktrees();
d0c39a49
NTND
515 for (p = worktrees; *p; p++) {
516 struct worktree *wt = *p;
ab3e1f78
NTND
517 struct object_id oid;
518 int flag;
d0c39a49
NTND
519
520 if (wt->is_current)
521 continue;
522
ef2d5547
523 strbuf_reset(&refname);
524 strbuf_worktree_ref(wt, &refname, "HEAD");
f1da24ca 525 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
76887df0
ÆAB
526 refname.buf,
527 RESOLVE_REF_READING,
ce14de03 528 &oid, &flag))
ef2d5547 529 ret = fn(refname.buf, &oid, flag, cb_data);
d0c39a49
NTND
530 if (ret)
531 break;
532 }
533 free_worktrees(worktrees);
ef2d5547 534 strbuf_release(&refname);
d0c39a49
NTND
535 return ret;
536}
bdd1f3e4
ES
537
538/*
539 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
540 * pointing at <repo>/worktrees/<id>.
541 */
542static void repair_gitfile(struct worktree *wt,
543 worktree_repair_fn fn, void *cb_data)
544{
545 struct strbuf dotgit = STRBUF_INIT;
546 struct strbuf repo = STRBUF_INIT;
547 char *backlink;
548 const char *repair = NULL;
549 int err;
550
551 /* missing worktree can't be repaired */
552 if (!file_exists(wt->path))
553 return;
554
555 if (!is_directory(wt->path)) {
556 fn(1, wt->path, _("not a directory"), cb_data);
557 return;
558 }
559
560 strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
561 strbuf_addf(&dotgit, "%s/.git", wt->path);
562 backlink = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
563
564 if (err == READ_GITFILE_ERR_NOT_A_FILE)
565 fn(1, wt->path, _(".git is not a file"), cb_data);
566 else if (err)
567 repair = _(".git file broken");
568 else if (fspathcmp(backlink, repo.buf))
569 repair = _(".git file incorrect");
570
571 if (repair) {
572 fn(0, wt->path, repair, cb_data);
573 write_file(dotgit.buf, "gitdir: %s", repo.buf);
574 }
575
576 free(backlink);
577 strbuf_release(&repo);
578 strbuf_release(&dotgit);
579}
580
581static void repair_noop(int iserr, const char *path, const char *msg,
582 void *cb_data)
583{
584 /* nothing */
585}
586
587void repair_worktrees(worktree_repair_fn fn, void *cb_data)
588{
589 struct worktree **worktrees = get_worktrees();
590 struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
591
592 if (!fn)
593 fn = repair_noop;
594 for (; *wt; wt++)
595 repair_gitfile(*wt, fn, cb_data);
596 free_worktrees(worktrees);
597}
b214ab5a
ES
598
599static int is_main_worktree_path(const char *path)
600{
601 struct strbuf target = STRBUF_INIT;
602 struct strbuf maindir = STRBUF_INIT;
603 int cmp;
604
605 strbuf_add_real_path(&target, path);
606 strbuf_strip_suffix(&target, "/.git");
607 strbuf_add_real_path(&maindir, get_git_common_dir());
608 strbuf_strip_suffix(&maindir, "/.git");
609 cmp = fspathcmp(maindir.buf, target.buf);
610
611 strbuf_release(&maindir);
612 strbuf_release(&target);
613 return !cmp;
614}
615
cf76baea
ES
616/*
617 * If both the main worktree and linked worktree have been moved, then the
618 * gitfile /path/to/worktree/.git won't point into the repository, thus we
619 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
620 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
621 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
622 */
623static char *infer_backlink(const char *gitfile)
624{
625 struct strbuf actual = STRBUF_INIT;
626 struct strbuf inferred = STRBUF_INIT;
627 const char *id;
628
629 if (strbuf_read_file(&actual, gitfile, 0) < 0)
630 goto error;
631 if (!starts_with(actual.buf, "gitdir:"))
632 goto error;
633 if (!(id = find_last_dir_sep(actual.buf)))
634 goto error;
635 strbuf_trim(&actual);
636 id++; /* advance past '/' to point at <id> */
637 if (!*id)
638 goto error;
639 strbuf_git_common_path(&inferred, the_repository, "worktrees/%s", id);
640 if (!is_directory(inferred.buf))
641 goto error;
642
643 strbuf_release(&actual);
644 return strbuf_detach(&inferred, NULL);
645
646error:
647 strbuf_release(&actual);
648 strbuf_release(&inferred);
649 return NULL;
650}
651
b214ab5a
ES
652/*
653 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
654 * the worktree's path.
655 */
656void repair_worktree_at_path(const char *path,
657 worktree_repair_fn fn, void *cb_data)
658{
659 struct strbuf dotgit = STRBUF_INIT;
660 struct strbuf realdotgit = STRBUF_INIT;
661 struct strbuf gitdir = STRBUF_INIT;
662 struct strbuf olddotgit = STRBUF_INIT;
663 char *backlink = NULL;
664 const char *repair = NULL;
665 int err;
666
667 if (!fn)
668 fn = repair_noop;
669
670 if (is_main_worktree_path(path))
671 goto done;
672
673 strbuf_addf(&dotgit, "%s/.git", path);
674 if (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {
675 fn(1, path, _("not a valid path"), cb_data);
676 goto done;
677 }
678
679 backlink = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));
680 if (err == READ_GITFILE_ERR_NOT_A_FILE) {
681 fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
682 goto done;
cf76baea
ES
683 } else if (err == READ_GITFILE_ERR_NOT_A_REPO) {
684 if (!(backlink = infer_backlink(realdotgit.buf))) {
685 fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
686 goto done;
687 }
b214ab5a
ES
688 } else if (err) {
689 fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
690 goto done;
691 }
692
693 strbuf_addf(&gitdir, "%s/gitdir", backlink);
694 if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
695 repair = _("gitdir unreadable");
696 else {
697 strbuf_rtrim(&olddotgit);
698 if (fspathcmp(olddotgit.buf, realdotgit.buf))
699 repair = _("gitdir incorrect");
700 }
701
702 if (repair) {
703 fn(0, gitdir.buf, repair, cb_data);
704 write_file(gitdir.buf, "%s", realdotgit.buf);
705 }
706done:
707 free(backlink);
708 strbuf_release(&olddotgit);
709 strbuf_release(&gitdir);
710 strbuf_release(&realdotgit);
711 strbuf_release(&dotgit);
712}
a29a8b75
RS
713
714int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire)
715{
716 struct stat st;
717 char *path;
718 int fd;
719 size_t len;
720 ssize_t read_result;
721
722 *wtpath = NULL;
723 if (!is_directory(git_path("worktrees/%s", id))) {
724 strbuf_addstr(reason, _("not a valid directory"));
725 return 1;
726 }
727 if (file_exists(git_path("worktrees/%s/locked", id)))
728 return 0;
729 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
730 strbuf_addstr(reason, _("gitdir file does not exist"));
731 return 1;
732 }
733 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
734 if (fd < 0) {
735 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
736 strerror(errno));
737 return 1;
738 }
739 len = xsize_t(st.st_size);
740 path = xmallocz(len);
741
742 read_result = read_in_full(fd, path, len);
743 if (read_result < 0) {
744 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
745 strerror(errno));
746 close(fd);
747 free(path);
748 return 1;
749 }
750 close(fd);
751
752 if (read_result != len) {
753 strbuf_addf(reason,
754 _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
755 (uintmax_t)len, (uintmax_t)read_result);
756 free(path);
757 return 1;
758 }
759 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
760 len--;
761 if (!len) {
762 strbuf_addstr(reason, _("invalid gitdir file"));
763 free(path);
764 return 1;
765 }
766 path[len] = '\0';
767 if (!file_exists(path)) {
768 if (stat(git_path("worktrees/%s/index", id), &st) ||
769 st.st_mtime <= expire) {
770 strbuf_addstr(reason, _("gitdir file points to non-existent location"));
771 free(path);
772 return 1;
773 } else {
774 *wtpath = path;
775 return 0;
776 }
777 }
778 *wtpath = path;
779 return 0;
780}
615a84ad
DS
781
782static int move_config_setting(const char *key, const char *value,
783 const char *from_file, const char *to_file)
784{
785 if (git_config_set_in_file_gently(to_file, key, value))
786 return error(_("unable to set %s in '%s'"), key, to_file);
787 if (git_config_set_in_file_gently(from_file, key, NULL))
788 return error(_("unable to unset %s in '%s'"), key, from_file);
789 return 0;
790}
791
792int init_worktree_config(struct repository *r)
793{
794 int res = 0;
795 int bare = 0;
796 struct config_set cs = { { 0 } };
797 const char *core_worktree;
798 char *common_config_file;
799 char *main_worktree_file;
800
801 /*
802 * If the extension is already enabled, then we can skip the
803 * upgrade process.
804 */
805 if (repository_format_worktree_config)
806 return 0;
807 if ((res = git_config_set_gently("extensions.worktreeConfig", "true")))
808 return error(_("failed to set extensions.worktreeConfig setting"));
809
810 common_config_file = xstrfmt("%s/config", r->commondir);
811 main_worktree_file = xstrfmt("%s/config.worktree", r->commondir);
812
813 git_configset_init(&cs);
814 git_configset_add_file(&cs, common_config_file);
815
816 /*
817 * If core.bare is true in the common config file, then we need to
818 * move it to the main worktree's config file or it will break all
819 * worktrees. If it is false, then leave it in place because it
820 * _could_ be negating a global core.bare=true.
821 */
822 if (!git_configset_get_bool(&cs, "core.bare", &bare) && bare) {
823 if ((res = move_config_setting("core.bare", "true",
824 common_config_file,
825 main_worktree_file)))
826 goto cleanup;
827 }
828 /*
829 * If core.worktree is set, then the main worktree is located
830 * somewhere different than the parent of the common Git dir.
831 * Relocate that value to avoid breaking all worktrees with this
832 * upgrade to worktree config.
833 */
834 if (!git_configset_get_value(&cs, "core.worktree", &core_worktree)) {
835 if ((res = move_config_setting("core.worktree", core_worktree,
836 common_config_file,
837 main_worktree_file)))
838 goto cleanup;
839 }
840
841 /*
842 * Ensure that we use worktree config for the remaining lifetime
843 * of the current process.
844 */
845 repository_format_worktree_config = 1;
846
847cleanup:
848 git_configset_clear(&cs);
849 free(common_config_file);
850 free(main_worktree_file);
851 return res;
852}