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