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