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