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