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