Line | Count | Source |
1 | | #define DISABLE_SIGN_COMPARE_WARNINGS |
2 | | |
3 | | #include "git-compat-util.h" |
4 | | #include "abspath.h" |
5 | | #include "environment.h" |
6 | | #include "gettext.h" |
7 | | #include "path.h" |
8 | | #include "repository.h" |
9 | | #include "refs.h" |
10 | | #include "setup.h" |
11 | | #include "strbuf.h" |
12 | | #include "worktree.h" |
13 | | #include "dir.h" |
14 | | #include "wt-status.h" |
15 | | #include "config.h" |
16 | | |
17 | | void free_worktree(struct worktree *worktree) |
18 | 0 | { |
19 | 0 | if (!worktree) |
20 | 0 | return; |
21 | 0 | free(worktree->path); |
22 | 0 | free(worktree->id); |
23 | 0 | free(worktree->head_ref); |
24 | 0 | free(worktree->lock_reason); |
25 | 0 | free(worktree->prune_reason); |
26 | 0 | free(worktree); |
27 | 0 | } |
28 | | |
29 | | void free_worktrees(struct worktree **worktrees) |
30 | 0 | { |
31 | 0 | int i = 0; |
32 | 0 | for (i = 0; worktrees[i]; i++) |
33 | 0 | free_worktree(worktrees[i]); |
34 | 0 | free (worktrees); |
35 | 0 | } |
36 | | |
37 | | /** |
38 | | * Update head_oid, head_ref and is_detached of the given worktree |
39 | | */ |
40 | | static void add_head_info(struct worktree *wt) |
41 | 0 | { |
42 | 0 | int flags; |
43 | 0 | const char *target; |
44 | |
|
45 | 0 | target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt), |
46 | 0 | "HEAD", |
47 | 0 | 0, |
48 | 0 | &wt->head_oid, &flags); |
49 | 0 | if (!target) |
50 | 0 | return; |
51 | | |
52 | 0 | if (flags & REF_ISSYMREF) |
53 | 0 | wt->head_ref = xstrdup(target); |
54 | 0 | else |
55 | 0 | wt->is_detached = 1; |
56 | 0 | } |
57 | | |
58 | | static int is_current_worktree(struct worktree *wt) |
59 | 0 | { |
60 | 0 | char *git_dir = absolute_pathdup(repo_get_git_dir(wt->repo)); |
61 | 0 | char *wt_git_dir = get_worktree_git_dir(wt); |
62 | 0 | int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir)); |
63 | 0 | free(wt_git_dir); |
64 | 0 | free(git_dir); |
65 | 0 | return is_current; |
66 | 0 | } |
67 | | |
68 | | struct worktree *get_current_worktree(struct repository *repo) |
69 | 0 | { |
70 | 0 | struct worktree *wt = xcalloc(1, sizeof(*wt)); |
71 | 0 | char *gitdir = absolute_pathdup(repo->gitdir); |
72 | 0 | char *commondir = absolute_pathdup(repo->commondir); |
73 | |
|
74 | 0 | wt->repo = repo; |
75 | 0 | wt->path = absolute_pathdup(repo->worktree ? repo->worktree |
76 | 0 | : repo->gitdir); |
77 | 0 | wt->is_bare = !repo->worktree; |
78 | 0 | if (fspathcmp(gitdir, commondir)) |
79 | 0 | wt->id = xstrdup(find_last_dir_sep(gitdir) + 1); |
80 | 0 | wt->is_current = true; |
81 | 0 | add_head_info(wt); |
82 | |
|
83 | 0 | free(gitdir); |
84 | 0 | free(commondir); |
85 | 0 | return wt; |
86 | 0 | } |
87 | | |
88 | | /* |
89 | | * When in a secondary worktree, and when extensions.worktreeConfig |
90 | | * is true, only $commondir/config and $commondir/worktrees/<id>/ |
91 | | * config.worktree are consulted, hence any core.bare=true setting in |
92 | | * $commondir/config.worktree gets overlooked. Thus, check it manually |
93 | | * to determine if the repository is bare. |
94 | | */ |
95 | | static int is_main_worktree_bare(struct repository *repo) |
96 | 0 | { |
97 | 0 | int bare = 0; |
98 | 0 | struct config_set cs = {0}; |
99 | 0 | char *worktree_config = xstrfmt("%s/config.worktree", repo_get_common_dir(repo)); |
100 | |
|
101 | 0 | git_configset_init(&cs); |
102 | 0 | git_configset_add_file(&cs, worktree_config); |
103 | 0 | git_configset_get_bool(&cs, "core.bare", &bare); |
104 | |
|
105 | 0 | git_configset_clear(&cs); |
106 | 0 | free(worktree_config); |
107 | 0 | return bare; |
108 | 0 | } |
109 | | |
110 | | /** |
111 | | * get the main worktree |
112 | | */ |
113 | | static struct worktree *get_main_worktree(struct repository *repo, |
114 | | int skip_reading_head) |
115 | 0 | { |
116 | 0 | struct worktree *worktree = NULL; |
117 | 0 | struct strbuf worktree_path = STRBUF_INIT; |
118 | |
|
119 | 0 | strbuf_add_real_path(&worktree_path, repo_get_common_dir(repo)); |
120 | 0 | strbuf_strip_suffix(&worktree_path, "/.git"); |
121 | |
|
122 | 0 | CALLOC_ARRAY(worktree, 1); |
123 | 0 | worktree->repo = repo; |
124 | 0 | worktree->path = strbuf_detach(&worktree_path, NULL); |
125 | 0 | worktree->is_current = is_current_worktree(worktree); |
126 | 0 | worktree->is_bare = (repo->bare_cfg == 1) || |
127 | 0 | is_bare_repository(repo) || |
128 | | /* |
129 | | * When in a secondary worktree we have to also verify if the main |
130 | | * worktree is bare in $commondir/config.worktree. |
131 | | * This check is unnecessary if we're currently in the main worktree, |
132 | | * as prior checks already consulted all configs of the current worktree. |
133 | | */ |
134 | 0 | (!worktree->is_current && is_main_worktree_bare(repo)); |
135 | |
|
136 | 0 | if (!skip_reading_head) |
137 | 0 | add_head_info(worktree); |
138 | 0 | return worktree; |
139 | 0 | } |
140 | | |
141 | | struct worktree *get_linked_worktree(struct repository *repo, |
142 | | const char *id, |
143 | | int skip_reading_head) |
144 | 0 | { |
145 | 0 | struct worktree *worktree = NULL; |
146 | 0 | struct strbuf path = STRBUF_INIT; |
147 | 0 | struct strbuf worktree_path = STRBUF_INIT; |
148 | |
|
149 | 0 | if (!id) |
150 | 0 | die("Missing linked worktree name"); |
151 | | |
152 | 0 | repo_common_path_append(repo, &path, "worktrees/%s/gitdir", id); |
153 | 0 | if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0) |
154 | | /* invalid gitdir file */ |
155 | 0 | goto done; |
156 | 0 | strbuf_rtrim(&worktree_path); |
157 | 0 | strbuf_strip_suffix(&worktree_path, "/.git"); |
158 | |
|
159 | 0 | if (!is_absolute_path(worktree_path.buf)) { |
160 | 0 | strbuf_strip_suffix(&path, "gitdir"); |
161 | 0 | strbuf_addbuf(&path, &worktree_path); |
162 | 0 | strbuf_realpath_forgiving(&worktree_path, path.buf, 0); |
163 | 0 | } |
164 | |
|
165 | 0 | CALLOC_ARRAY(worktree, 1); |
166 | 0 | worktree->repo = repo; |
167 | 0 | worktree->path = strbuf_detach(&worktree_path, NULL); |
168 | 0 | worktree->id = xstrdup(id); |
169 | 0 | worktree->is_current = is_current_worktree(worktree); |
170 | 0 | if (!skip_reading_head) |
171 | 0 | add_head_info(worktree); |
172 | |
|
173 | 0 | done: |
174 | 0 | strbuf_release(&path); |
175 | 0 | strbuf_release(&worktree_path); |
176 | 0 | return worktree; |
177 | 0 | } |
178 | | |
179 | | /* |
180 | | * NEEDSWORK: This function exists so that we can look up metadata of a |
181 | | * worktree without trying to access any of its internals like the refdb. It |
182 | | * would be preferable to instead have a corruption-tolerant function for |
183 | | * retrieving worktree metadata that could be used when the worktree is known |
184 | | * to not be in a healthy state, e.g. when creating or repairing it. |
185 | | */ |
186 | | static struct worktree **get_worktrees_internal(struct repository *repo, |
187 | | int skip_reading_head) |
188 | 0 | { |
189 | 0 | struct worktree **list = NULL; |
190 | 0 | struct strbuf path = STRBUF_INIT; |
191 | 0 | DIR *dir; |
192 | 0 | struct dirent *d; |
193 | 0 | int counter = 0, alloc = 2; |
194 | |
|
195 | 0 | ALLOC_ARRAY(list, alloc); |
196 | |
|
197 | 0 | list[counter++] = get_main_worktree(repo, skip_reading_head); |
198 | |
|
199 | 0 | strbuf_addf(&path, "%s/worktrees", repo_get_common_dir(repo)); |
200 | 0 | dir = opendir(path.buf); |
201 | 0 | strbuf_release(&path); |
202 | 0 | if (dir) { |
203 | 0 | while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) { |
204 | 0 | struct worktree *linked = NULL; |
205 | |
|
206 | 0 | if ((linked = get_linked_worktree(repo, d->d_name, skip_reading_head))) { |
207 | 0 | ALLOC_GROW(list, counter + 1, alloc); |
208 | 0 | list[counter++] = linked; |
209 | 0 | } |
210 | 0 | } |
211 | 0 | closedir(dir); |
212 | 0 | } |
213 | 0 | ALLOC_GROW(list, counter + 1, alloc); |
214 | 0 | list[counter] = NULL; |
215 | |
|
216 | 0 | return list; |
217 | 0 | } |
218 | | |
219 | | struct worktree **get_worktrees(struct repository *repo) |
220 | 0 | { |
221 | 0 | return get_worktrees_internal(repo, 0); |
222 | 0 | } |
223 | | |
224 | | struct worktree **get_worktrees_without_reading_head(struct repository *repo) |
225 | 0 | { |
226 | 0 | return get_worktrees_internal(repo, 1); |
227 | 0 | } |
228 | | |
229 | | char *get_worktree_git_dir(const struct worktree *wt) |
230 | 0 | { |
231 | 0 | if (!wt) |
232 | 0 | BUG("%s() called with NULL worktree", __func__); |
233 | 0 | else if (!wt->id) |
234 | 0 | return xstrdup(repo_get_common_dir(wt->repo)); |
235 | 0 | else |
236 | 0 | return repo_common_path(wt->repo, "worktrees/%s", wt->id); |
237 | 0 | } |
238 | | |
239 | | static struct worktree *find_worktree_by_suffix(struct worktree **list, |
240 | | const char *suffix) |
241 | 0 | { |
242 | 0 | struct worktree *found = NULL; |
243 | 0 | int nr_found = 0, suffixlen; |
244 | |
|
245 | 0 | suffixlen = strlen(suffix); |
246 | 0 | if (!suffixlen) |
247 | 0 | return NULL; |
248 | | |
249 | 0 | for (; *list && nr_found < 2; list++) { |
250 | 0 | const char *path = (*list)->path; |
251 | 0 | int pathlen = strlen(path); |
252 | 0 | int start = pathlen - suffixlen; |
253 | | |
254 | | /* suffix must start at directory boundary */ |
255 | 0 | if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) && |
256 | 0 | !fspathcmp(suffix, path + start)) { |
257 | 0 | found = *list; |
258 | 0 | nr_found++; |
259 | 0 | } |
260 | 0 | } |
261 | 0 | return nr_found == 1 ? found : NULL; |
262 | 0 | } |
263 | | |
264 | | struct worktree *find_worktree(struct worktree **list, |
265 | | const char *prefix, |
266 | | const char *arg) |
267 | 0 | { |
268 | 0 | struct worktree *wt; |
269 | 0 | char *to_free = NULL; |
270 | |
|
271 | 0 | if ((wt = find_worktree_by_suffix(list, arg))) |
272 | 0 | return wt; |
273 | | |
274 | 0 | if (prefix) |
275 | 0 | arg = to_free = prefix_filename(prefix, arg); |
276 | 0 | wt = find_worktree_by_path(list, arg); |
277 | 0 | free(to_free); |
278 | 0 | return wt; |
279 | 0 | } |
280 | | |
281 | | struct worktree *find_worktree_by_path(struct worktree **list, const char *p) |
282 | 0 | { |
283 | 0 | struct strbuf wt_path = STRBUF_INIT; |
284 | 0 | char *path = real_pathdup(p, 0); |
285 | |
|
286 | 0 | if (!path) |
287 | 0 | return NULL; |
288 | 0 | for (; *list; list++) { |
289 | 0 | if (!strbuf_realpath(&wt_path, (*list)->path, 0)) |
290 | 0 | continue; |
291 | | |
292 | 0 | if (!fspathcmp(path, wt_path.buf)) |
293 | 0 | break; |
294 | 0 | } |
295 | 0 | free(path); |
296 | 0 | strbuf_release(&wt_path); |
297 | 0 | return *list; |
298 | 0 | } |
299 | | |
300 | | int is_main_worktree(const struct worktree *wt) |
301 | 0 | { |
302 | 0 | return !wt->id; |
303 | 0 | } |
304 | | |
305 | | const char *worktree_lock_reason(struct worktree *wt) |
306 | 0 | { |
307 | 0 | if (is_main_worktree(wt)) |
308 | 0 | return NULL; |
309 | | |
310 | 0 | if (!wt->lock_reason_valid) { |
311 | 0 | struct strbuf path = STRBUF_INIT; |
312 | |
|
313 | 0 | strbuf_addstr(&path, worktree_git_path(wt, "locked")); |
314 | 0 | if (file_exists(path.buf)) { |
315 | 0 | struct strbuf lock_reason = STRBUF_INIT; |
316 | 0 | if (strbuf_read_file(&lock_reason, path.buf, 0) < 0) |
317 | 0 | die_errno(_("failed to read '%s'"), path.buf); |
318 | 0 | strbuf_trim(&lock_reason); |
319 | 0 | wt->lock_reason = strbuf_detach(&lock_reason, NULL); |
320 | 0 | } else |
321 | 0 | wt->lock_reason = NULL; |
322 | 0 | wt->lock_reason_valid = 1; |
323 | 0 | strbuf_release(&path); |
324 | 0 | } |
325 | | |
326 | 0 | return wt->lock_reason; |
327 | 0 | } |
328 | | |
329 | | const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire) |
330 | 0 | { |
331 | 0 | struct strbuf reason = STRBUF_INIT; |
332 | 0 | char *path = NULL; |
333 | |
|
334 | 0 | if (is_main_worktree(wt)) |
335 | 0 | return NULL; |
336 | 0 | if (wt->prune_reason_valid) |
337 | 0 | return wt->prune_reason; |
338 | | |
339 | 0 | if (should_prune_worktree(wt->repo, wt->id, &reason, &path, expire)) |
340 | 0 | wt->prune_reason = strbuf_detach(&reason, NULL); |
341 | 0 | wt->prune_reason_valid = 1; |
342 | |
|
343 | 0 | strbuf_release(&reason); |
344 | 0 | free(path); |
345 | 0 | return wt->prune_reason; |
346 | 0 | } |
347 | | |
348 | | /* convenient wrapper to deal with NULL strbuf */ |
349 | | __attribute__((format (printf, 2, 3))) |
350 | | static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...) |
351 | 0 | { |
352 | 0 | va_list params; |
353 | |
|
354 | 0 | if (!buf) |
355 | 0 | return; |
356 | | |
357 | 0 | va_start(params, fmt); |
358 | 0 | strbuf_vaddf(buf, fmt, params); |
359 | 0 | va_end(params); |
360 | 0 | } |
361 | | |
362 | | int validate_worktree(const struct worktree *wt, struct strbuf *errmsg, |
363 | | unsigned flags) |
364 | 0 | { |
365 | 0 | struct strbuf wt_path = STRBUF_INIT; |
366 | 0 | struct strbuf realpath = STRBUF_INIT; |
367 | 0 | struct strbuf buf = STRBUF_INIT; |
368 | 0 | char *path = NULL; |
369 | 0 | int err, ret = -1; |
370 | |
|
371 | 0 | strbuf_addf(&wt_path, "%s/.git", wt->path); |
372 | |
|
373 | 0 | if (is_main_worktree(wt)) { |
374 | 0 | if (is_directory(wt_path.buf)) { |
375 | 0 | ret = 0; |
376 | 0 | goto done; |
377 | 0 | } |
378 | | /* |
379 | | * Main worktree using .git file to point to the |
380 | | * repository would make it impossible to know where |
381 | | * the actual worktree is if this function is executed |
382 | | * from another worktree. No .git file support for now. |
383 | | */ |
384 | 0 | strbuf_addf_gently(errmsg, |
385 | 0 | _("'%s' at main working tree is not the repository directory"), |
386 | 0 | wt_path.buf); |
387 | 0 | goto done; |
388 | 0 | } |
389 | | |
390 | | /* |
391 | | * Make sure "gitdir" file points to a real .git file and that |
392 | | * file points back here. |
393 | | */ |
394 | 0 | if (!is_absolute_path(wt->path)) { |
395 | 0 | strbuf_addf_gently(errmsg, |
396 | 0 | _("'%s' file does not contain absolute path to the working tree location"), |
397 | 0 | repo_common_path_replace(wt->repo, &buf, "worktrees/%s/gitdir", wt->id)); |
398 | 0 | goto done; |
399 | 0 | } |
400 | | |
401 | 0 | if (flags & WT_VALIDATE_WORKTREE_MISSING_OK && |
402 | 0 | !file_exists(wt->path)) { |
403 | 0 | ret = 0; |
404 | 0 | goto done; |
405 | 0 | } |
406 | | |
407 | 0 | if (!file_exists(wt_path.buf)) { |
408 | 0 | strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf); |
409 | 0 | goto done; |
410 | 0 | } |
411 | | |
412 | 0 | path = xstrdup_or_null(read_gitfile_gently(wt_path.buf, &err)); |
413 | 0 | if (!path) { |
414 | 0 | strbuf_addf_gently(errmsg, _("'%s' is not a .git file, error code %d"), |
415 | 0 | wt_path.buf, err); |
416 | 0 | goto done; |
417 | 0 | } |
418 | | |
419 | 0 | strbuf_realpath(&realpath, repo_common_path_replace(wt->repo, &buf, "worktrees/%s", wt->id), 1); |
420 | 0 | ret = fspathcmp(path, realpath.buf); |
421 | |
|
422 | 0 | if (ret) |
423 | 0 | strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"), |
424 | 0 | wt->path, repo_common_path_replace(wt->repo, &buf, |
425 | 0 | "worktrees/%s", wt->id)); |
426 | 0 | done: |
427 | 0 | free(path); |
428 | 0 | strbuf_release(&buf); |
429 | 0 | strbuf_release(&wt_path); |
430 | 0 | strbuf_release(&realpath); |
431 | 0 | return ret; |
432 | 0 | } |
433 | | |
434 | | void update_worktree_location(struct worktree *wt, const char *path_, |
435 | | int use_relative_paths) |
436 | 0 | { |
437 | 0 | struct strbuf path = STRBUF_INIT; |
438 | 0 | struct strbuf dotgit = STRBUF_INIT; |
439 | 0 | struct strbuf gitdir = STRBUF_INIT; |
440 | 0 | char *wt_gitdir; |
441 | |
|
442 | 0 | if (is_main_worktree(wt)) |
443 | 0 | BUG("can't relocate main worktree"); |
444 | | |
445 | 0 | wt_gitdir = repo_common_path(wt->repo, "worktrees/%s/gitdir", wt->id); |
446 | 0 | strbuf_realpath(&gitdir, wt_gitdir, 1); |
447 | 0 | strbuf_realpath(&path, path_, 1); |
448 | 0 | strbuf_addf(&dotgit, "%s/.git", path.buf); |
449 | 0 | if (fspathcmp(wt->path, path.buf)) { |
450 | 0 | write_worktree_linking_files(wt->repo, dotgit.buf, |
451 | 0 | gitdir.buf, use_relative_paths); |
452 | |
|
453 | 0 | free(wt->path); |
454 | 0 | wt->path = strbuf_detach(&path, NULL); |
455 | 0 | } |
456 | 0 | strbuf_release(&path); |
457 | 0 | strbuf_release(&dotgit); |
458 | 0 | strbuf_release(&gitdir); |
459 | 0 | free(wt_gitdir); |
460 | 0 | } |
461 | | |
462 | | int is_worktree_being_rebased(const struct worktree *wt, |
463 | | const char *target) |
464 | 0 | { |
465 | 0 | struct wt_status_state state; |
466 | 0 | int found_rebase; |
467 | |
|
468 | 0 | memset(&state, 0, sizeof(state)); |
469 | 0 | found_rebase = wt_status_check_rebase(wt, &state) && |
470 | 0 | (state.rebase_in_progress || |
471 | 0 | state.rebase_interactive_in_progress) && |
472 | 0 | state.branch && |
473 | 0 | skip_prefix(target, "refs/heads/", &target) && |
474 | 0 | !strcmp(state.branch, target); |
475 | 0 | wt_status_state_free_buffers(&state); |
476 | 0 | return found_rebase; |
477 | 0 | } |
478 | | |
479 | | int is_worktree_being_bisected(const struct worktree *wt, |
480 | | const char *target) |
481 | 0 | { |
482 | 0 | struct wt_status_state state; |
483 | 0 | int found_bisect; |
484 | |
|
485 | 0 | memset(&state, 0, sizeof(state)); |
486 | 0 | found_bisect = wt_status_check_bisect(wt, &state) && |
487 | 0 | state.bisecting_from && |
488 | 0 | skip_prefix(target, "refs/heads/", &target) && |
489 | 0 | !strcmp(state.bisecting_from, target); |
490 | 0 | wt_status_state_free_buffers(&state); |
491 | 0 | return found_bisect; |
492 | 0 | } |
493 | | |
494 | | /* |
495 | | * note: this function should be able to detect shared symref even if |
496 | | * HEAD is temporarily detached (e.g. in the middle of rebase or |
497 | | * bisect). New commands that do similar things should update this |
498 | | * function as well. |
499 | | */ |
500 | | int is_shared_symref(const struct worktree *wt, const char *symref, |
501 | | const char *target) |
502 | 0 | { |
503 | 0 | const char *symref_target; |
504 | 0 | struct ref_store *refs; |
505 | 0 | int flags; |
506 | |
|
507 | 0 | if (wt->is_bare) |
508 | 0 | return 0; |
509 | | |
510 | 0 | if (wt->is_detached && !strcmp(symref, "HEAD")) { |
511 | 0 | if (is_worktree_being_rebased(wt, target)) |
512 | 0 | return 1; |
513 | 0 | if (is_worktree_being_bisected(wt, target)) |
514 | 0 | return 1; |
515 | 0 | } |
516 | | |
517 | 0 | refs = get_worktree_ref_store(wt); |
518 | 0 | symref_target = refs_resolve_ref_unsafe(refs, symref, 0, |
519 | 0 | NULL, &flags); |
520 | 0 | if ((flags & REF_ISSYMREF) && |
521 | 0 | symref_target && !strcmp(symref_target, target)) |
522 | 0 | return 1; |
523 | | |
524 | 0 | return 0; |
525 | 0 | } |
526 | | |
527 | | const struct worktree *find_shared_symref(struct worktree **worktrees, |
528 | | const char *symref, |
529 | | const char *target) |
530 | 0 | { |
531 | |
|
532 | 0 | for (int i = 0; worktrees[i]; i++) |
533 | 0 | if (is_shared_symref(worktrees[i], symref, target)) |
534 | 0 | return worktrees[i]; |
535 | | |
536 | 0 | return NULL; |
537 | 0 | } |
538 | | |
539 | | int submodule_uses_worktrees(struct repository *repo, |
540 | | const char *path) |
541 | 0 | { |
542 | 0 | char *submodule_gitdir; |
543 | 0 | struct strbuf sb = STRBUF_INIT, err = STRBUF_INIT; |
544 | 0 | DIR *dir; |
545 | 0 | struct dirent *d; |
546 | 0 | int ret = 0; |
547 | 0 | struct repository_format format = REPOSITORY_FORMAT_INIT; |
548 | |
|
549 | 0 | submodule_gitdir = repo_submodule_path(repo, |
550 | 0 | path, "%s", ""); |
551 | 0 | if (!submodule_gitdir) |
552 | 0 | return 0; |
553 | | |
554 | | /* The env would be set for the superproject. */ |
555 | 0 | get_common_dir_noenv(&sb, submodule_gitdir); |
556 | 0 | free(submodule_gitdir); |
557 | |
|
558 | 0 | strbuf_addstr(&sb, "/config"); |
559 | 0 | read_repository_format(&format, sb.buf); |
560 | 0 | if (verify_repository_format(&format, &err)) { |
561 | 0 | strbuf_release(&err); |
562 | 0 | strbuf_release(&sb); |
563 | 0 | clear_repository_format(&format); |
564 | 0 | return 1; |
565 | 0 | } |
566 | 0 | clear_repository_format(&format); |
567 | 0 | strbuf_release(&err); |
568 | | |
569 | | /* Replace config by worktrees. */ |
570 | 0 | strbuf_setlen(&sb, sb.len - strlen("config")); |
571 | 0 | strbuf_addstr(&sb, "worktrees"); |
572 | | |
573 | | /* See if there is any file inside the worktrees directory. */ |
574 | 0 | dir = opendir(sb.buf); |
575 | 0 | strbuf_release(&sb); |
576 | |
|
577 | 0 | if (!dir) |
578 | 0 | return 0; |
579 | | |
580 | 0 | d = readdir_skip_dot_and_dotdot(dir); |
581 | 0 | if (d) |
582 | 0 | ret = 1; |
583 | 0 | closedir(dir); |
584 | 0 | return ret; |
585 | 0 | } |
586 | | |
587 | | void strbuf_worktree_ref(const struct worktree *wt, |
588 | | struct strbuf *sb, |
589 | | const char *refname) |
590 | 0 | { |
591 | 0 | if (parse_worktree_ref(refname, NULL, NULL, NULL) == |
592 | 0 | REF_WORKTREE_CURRENT && |
593 | 0 | wt && !wt->is_current) { |
594 | 0 | if (is_main_worktree(wt)) |
595 | 0 | strbuf_addstr(sb, "main-worktree/"); |
596 | 0 | else |
597 | 0 | strbuf_addf(sb, "worktrees/%s/", wt->id); |
598 | 0 | } |
599 | 0 | strbuf_addstr(sb, refname); |
600 | 0 | } |
601 | | |
602 | | int other_head_refs(struct repository *repo, |
603 | | refs_for_each_cb fn, void *cb_data) |
604 | 0 | { |
605 | 0 | struct worktree **worktrees, **p; |
606 | 0 | struct strbuf refname = STRBUF_INIT; |
607 | 0 | int ret = 0; |
608 | |
|
609 | 0 | worktrees = get_worktrees(repo); |
610 | 0 | for (p = worktrees; *p; p++) { |
611 | 0 | struct worktree *wt = *p; |
612 | 0 | struct object_id oid; |
613 | 0 | int flag; |
614 | |
|
615 | 0 | if (wt->is_current) |
616 | 0 | continue; |
617 | | |
618 | 0 | strbuf_reset(&refname); |
619 | 0 | strbuf_worktree_ref(wt, &refname, "HEAD"); |
620 | 0 | if (refs_resolve_ref_unsafe(get_main_ref_store(repo), |
621 | 0 | refname.buf, |
622 | 0 | RESOLVE_REF_READING, |
623 | 0 | &oid, &flag)) { |
624 | 0 | struct reference ref = { |
625 | 0 | .name = refname.buf, |
626 | 0 | .oid = &oid, |
627 | 0 | .flags = flag, |
628 | 0 | }; |
629 | |
|
630 | 0 | ret = fn(&ref, cb_data); |
631 | 0 | } |
632 | 0 | if (ret) |
633 | 0 | break; |
634 | 0 | } |
635 | 0 | free_worktrees(worktrees); |
636 | 0 | strbuf_release(&refname); |
637 | 0 | return ret; |
638 | 0 | } |
639 | | |
640 | | /* |
641 | | * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not |
642 | | * pointing at <repo>/worktrees/<id>. |
643 | | */ |
644 | | static void repair_gitfile(struct worktree *wt, |
645 | | worktree_repair_fn fn, void *cb_data, |
646 | | int use_relative_paths) |
647 | 0 | { |
648 | 0 | struct strbuf dotgit = STRBUF_INIT; |
649 | 0 | struct strbuf gitdir = STRBUF_INIT; |
650 | 0 | struct strbuf repo = STRBUF_INIT; |
651 | 0 | struct strbuf backlink = STRBUF_INIT; |
652 | 0 | struct strbuf contents = STRBUF_INIT; |
653 | 0 | const char *dotgit_contents = NULL; |
654 | 0 | const char *repair = NULL; |
655 | 0 | char *path = NULL; |
656 | 0 | int err; |
657 | | |
658 | | /* missing worktree can't be repaired */ |
659 | 0 | if (!file_exists(wt->path)) |
660 | 0 | goto done; |
661 | | |
662 | 0 | if (!is_directory(wt->path)) { |
663 | 0 | fn(1, wt->path, _("not a directory"), cb_data); |
664 | 0 | goto done; |
665 | 0 | } |
666 | | |
667 | 0 | path = repo_common_path(wt->repo, "worktrees/%s", wt->id); |
668 | 0 | strbuf_realpath(&repo, path, 1); |
669 | 0 | strbuf_addf(&dotgit, "%s/.git", wt->path); |
670 | 0 | strbuf_addf(&gitdir, "%s/gitdir", repo.buf); |
671 | 0 | err = read_gitfile_raw(&contents, dotgit.buf); |
672 | 0 | if (!err) |
673 | 0 | dotgit_contents = contents.buf; |
674 | |
|
675 | 0 | if (dotgit_contents) { |
676 | 0 | if (is_absolute_path(dotgit_contents)) { |
677 | 0 | strbuf_addstr(&backlink, dotgit_contents); |
678 | 0 | } else { |
679 | 0 | strbuf_addf(&backlink, "%s/%s", wt->path, dotgit_contents); |
680 | 0 | strbuf_realpath_forgiving(&backlink, backlink.buf, 0); |
681 | 0 | } |
682 | 0 | } |
683 | |
|
684 | 0 | if (err == READ_GITFILE_ERR_NOT_A_FILE || |
685 | 0 | err == READ_GITFILE_ERR_IS_A_DIR) |
686 | 0 | fn(1, wt->path, _(".git is not a file"), cb_data); |
687 | 0 | else if (err || !is_git_directory(backlink.buf)) |
688 | 0 | repair = _(".git file broken"); |
689 | 0 | else if (fspathcmp(backlink.buf, repo.buf)) |
690 | 0 | repair = _(".git file incorrect"); |
691 | 0 | else if (use_relative_paths == is_absolute_path(dotgit_contents)) |
692 | 0 | repair = _(".git file absolute/relative path mismatch"); |
693 | |
|
694 | 0 | if (repair) { |
695 | 0 | fn(0, wt->path, repair, cb_data); |
696 | 0 | write_worktree_linking_files(wt->repo, dotgit.buf, |
697 | 0 | gitdir.buf, use_relative_paths); |
698 | 0 | } |
699 | |
|
700 | 0 | done: |
701 | 0 | free(path); |
702 | 0 | strbuf_release(&repo); |
703 | 0 | strbuf_release(&dotgit); |
704 | 0 | strbuf_release(&gitdir); |
705 | 0 | strbuf_release(&backlink); |
706 | 0 | strbuf_release(&contents); |
707 | 0 | } |
708 | | |
709 | | static void repair_noop(int iserr UNUSED, |
710 | | const char *path UNUSED, |
711 | | const char *msg UNUSED, |
712 | | void *cb_data UNUSED) |
713 | 0 | { |
714 | | /* nothing */ |
715 | 0 | } |
716 | | |
717 | | void repair_worktrees(struct repository *repo, worktree_repair_fn fn, |
718 | | void *cb_data, int use_relative_paths) |
719 | 0 | { |
720 | 0 | struct worktree **worktrees = get_worktrees_internal(repo, 1); |
721 | 0 | struct worktree **wt = worktrees + 1; /* +1 skips main worktree */ |
722 | |
|
723 | 0 | if (!fn) |
724 | 0 | fn = repair_noop; |
725 | 0 | for (; *wt; wt++) |
726 | 0 | repair_gitfile(*wt, fn, cb_data, use_relative_paths); |
727 | 0 | free_worktrees(worktrees); |
728 | 0 | } |
729 | | |
730 | | void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path) |
731 | 0 | { |
732 | 0 | struct strbuf gitdir = STRBUF_INIT; |
733 | 0 | struct strbuf dotgit = STRBUF_INIT; |
734 | 0 | int is_relative_path; |
735 | 0 | char *path = NULL; |
736 | |
|
737 | 0 | if (is_main_worktree(wt)) |
738 | 0 | goto done; |
739 | | |
740 | 0 | path = repo_common_path(wt->repo, "worktrees/%s/gitdir", wt->id); |
741 | 0 | strbuf_realpath(&gitdir, path, 1); |
742 | |
|
743 | 0 | if (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0) |
744 | 0 | goto done; |
745 | | |
746 | 0 | strbuf_rtrim(&dotgit); |
747 | 0 | is_relative_path = ! is_absolute_path(dotgit.buf); |
748 | 0 | if (is_relative_path) { |
749 | 0 | strbuf_insertf(&dotgit, 0, "%s/worktrees/%s/", old_path, wt->id); |
750 | 0 | strbuf_realpath_forgiving(&dotgit, dotgit.buf, 0); |
751 | 0 | } |
752 | |
|
753 | 0 | if (!file_exists(dotgit.buf)) |
754 | 0 | goto done; |
755 | | |
756 | 0 | write_worktree_linking_files(wt->repo, dotgit.buf, |
757 | 0 | gitdir.buf, is_relative_path); |
758 | 0 | done: |
759 | 0 | strbuf_release(&gitdir); |
760 | 0 | strbuf_release(&dotgit); |
761 | 0 | free(path); |
762 | 0 | } |
763 | | |
764 | | void repair_worktrees_after_gitdir_move(struct repository *repo, const char *old_path) |
765 | 0 | { |
766 | 0 | struct worktree **worktrees = get_worktrees_internal(repo, 1); |
767 | 0 | struct worktree **wt = worktrees + 1; /* +1 skips main worktree */ |
768 | |
|
769 | 0 | for (; *wt; wt++) |
770 | 0 | repair_worktree_after_gitdir_move(*wt, old_path); |
771 | 0 | free_worktrees(worktrees); |
772 | 0 | } |
773 | | |
774 | | static int is_main_worktree_path(struct repository *repo, const char *path) |
775 | 0 | { |
776 | 0 | struct strbuf target = STRBUF_INIT; |
777 | 0 | struct strbuf maindir = STRBUF_INIT; |
778 | 0 | int cmp; |
779 | |
|
780 | 0 | strbuf_add_real_path(&target, path); |
781 | 0 | strbuf_strip_suffix(&target, "/.git"); |
782 | 0 | strbuf_add_real_path(&maindir, repo_get_common_dir(repo)); |
783 | 0 | strbuf_strip_suffix(&maindir, "/.git"); |
784 | 0 | cmp = fspathcmp(maindir.buf, target.buf); |
785 | |
|
786 | 0 | strbuf_release(&maindir); |
787 | 0 | strbuf_release(&target); |
788 | 0 | return !cmp; |
789 | 0 | } |
790 | | |
791 | | /* |
792 | | * If both the main worktree and linked worktree have been moved, then the |
793 | | * gitfile /path/to/worktree/.git won't point into the repository, thus we |
794 | | * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may |
795 | | * be able to infer the gitdir by manually reading /path/to/worktree/.git, |
796 | | * extracting the <id>, and checking if <repo>/worktrees/<id> exists. |
797 | | * |
798 | | * Returns -1 on failure and strbuf.len on success. |
799 | | */ |
800 | | static ssize_t infer_backlink(struct repository *repo, |
801 | | const char *gitfile, |
802 | | struct strbuf *inferred) |
803 | 0 | { |
804 | 0 | struct strbuf actual = STRBUF_INIT; |
805 | 0 | const char *id; |
806 | |
|
807 | 0 | if (strbuf_read_file(&actual, gitfile, 0) < 0) |
808 | 0 | goto error; |
809 | 0 | if (!starts_with(actual.buf, "gitdir:")) |
810 | 0 | goto error; |
811 | 0 | if (!(id = find_last_dir_sep(actual.buf))) |
812 | 0 | goto error; |
813 | 0 | strbuf_trim(&actual); |
814 | 0 | id++; /* advance past '/' to point at <id> */ |
815 | 0 | if (!*id) |
816 | 0 | goto error; |
817 | 0 | repo_common_path_replace(repo, inferred, "worktrees/%s", id); |
818 | 0 | if (!is_directory(inferred->buf)) |
819 | 0 | goto error; |
820 | | |
821 | 0 | strbuf_release(&actual); |
822 | 0 | return inferred->len; |
823 | 0 | error: |
824 | 0 | strbuf_release(&actual); |
825 | 0 | strbuf_reset(inferred); /* clear invalid path */ |
826 | 0 | return -1; |
827 | 0 | } |
828 | | |
829 | | /* |
830 | | * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at |
831 | | * the worktree's path. |
832 | | */ |
833 | | void repair_worktree_at_path(struct repository *repo, |
834 | | const char *path, |
835 | | worktree_repair_fn fn, void *cb_data, |
836 | | int use_relative_paths) |
837 | 0 | { |
838 | 0 | struct strbuf dotgit = STRBUF_INIT; |
839 | 0 | struct strbuf backlink = STRBUF_INIT; |
840 | 0 | struct strbuf inferred_backlink = STRBUF_INIT; |
841 | 0 | struct strbuf gitdir = STRBUF_INIT; |
842 | 0 | struct strbuf olddotgit = STRBUF_INIT; |
843 | 0 | char *dotgit_contents = NULL; |
844 | 0 | const char *repair = NULL; |
845 | 0 | int err; |
846 | |
|
847 | 0 | if (!fn) |
848 | 0 | fn = repair_noop; |
849 | |
|
850 | 0 | if (is_main_worktree_path(repo, path)) |
851 | 0 | goto done; |
852 | | |
853 | 0 | strbuf_addf(&dotgit, "%s/.git", path); |
854 | 0 | if (!strbuf_realpath(&dotgit, dotgit.buf, 0)) { |
855 | 0 | fn(1, path, _("not a valid path"), cb_data); |
856 | 0 | goto done; |
857 | 0 | } |
858 | | |
859 | 0 | infer_backlink(repo, dotgit.buf, &inferred_backlink); |
860 | 0 | strbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0); |
861 | 0 | dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err)); |
862 | 0 | if (dotgit_contents) { |
863 | 0 | strbuf_addstr(&backlink, dotgit_contents); |
864 | 0 | } else if (err == READ_GITFILE_ERR_NOT_A_FILE || |
865 | 0 | err == READ_GITFILE_ERR_IS_A_DIR) { |
866 | 0 | fn(1, dotgit.buf, _("unable to locate repository; .git is not a file"), cb_data); |
867 | 0 | goto done; |
868 | 0 | } else if (err == READ_GITFILE_ERR_NOT_A_REPO) { |
869 | 0 | if (inferred_backlink.len) { |
870 | | /* |
871 | | * Worktree's .git file does not point at a repository |
872 | | * but we found a .git/worktrees/<id> in this |
873 | | * repository with the same <id> as recorded in the |
874 | | * worktree's .git file so make the worktree point at |
875 | | * the discovered .git/worktrees/<id>. |
876 | | */ |
877 | 0 | strbuf_swap(&backlink, &inferred_backlink); |
878 | 0 | } else { |
879 | 0 | fn(1, dotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data); |
880 | 0 | goto done; |
881 | 0 | } |
882 | 0 | } else { |
883 | 0 | fn(1, dotgit.buf, _("unable to locate repository; .git file broken"), cb_data); |
884 | 0 | goto done; |
885 | 0 | } |
886 | | |
887 | | /* |
888 | | * If we got this far, either the worktree's .git file pointed at a |
889 | | * valid repository (i.e. read_gitfile_gently() returned success) or |
890 | | * the .git file did not point at a repository but we were able to |
891 | | * infer a suitable new value for the .git file by locating a |
892 | | * .git/worktrees/<id> in *this* repository corresponding to the <id> |
893 | | * recorded in the worktree's .git file. |
894 | | * |
895 | | * However, if, at this point, inferred_backlink is non-NULL (i.e. we |
896 | | * found a suitable .git/worktrees/<id> in *this* repository) *and* the |
897 | | * worktree's .git file points at a valid repository *and* those two |
898 | | * paths differ, then that indicates that the user probably *copied* |
899 | | * the main and linked worktrees to a new location as a unit rather |
900 | | * than *moving* them. Thus, the copied worktree's .git file actually |
901 | | * points at the .git/worktrees/<id> in the *original* repository, not |
902 | | * in the "copy" repository. In this case, point the "copy" worktree's |
903 | | * .git file at the "copy" repository. |
904 | | */ |
905 | 0 | if (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf)) |
906 | 0 | strbuf_swap(&backlink, &inferred_backlink); |
907 | |
|
908 | 0 | strbuf_addf(&gitdir, "%s/gitdir", backlink.buf); |
909 | 0 | if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0) |
910 | 0 | repair = _("gitdir unreadable"); |
911 | 0 | else if (use_relative_paths == is_absolute_path(olddotgit.buf)) |
912 | 0 | repair = _("gitdir absolute/relative path mismatch"); |
913 | 0 | else { |
914 | 0 | strbuf_rtrim(&olddotgit); |
915 | 0 | if (!is_absolute_path(olddotgit.buf)) { |
916 | 0 | strbuf_insertf(&olddotgit, 0, "%s/", backlink.buf); |
917 | 0 | strbuf_realpath_forgiving(&olddotgit, olddotgit.buf, 0); |
918 | 0 | } |
919 | 0 | if (fspathcmp(olddotgit.buf, dotgit.buf)) |
920 | 0 | repair = _("gitdir incorrect"); |
921 | 0 | } |
922 | |
|
923 | 0 | if (repair) { |
924 | 0 | fn(0, gitdir.buf, repair, cb_data); |
925 | 0 | write_worktree_linking_files(repo, dotgit.buf, |
926 | 0 | gitdir.buf, use_relative_paths); |
927 | 0 | } |
928 | 0 | done: |
929 | 0 | free(dotgit_contents); |
930 | 0 | strbuf_release(&olddotgit); |
931 | 0 | strbuf_release(&backlink); |
932 | 0 | strbuf_release(&inferred_backlink); |
933 | 0 | strbuf_release(&gitdir); |
934 | 0 | strbuf_release(&dotgit); |
935 | 0 | } |
936 | | |
937 | | int should_prune_worktree(struct repository *repo, |
938 | | const char *id, |
939 | | struct strbuf *reason, |
940 | | char **wtpath, |
941 | | timestamp_t expire) |
942 | 0 | { |
943 | 0 | struct stat st; |
944 | 0 | struct strbuf dotgit = STRBUF_INIT; |
945 | 0 | struct strbuf gitdir = STRBUF_INIT; |
946 | 0 | struct strbuf repo_path = STRBUF_INIT; |
947 | 0 | struct strbuf file = STRBUF_INIT; |
948 | 0 | char *path = NULL; |
949 | 0 | int rc = 0; |
950 | 0 | int fd; |
951 | 0 | size_t len; |
952 | 0 | ssize_t read_result; |
953 | |
|
954 | 0 | *wtpath = NULL; |
955 | |
|
956 | 0 | path = repo_common_path(repo, "worktrees/%s", id); |
957 | 0 | strbuf_realpath(&repo_path, path, 1); |
958 | 0 | FREE_AND_NULL(path); |
959 | |
|
960 | 0 | strbuf_addf(&gitdir, "%s/gitdir", repo_path.buf); |
961 | 0 | if (!is_directory(repo_path.buf)) { |
962 | 0 | strbuf_addstr(reason, _("not a valid directory")); |
963 | 0 | rc = 1; |
964 | 0 | goto done; |
965 | 0 | } |
966 | 0 | strbuf_addf(&file, "%s/locked", repo_path.buf); |
967 | 0 | if (file_exists(file.buf)) { |
968 | 0 | goto done; |
969 | 0 | } |
970 | 0 | if (stat(gitdir.buf, &st)) { |
971 | 0 | strbuf_addstr(reason, _("gitdir file does not exist")); |
972 | 0 | rc = 1; |
973 | 0 | goto done; |
974 | 0 | } |
975 | 0 | fd = open(gitdir.buf, O_RDONLY); |
976 | 0 | if (fd < 0) { |
977 | 0 | strbuf_addf(reason, _("unable to read gitdir file (%s)"), |
978 | 0 | strerror(errno)); |
979 | 0 | rc = 1; |
980 | 0 | goto done; |
981 | 0 | } |
982 | 0 | len = xsize_t(st.st_size); |
983 | 0 | path = xmallocz(len); |
984 | |
|
985 | 0 | read_result = read_in_full(fd, path, len); |
986 | 0 | close(fd); |
987 | 0 | if (read_result < 0) { |
988 | 0 | strbuf_addf(reason, _("unable to read gitdir file (%s)"), |
989 | 0 | strerror(errno)); |
990 | 0 | rc = 1; |
991 | 0 | goto done; |
992 | 0 | } else if (read_result != len) { |
993 | 0 | strbuf_addf(reason, |
994 | 0 | _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"), |
995 | 0 | (uintmax_t)len, (uintmax_t)read_result); |
996 | 0 | rc = 1; |
997 | 0 | goto done; |
998 | 0 | } |
999 | 0 | while (len && (path[len - 1] == '\n' || path[len - 1] == '\r')) |
1000 | 0 | len--; |
1001 | 0 | if (!len) { |
1002 | 0 | strbuf_addstr(reason, _("invalid gitdir file")); |
1003 | 0 | rc = 1; |
1004 | 0 | goto done; |
1005 | 0 | } |
1006 | 0 | path[len] = '\0'; |
1007 | 0 | if (is_absolute_path(path)) { |
1008 | 0 | strbuf_addstr(&dotgit, path); |
1009 | 0 | } else { |
1010 | 0 | strbuf_addf(&dotgit, "%s/%s", repo_path.buf, path); |
1011 | 0 | strbuf_realpath_forgiving(&dotgit, dotgit.buf, 0); |
1012 | 0 | } |
1013 | 0 | if (!file_exists(dotgit.buf)) { |
1014 | 0 | strbuf_reset(&file); |
1015 | 0 | strbuf_addf(&file, "%s/index", repo_path.buf); |
1016 | 0 | if (stat(file.buf, &st) || st.st_mtime <= expire) { |
1017 | 0 | strbuf_addstr(reason, _("gitdir file points to non-existent location")); |
1018 | 0 | rc = 1; |
1019 | 0 | goto done; |
1020 | 0 | } |
1021 | 0 | } |
1022 | 0 | *wtpath = strbuf_detach(&dotgit, NULL); |
1023 | 0 | done: |
1024 | 0 | free(path); |
1025 | 0 | strbuf_release(&dotgit); |
1026 | 0 | strbuf_release(&gitdir); |
1027 | 0 | strbuf_release(&repo_path); |
1028 | 0 | strbuf_release(&file); |
1029 | 0 | return rc; |
1030 | 0 | } |
1031 | | |
1032 | | static int move_config_setting(struct repository *repo, |
1033 | | const char *key, const char *value, |
1034 | | const char *from_file, const char *to_file) |
1035 | 0 | { |
1036 | 0 | if (repo_config_set_in_file_gently(repo, to_file, key, NULL, value)) |
1037 | 0 | return error(_("unable to set %s in '%s'"), key, to_file); |
1038 | 0 | if (repo_config_set_in_file_gently(repo, from_file, key, NULL, NULL)) |
1039 | 0 | return error(_("unable to unset %s in '%s'"), key, from_file); |
1040 | 0 | return 0; |
1041 | 0 | } |
1042 | | |
1043 | | int init_worktree_config(struct repository *r) |
1044 | 0 | { |
1045 | 0 | int res = 0; |
1046 | 0 | int bare = 0; |
1047 | 0 | struct config_set cs = { { 0 } }; |
1048 | 0 | const char *core_worktree; |
1049 | 0 | char *common_config_file; |
1050 | 0 | char *main_worktree_file; |
1051 | | |
1052 | | /* |
1053 | | * If the extension is already enabled, then we can skip the |
1054 | | * upgrade process. |
1055 | | */ |
1056 | 0 | if (r->repository_format_worktree_config) |
1057 | 0 | return 0; |
1058 | 0 | if ((res = repo_config_set_gently(r, "extensions.worktreeConfig", "true"))) |
1059 | 0 | return error(_("failed to set extensions.worktreeConfig setting")); |
1060 | | |
1061 | 0 | common_config_file = xstrfmt("%s/config", r->commondir); |
1062 | 0 | main_worktree_file = xstrfmt("%s/config.worktree", r->commondir); |
1063 | |
|
1064 | 0 | git_configset_init(&cs); |
1065 | 0 | git_configset_add_file(&cs, common_config_file); |
1066 | | |
1067 | | /* |
1068 | | * If core.bare is true in the common config file, then we need to |
1069 | | * move it to the main worktree's config file or it will break all |
1070 | | * worktrees. If it is false, then leave it in place because it |
1071 | | * _could_ be negating a global core.bare=true. |
1072 | | */ |
1073 | 0 | if (!git_configset_get_bool(&cs, "core.bare", &bare) && bare) { |
1074 | 0 | if ((res = move_config_setting(r, "core.bare", "true", |
1075 | 0 | common_config_file, |
1076 | 0 | main_worktree_file))) |
1077 | 0 | goto cleanup; |
1078 | 0 | } |
1079 | | /* |
1080 | | * If core.worktree is set, then the main worktree is located |
1081 | | * somewhere different than the parent of the common Git dir. |
1082 | | * Relocate that value to avoid breaking all worktrees with this |
1083 | | * upgrade to worktree config. |
1084 | | */ |
1085 | 0 | if (!git_configset_get_value(&cs, "core.worktree", &core_worktree, NULL)) { |
1086 | 0 | if ((res = move_config_setting(r, "core.worktree", core_worktree, |
1087 | 0 | common_config_file, |
1088 | 0 | main_worktree_file))) |
1089 | 0 | goto cleanup; |
1090 | 0 | } |
1091 | | |
1092 | | /* |
1093 | | * Ensure that we use worktree config for the remaining lifetime |
1094 | | * of the current process. |
1095 | | */ |
1096 | 0 | r->repository_format_worktree_config = 1; |
1097 | |
|
1098 | 0 | cleanup: |
1099 | 0 | git_configset_clear(&cs); |
1100 | 0 | free(common_config_file); |
1101 | 0 | free(main_worktree_file); |
1102 | 0 | return res; |
1103 | 0 | } |
1104 | | |
1105 | | void write_worktree_linking_files(struct repository *repo, |
1106 | | const char *dotgit, const char *gitdir, |
1107 | | int use_relative_paths) |
1108 | 0 | { |
1109 | 0 | struct strbuf path = STRBUF_INIT; |
1110 | 0 | struct strbuf repo_path = STRBUF_INIT; |
1111 | 0 | struct strbuf tmp = STRBUF_INIT; |
1112 | |
|
1113 | 0 | strbuf_addstr(&path, dotgit); |
1114 | 0 | strbuf_strip_suffix(&path, "/.git"); |
1115 | 0 | strbuf_realpath(&path, path.buf, 1); |
1116 | 0 | strbuf_addstr(&repo_path, gitdir); |
1117 | 0 | strbuf_strip_suffix(&repo_path, "/gitdir"); |
1118 | 0 | strbuf_realpath(&repo_path, repo_path.buf, 1); |
1119 | |
|
1120 | 0 | if (use_relative_paths && !repo->repository_format_relative_worktrees) { |
1121 | 0 | if (upgrade_repository_format(repo, 1) < 0) |
1122 | 0 | die(_("unable to upgrade repository format to support relative worktrees")); |
1123 | 0 | if (repo_config_set_gently(repo, "extensions.relativeWorktrees", "true")) |
1124 | 0 | die(_("unable to set extensions.relativeWorktrees setting")); |
1125 | 0 | repo->repository_format_relative_worktrees = 1; |
1126 | 0 | } |
1127 | | |
1128 | 0 | if (use_relative_paths) { |
1129 | 0 | write_file(gitdir, "%s/.git", relative_path(path.buf, repo_path.buf, &tmp)); |
1130 | 0 | write_file(dotgit, "gitdir: %s", relative_path(repo_path.buf, path.buf, &tmp)); |
1131 | 0 | } else { |
1132 | 0 | write_file(gitdir, "%s/.git", path.buf); |
1133 | 0 | write_file(dotgit, "gitdir: %s", repo_path.buf); |
1134 | 0 | } |
1135 | |
|
1136 | 0 | strbuf_release(&path); |
1137 | 0 | strbuf_release(&repo_path); |
1138 | 0 | strbuf_release(&tmp); |
1139 | 0 | } |