blob: b5ee71c5ebda499899e5f64209611f0484359867 [file] [log] [blame]
Elijah Newren61a7b982023-03-21 06:26:06 +00001#include "git-compat-util.h"
Elijah Newren0b027f62023-03-21 06:25:58 +00002#include "abspath.h"
Elijah Newren36bf1952023-02-24 00:09:24 +00003#include "alloc.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00004#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00005#include "gettext.h"
Brandon Williamsb3371722017-06-22 11:43:37 -07006#include "repository.h"
Michael Rappazzoac6c5612015-10-02 07:55:31 -04007#include "refs.h"
Elijah Newrene38da482023-03-21 06:26:05 +00008#include "setup.h"
Michael Rappazzoac6c5612015-10-02 07:55:31 -04009#include "strbuf.h"
10#include "worktree.h"
Nguyễn Thái Ngọc Duy750e8a62016-04-22 20:01:28 +070011#include "dir.h"
Nguyễn Thái Ngọc Duy8d9fdd72016-04-22 20:01:33 +070012#include "wt-status.h"
Derrick Stolee615a84a2022-02-07 21:32:59 +000013#include "config.h"
Elijah Newrend5ebb502023-03-21 06:26:01 +000014#include "wrapper.h"
Michael Rappazzoac6c5612015-10-02 07:55:31 -040015
Michael Rappazzo51934902015-10-08 13:01:03 -040016void free_worktrees(struct worktree **worktrees)
17{
18 int i = 0;
19
20 for (i = 0; worktrees[i]; i++) {
21 free(worktrees[i]->path);
Nguyễn Thái Ngọc Duy69dfe3b2016-04-22 20:01:26 +070022 free(worktrees[i]->id);
Michael Rappazzo92718b72015-10-08 13:01:04 -040023 free(worktrees[i]->head_ref);
Nguyễn Thái Ngọc Duy346ef532016-06-13 19:18:23 +070024 free(worktrees[i]->lock_reason);
Rafael Silvafc0c7d52021-01-19 22:27:34 +010025 free(worktrees[i]->prune_reason);
Michael Rappazzo51934902015-10-08 13:01:03 -040026 free(worktrees[i]);
27 }
28 free (worktrees);
29}
30
Michael Rappazzo51934902015-10-08 13:01:03 -040031/**
Martin Ågrencfaf9f02020-09-27 15:15:45 +020032 * Update head_oid, head_ref and is_detached of the given worktree
Michael Rappazzo92718b72015-10-08 13:01:04 -040033 */
Nguyễn Thái Ngọc Duyfa099d22017-04-24 17:01:23 +070034static void add_head_info(struct worktree *wt)
Michael Rappazzo92718b72015-10-08 13:01:04 -040035{
Nguyễn Thái Ngọc Duyfa099d22017-04-24 17:01:23 +070036 int flags;
37 const char *target;
38
Ævar Arnfjörð Bjarmasonf1da24c2021-10-16 11:39:27 +020039 target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
Nguyễn Thái Ngọc Duyfa099d22017-04-24 17:01:23 +070040 "HEAD",
Nguyễn Thái Ngọc Duy31824d12017-08-24 17:41:24 +070041 0,
Ævar Arnfjörð Bjarmasonce14de02022-01-26 15:37:01 +010042 &wt->head_oid, &flags);
Nguyễn Thái Ngọc Duyfa099d22017-04-24 17:01:23 +070043 if (!target)
44 return;
45
46 if (flags & REF_ISSYMREF)
47 wt->head_ref = xstrdup(target);
48 else
49 wt->is_detached = 1;
Michael Rappazzo92718b72015-10-08 13:01:04 -040050}
51
52/**
Michael Rappazzo51934902015-10-08 13:01:03 -040053 * get the main worktree
54 */
55static struct worktree *get_main_worktree(void)
Michael Rappazzo1ceb7f92015-10-08 13:01:02 -040056{
Michael Rappazzo51934902015-10-08 13:01:03 -040057 struct worktree *worktree = NULL;
Michael Rappazzo51934902015-10-08 13:01:03 -040058 struct strbuf worktree_path = STRBUF_INIT;
Michael Rappazzo1ceb7f92015-10-08 13:01:02 -040059
Eric Sunshine918d8ff2020-07-31 19:32:14 -040060 strbuf_add_real_path(&worktree_path, get_git_common_dir());
61 strbuf_strip_suffix(&worktree_path, "/.git");
Michael Rappazzo51934902015-10-08 13:01:03 -040062
René Scharfeca56dad2021-03-13 17:17:22 +010063 CALLOC_ARRAY(worktree, 1);
Michael Rappazzo92718b72015-10-08 13:01:04 -040064 worktree->path = strbuf_detach(&worktree_path, NULL);
Jonathan Tanf3534c92019-04-19 10:21:28 -070065 /*
66 * NEEDSWORK: If this function is called from a secondary worktree and
67 * config.worktree is present, is_bare_repository_cfg will reflect the
68 * contents of config.worktree, not the contents of the main worktree.
69 * This means that worktree->is_bare may be set to 0 even if the main
70 * worktree is configured to be bare.
71 */
72 worktree->is_bare = (is_bare_repository_cfg == 1) ||
73 is_bare_repository();
Nguyễn Thái Ngọc Duyfa099d22017-04-24 17:01:23 +070074 add_head_info(worktree);
Michael Rappazzo51934902015-10-08 13:01:03 -040075 return worktree;
Michael Rappazzo1ceb7f92015-10-08 13:01:02 -040076}
77
Michael Rappazzo51934902015-10-08 13:01:03 -040078static struct worktree *get_linked_worktree(const char *id)
Michael Rappazzoac6c5612015-10-02 07:55:31 -040079{
Michael Rappazzo51934902015-10-08 13:01:03 -040080 struct worktree *worktree = NULL;
Michael Rappazzoac6c5612015-10-02 07:55:31 -040081 struct strbuf path = STRBUF_INIT;
Michael Rappazzo51934902015-10-08 13:01:03 -040082 struct strbuf worktree_path = STRBUF_INIT;
Michael Rappazzoac6c5612015-10-02 07:55:31 -040083
Michael Rappazzo1ceb7f92015-10-08 13:01:02 -040084 if (!id)
85 die("Missing linked worktree name");
Michael Rappazzoac6c5612015-10-02 07:55:31 -040086
Brandon Williamsb3371722017-06-22 11:43:37 -070087 strbuf_git_common_path(&path, the_repository, "worktrees/%s/gitdir", id);
Michael Rappazzo51934902015-10-08 13:01:03 -040088 if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
89 /* invalid gitdir file */
90 goto done;
Michael Rappazzo51934902015-10-08 13:01:03 -040091 strbuf_rtrim(&worktree_path);
Eric Sunshine1c4854e2020-07-31 19:32:13 -040092 strbuf_strip_suffix(&worktree_path, "/.git");
Michael Rappazzo51934902015-10-08 13:01:03 -040093
René Scharfeca56dad2021-03-13 17:17:22 +010094 CALLOC_ARRAY(worktree, 1);
Michael Rappazzo92718b72015-10-08 13:01:04 -040095 worktree->path = strbuf_detach(&worktree_path, NULL);
Nguyễn Thái Ngọc Duy69dfe3b2016-04-22 20:01:26 +070096 worktree->id = xstrdup(id);
Nguyễn Thái Ngọc Duyfa099d22017-04-24 17:01:23 +070097 add_head_info(worktree);
Michael Rappazzo51934902015-10-08 13:01:03 -040098
Michael Rappazzoac6c5612015-10-02 07:55:31 -040099done:
100 strbuf_release(&path);
Michael Rappazzo51934902015-10-08 13:01:03 -0400101 strbuf_release(&worktree_path);
Michael Rappazzo51934902015-10-08 13:01:03 -0400102 return worktree;
Michael Rappazzoac6c5612015-10-02 07:55:31 -0400103}
104
Nguyễn Thái Ngọc Duy750e8a62016-04-22 20:01:28 +0700105static void mark_current_worktree(struct worktree **worktrees)
106{
René Scharfe0aaad412017-01-26 18:54:23 +0100107 char *git_dir = absolute_pathdup(get_git_dir());
Nguyễn Thái Ngọc Duy750e8a62016-04-22 20:01:28 +0700108 int i;
109
Nguyễn Thái Ngọc Duy750e8a62016-04-22 20:01:28 +0700110 for (i = 0; worktrees[i]; i++) {
111 struct worktree *wt = worktrees[i];
Nguyễn Thái Ngọc Duy360af2d2016-05-22 16:33:52 +0700112 const char *wt_git_dir = get_worktree_git_dir(wt);
113
114 if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
115 wt->is_current = 1;
Nguyễn Thái Ngọc Duy750e8a62016-04-22 20:01:28 +0700116 break;
Nguyễn Thái Ngọc Duy360af2d2016-05-22 16:33:52 +0700117 }
Nguyễn Thái Ngọc Duy750e8a62016-04-22 20:01:28 +0700118 }
Nguyễn Thái Ngọc Duy360af2d2016-05-22 16:33:52 +0700119 free(git_dir);
Nguyễn Thái Ngọc Duy750e8a62016-04-22 20:01:28 +0700120}
121
Eric Sunshine03f24652020-06-19 19:35:44 -0400122struct worktree **get_worktrees(void)
Michael Rappazzoac6c5612015-10-02 07:55:31 -0400123{
Michael Rappazzo51934902015-10-08 13:01:03 -0400124 struct worktree **list = NULL;
Michael Rappazzoac6c5612015-10-02 07:55:31 -0400125 struct strbuf path = STRBUF_INIT;
126 DIR *dir;
127 struct dirent *d;
Michael Rappazzo51934902015-10-08 13:01:03 -0400128 int counter = 0, alloc = 2;
Michael Rappazzoac6c5612015-10-02 07:55:31 -0400129
René Scharfe3f646992017-02-25 11:30:03 +0100130 ALLOC_ARRAY(list, alloc);
Michael Rappazzo51934902015-10-08 13:01:03 -0400131
Nguyễn Thái Ngọc Duya2345632016-11-28 16:36:54 +0700132 list[counter++] = get_main_worktree();
Michael Rappazzoac6c5612015-10-02 07:55:31 -0400133
134 strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
135 dir = opendir(path.buf);
136 strbuf_release(&path);
Michael Rappazzo51934902015-10-08 13:01:03 -0400137 if (dir) {
Elijah Newrenb548f0f2021-05-12 17:28:22 +0000138 while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {
Michael Rappazzo51934902015-10-08 13:01:03 -0400139 struct worktree *linked = NULL;
Michael Rappazzoac6c5612015-10-02 07:55:31 -0400140
Nguyễn Thái Ngọc Duyd4cddd62016-01-18 18:21:29 +0700141 if ((linked = get_linked_worktree(d->d_name))) {
142 ALLOC_GROW(list, counter + 1, alloc);
143 list[counter++] = linked;
144 }
Michael Rappazzo51934902015-10-08 13:01:03 -0400145 }
146 closedir(dir);
Michael Rappazzoac6c5612015-10-02 07:55:31 -0400147 }
Michael Rappazzo51934902015-10-08 13:01:03 -0400148 ALLOC_GROW(list, counter + 1, alloc);
149 list[counter] = NULL;
Nguyễn Thái Ngọc Duy750e8a62016-04-22 20:01:28 +0700150
151 mark_current_worktree(list);
Michael Rappazzo51934902015-10-08 13:01:03 -0400152 return list;
153}
154
Nguyễn Thái Ngọc Duy69dfe3b2016-04-22 20:01:26 +0700155const char *get_worktree_git_dir(const struct worktree *wt)
Michael Rappazzo51934902015-10-08 13:01:03 -0400156{
Nguyễn Thái Ngọc Duy69dfe3b2016-04-22 20:01:26 +0700157 if (!wt)
158 return get_git_dir();
159 else if (!wt->id)
160 return get_git_common_dir();
161 else
162 return git_common_path("worktrees/%s", wt->id);
163}
164
Nguyễn Thái Ngọc Duy080739b2016-06-13 19:18:26 +0700165static struct worktree *find_worktree_by_suffix(struct worktree **list,
166 const char *suffix)
167{
168 struct worktree *found = NULL;
169 int nr_found = 0, suffixlen;
170
171 suffixlen = strlen(suffix);
172 if (!suffixlen)
173 return NULL;
174
175 for (; *list && nr_found < 2; list++) {
176 const char *path = (*list)->path;
177 int pathlen = strlen(path);
178 int start = pathlen - suffixlen;
179
180 /* suffix must start at directory boundary */
181 if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) &&
182 !fspathcmp(suffix, path + start)) {
183 found = *list;
184 nr_found++;
185 }
186 }
187 return nr_found == 1 ? found : NULL;
188}
189
Nguyễn Thái Ngọc Duy68353142016-06-03 19:19:39 +0700190struct worktree *find_worktree(struct worktree **list,
191 const char *prefix,
192 const char *arg)
193{
Nguyễn Thái Ngọc Duy080739b2016-06-13 19:18:26 +0700194 struct worktree *wt;
Jeff Kinge4da43b2017-03-20 21:28:49 -0400195 char *to_free = NULL;
Nguyễn Thái Ngọc Duy68353142016-06-03 19:19:39 +0700196
Nguyễn Thái Ngọc Duy080739b2016-06-13 19:18:26 +0700197 if ((wt = find_worktree_by_suffix(list, arg)))
198 return wt;
199
Jeff Kinge4da43b2017-03-20 21:28:49 -0400200 if (prefix)
201 arg = to_free = prefix_filename(prefix, arg);
Eric Sunshinebb4995f2020-02-24 04:08:47 -0500202 wt = find_worktree_by_path(list, arg);
203 free(to_free);
204 return wt;
205}
206
207struct worktree *find_worktree_by_path(struct worktree **list, const char *p)
208{
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000209 struct strbuf wt_path = STRBUF_INIT;
Eric Sunshinebb4995f2020-02-24 04:08:47 -0500210 char *path = real_pathdup(p, 0);
211
212 if (!path)
Eric Sunshine4c5fa9e2018-08-28 17:20:18 -0400213 return NULL;
Nguyễn Thái Ngọc Duy105df732019-05-13 17:49:44 +0700214 for (; *list; list++) {
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000215 if (!strbuf_realpath(&wt_path, (*list)->path, 0))
216 continue;
Nguyễn Thái Ngọc Duy105df732019-05-13 17:49:44 +0700217
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000218 if (!fspathcmp(path, wt_path.buf))
Nguyễn Thái Ngọc Duy68353142016-06-03 19:19:39 +0700219 break;
Nguyễn Thái Ngọc Duy105df732019-05-13 17:49:44 +0700220 }
Nguyễn Thái Ngọc Duy68353142016-06-03 19:19:39 +0700221 free(path);
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000222 strbuf_release(&wt_path);
Nguyễn Thái Ngọc Duy68353142016-06-03 19:19:39 +0700223 return *list;
224}
225
Nguyễn Thái Ngọc Duy984ad9e2016-06-03 19:19:40 +0700226int is_main_worktree(const struct worktree *wt)
227{
228 return !wt->id;
229}
230
Nickolai Belakovskid236f122018-10-29 23:24:09 -0700231const char *worktree_lock_reason(struct worktree *wt)
Nguyễn Thái Ngọc Duy346ef532016-06-13 19:18:23 +0700232{
Rafael Silvaeb361352021-01-19 22:27:35 +0100233 if (is_main_worktree(wt))
234 return NULL;
Nguyễn Thái Ngọc Duy346ef532016-06-13 19:18:23 +0700235
236 if (!wt->lock_reason_valid) {
237 struct strbuf path = STRBUF_INIT;
238
239 strbuf_addstr(&path, worktree_git_path(wt, "locked"));
240 if (file_exists(path.buf)) {
241 struct strbuf lock_reason = STRBUF_INIT;
242 if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
243 die_errno(_("failed to read '%s'"), path.buf);
244 strbuf_trim(&lock_reason);
245 wt->lock_reason = strbuf_detach(&lock_reason, NULL);
246 } else
247 wt->lock_reason = NULL;
248 wt->lock_reason_valid = 1;
249 strbuf_release(&path);
250 }
251
252 return wt->lock_reason;
253}
254
Rafael Silvafc0c7d52021-01-19 22:27:34 +0100255const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire)
256{
257 struct strbuf reason = STRBUF_INIT;
258 char *path = NULL;
259
260 if (is_main_worktree(wt))
261 return NULL;
262 if (wt->prune_reason_valid)
263 return wt->prune_reason;
264
265 if (should_prune_worktree(wt->id, &reason, &path, expire))
266 wt->prune_reason = strbuf_detach(&reason, NULL);
267 wt->prune_reason_valid = 1;
268
269 strbuf_release(&reason);
270 free(path);
271 return wt->prune_reason;
272}
273
Nguyễn Thái Ngọc Duy4ddddc12018-01-24 16:53:51 +0700274/* convenient wrapper to deal with NULL strbuf */
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 10:05:18 +0200275__attribute__((format (printf, 2, 3)))
Nguyễn Thái Ngọc Duy4ddddc12018-01-24 16:53:51 +0700276static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...)
277{
278 va_list params;
279
280 if (!buf)
281 return;
282
283 va_start(params, fmt);
284 strbuf_vaddf(buf, fmt, params);
285 va_end(params);
286}
287
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +0700288int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
289 unsigned flags)
Nguyễn Thái Ngọc Duy4ddddc12018-01-24 16:53:51 +0700290{
291 struct strbuf wt_path = STRBUF_INIT;
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000292 struct strbuf realpath = STRBUF_INIT;
Nguyễn Thái Ngọc Duy4ddddc12018-01-24 16:53:51 +0700293 char *path = NULL;
294 int err, ret = -1;
295
296 strbuf_addf(&wt_path, "%s/.git", wt->path);
297
298 if (is_main_worktree(wt)) {
299 if (is_directory(wt_path.buf)) {
300 ret = 0;
301 goto done;
302 }
303 /*
304 * Main worktree using .git file to point to the
305 * repository would make it impossible to know where
306 * the actual worktree is if this function is executed
307 * from another worktree. No .git file support for now.
308 */
309 strbuf_addf_gently(errmsg,
310 _("'%s' at main working tree is not the repository directory"),
311 wt_path.buf);
312 goto done;
313 }
314
315 /*
316 * Make sure "gitdir" file points to a real .git file and that
317 * file points back here.
318 */
319 if (!is_absolute_path(wt->path)) {
320 strbuf_addf_gently(errmsg,
321 _("'%s' file does not contain absolute path to the working tree location"),
322 git_common_path("worktrees/%s/gitdir", wt->id));
323 goto done;
324 }
325
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +0700326 if (flags & WT_VALIDATE_WORKTREE_MISSING_OK &&
327 !file_exists(wt->path)) {
328 ret = 0;
329 goto done;
330 }
331
Nguyễn Thái Ngọc Duy4ddddc12018-01-24 16:53:51 +0700332 if (!file_exists(wt_path.buf)) {
333 strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf);
334 goto done;
335 }
336
337 path = xstrdup_or_null(read_gitfile_gently(wt_path.buf, &err));
338 if (!path) {
339 strbuf_addf_gently(errmsg, _("'%s' is not a .git file, error code %d"),
340 wt_path.buf, err);
341 goto done;
342 }
343
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000344 strbuf_realpath(&realpath, git_common_path("worktrees/%s", wt->id), 1);
345 ret = fspathcmp(path, realpath.buf);
Nguyễn Thái Ngọc Duy4ddddc12018-01-24 16:53:51 +0700346
347 if (ret)
348 strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"),
349 wt->path, git_common_path("worktrees/%s", wt->id));
350done:
351 free(path);
352 strbuf_release(&wt_path);
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000353 strbuf_release(&realpath);
Nguyễn Thái Ngọc Duy4ddddc12018-01-24 16:53:51 +0700354 return ret;
355}
356
Nguyễn Thái Ngọc Duy9c620fc2018-02-12 16:49:35 +0700357void update_worktree_location(struct worktree *wt, const char *path_)
358{
359 struct strbuf path = STRBUF_INIT;
360
361 if (is_main_worktree(wt))
Johannes Schindelin033abf92018-05-02 11:38:39 +0200362 BUG("can't relocate main worktree");
Nguyễn Thái Ngọc Duy9c620fc2018-02-12 16:49:35 +0700363
364 strbuf_realpath(&path, path_, 1);
365 if (fspathcmp(wt->path, path.buf)) {
366 write_file(git_common_path("worktrees/%s/gitdir", wt->id),
367 "%s/.git", path.buf);
368 free(wt->path);
369 wt->path = strbuf_detach(&path, NULL);
370 }
371 strbuf_release(&path);
372}
373
Nguyễn Thái Ngọc Duy14ace5b2016-04-22 20:01:36 +0700374int is_worktree_being_rebased(const struct worktree *wt,
375 const char *target)
Nguyễn Thái Ngọc Duy8d9fdd72016-04-22 20:01:33 +0700376{
377 struct wt_status_state state;
378 int found_rebase;
379
380 memset(&state, 0, sizeof(state));
381 found_rebase = wt_status_check_rebase(wt, &state) &&
Martin Ågrena46d1f72020-09-27 15:15:47 +0200382 (state.rebase_in_progress ||
383 state.rebase_interactive_in_progress) &&
384 state.branch &&
385 skip_prefix(target, "refs/heads/", &target) &&
386 !strcmp(state.branch, target);
Martin Ågren962dd7e2020-09-27 15:15:43 +0200387 wt_status_state_free_buffers(&state);
Nguyễn Thái Ngọc Duy8d9fdd72016-04-22 20:01:33 +0700388 return found_rebase;
389}
390
Nguyễn Thái Ngọc Duy14ace5b2016-04-22 20:01:36 +0700391int is_worktree_being_bisected(const struct worktree *wt,
392 const char *target)
Nguyễn Thái Ngọc Duy04a3dfb2016-04-22 20:01:35 +0700393{
394 struct wt_status_state state;
Martin Ågrenfb07bd42020-09-27 15:15:46 +0200395 int found_bisect;
Nguyễn Thái Ngọc Duy04a3dfb2016-04-22 20:01:35 +0700396
397 memset(&state, 0, sizeof(state));
Martin Ågrenfb07bd42020-09-27 15:15:46 +0200398 found_bisect = wt_status_check_bisect(wt, &state) &&
399 state.branch &&
Martin Ågrena46d1f72020-09-27 15:15:47 +0200400 skip_prefix(target, "refs/heads/", &target) &&
401 !strcmp(state.branch, target);
Martin Ågren962dd7e2020-09-27 15:15:43 +0200402 wt_status_state_free_buffers(&state);
Martin Ågrenfb07bd42020-09-27 15:15:46 +0200403 return found_bisect;
Nguyễn Thái Ngọc Duy04a3dfb2016-04-22 20:01:35 +0700404}
405
Nguyễn Thái Ngọc Duy8d9fdd72016-04-22 20:01:33 +0700406/*
407 * note: this function should be able to detect shared symref even if
408 * HEAD is temporarily detached (e.g. in the middle of rebase or
409 * bisect). New commands that do similar things should update this
410 * function as well.
411 */
Rubén Justo662078c2023-02-25 15:21:51 +0100412int is_shared_symref(const struct worktree *wt, const char *symref,
413 const char *target)
414{
415 const char *symref_target;
416 struct ref_store *refs;
417 int flags;
418
419 if (wt->is_bare)
420 return 0;
421
422 if (wt->is_detached && !strcmp(symref, "HEAD")) {
423 if (is_worktree_being_rebased(wt, target))
424 return 1;
425 if (is_worktree_being_bisected(wt, target))
426 return 1;
427 }
428
429 refs = get_worktree_ref_store(wt);
430 symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
431 NULL, &flags);
432 if ((flags & REF_ISSYMREF) &&
433 symref_target && !strcmp(symref_target, target))
434 return 1;
435
436 return 0;
437}
438
Anders Kaseorgc8dd4912021-12-01 14:15:43 -0800439const struct worktree *find_shared_symref(struct worktree **worktrees,
440 const char *symref,
Nguyễn Thái Ngọc Duyd3b9ac02016-04-22 20:01:27 +0700441 const char *target)
Michael Rappazzo51934902015-10-08 13:01:03 -0400442{
Michael Rappazzo51934902015-10-08 13:01:03 -0400443
Rubén Justofaa4d592023-02-25 15:22:02 +0100444 for (int i = 0; worktrees[i]; i++)
Rubén Justo662078c2023-02-25 15:21:51 +0100445 if (is_shared_symref(worktrees[i], symref, target))
446 return worktrees[i];
Nguyễn Thái Ngọc Duyfa099d22017-04-24 17:01:23 +0700447
Rubén Justo662078c2023-02-25 15:21:51 +0100448 return NULL;
Michael Rappazzoac6c5612015-10-02 07:55:31 -0400449}
Stefan Beller1a248cf2016-12-12 11:04:33 -0800450
451int submodule_uses_worktrees(const char *path)
452{
453 char *submodule_gitdir;
brian m. carlsone02a7142020-02-22 20:17:41 +0000454 struct strbuf sb = STRBUF_INIT, err = STRBUF_INIT;
Stefan Beller1a248cf2016-12-12 11:04:33 -0800455 DIR *dir;
456 struct dirent *d;
Stefan Beller7c4be452016-12-27 09:50:13 -0800457 int ret = 0;
Martin Ågrene8805af2019-02-28 21:36:28 +0100458 struct repository_format format = REPOSITORY_FORMAT_INIT;
Stefan Beller1a248cf2016-12-12 11:04:33 -0800459
460 submodule_gitdir = git_pathdup_submodule(path, "%s", "");
461 if (!submodule_gitdir)
462 return 0;
463
464 /* The env would be set for the superproject. */
465 get_common_dir_noenv(&sb, submodule_gitdir);
Johannes Schindelind32de662017-05-04 15:59:19 +0200466 free(submodule_gitdir);
Stefan Beller1a248cf2016-12-12 11:04:33 -0800467
Stefan Beller1a248cf2016-12-12 11:04:33 -0800468 strbuf_addstr(&sb, "/config");
469 read_repository_format(&format, sb.buf);
brian m. carlsone02a7142020-02-22 20:17:41 +0000470 if (verify_repository_format(&format, &err)) {
471 strbuf_release(&err);
Stefan Beller1a248cf2016-12-12 11:04:33 -0800472 strbuf_release(&sb);
Martin Ågrene8805af2019-02-28 21:36:28 +0100473 clear_repository_format(&format);
Stefan Beller1a248cf2016-12-12 11:04:33 -0800474 return 1;
475 }
Martin Ågrene8805af2019-02-28 21:36:28 +0100476 clear_repository_format(&format);
brian m. carlsone02a7142020-02-22 20:17:41 +0000477 strbuf_release(&err);
Stefan Beller1a248cf2016-12-12 11:04:33 -0800478
479 /* Replace config by worktrees. */
480 strbuf_setlen(&sb, sb.len - strlen("config"));
481 strbuf_addstr(&sb, "worktrees");
482
483 /* See if there is any file inside the worktrees directory. */
484 dir = opendir(sb.buf);
485 strbuf_release(&sb);
Stefan Beller1a248cf2016-12-12 11:04:33 -0800486
487 if (!dir)
488 return 0;
489
Elijah Newrenb548f0f2021-05-12 17:28:22 +0000490 d = readdir_skip_dot_and_dotdot(dir);
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700491 if (d)
Stefan Beller1a248cf2016-12-12 11:04:33 -0800492 ret = 1;
Stefan Beller1a248cf2016-12-12 11:04:33 -0800493 closedir(dir);
494 return ret;
495}
Nguyễn Thái Ngọc Duyd0c39a42017-08-23 19:36:59 +0700496
Nguyễn Thái Ngọc Duyab3e1f72018-10-21 10:08:56 +0200497void strbuf_worktree_ref(const struct worktree *wt,
498 struct strbuf *sb,
499 const char *refname)
500{
Han-Wen Nienhuys71e54732022-09-19 16:34:50 +0000501 if (parse_worktree_ref(refname, NULL, NULL, NULL) ==
502 REF_WORKTREE_CURRENT &&
503 wt && !wt->is_current) {
504 if (is_main_worktree(wt))
505 strbuf_addstr(sb, "main-worktree/");
506 else
507 strbuf_addf(sb, "worktrees/%s/", wt->id);
Nguyễn Thái Ngọc Duyab3e1f72018-10-21 10:08:56 +0200508 }
509 strbuf_addstr(sb, refname);
510}
511
Nguyễn Thái Ngọc Duyd0c39a42017-08-23 19:36:59 +0700512int other_head_refs(each_ref_fn fn, void *cb_data)
513{
514 struct worktree **worktrees, **p;
Martin Ågrenef2d5542020-09-27 15:15:44 +0200515 struct strbuf refname = STRBUF_INIT;
Nguyễn Thái Ngọc Duyd0c39a42017-08-23 19:36:59 +0700516 int ret = 0;
517
Eric Sunshine03f24652020-06-19 19:35:44 -0400518 worktrees = get_worktrees();
Nguyễn Thái Ngọc Duyd0c39a42017-08-23 19:36:59 +0700519 for (p = worktrees; *p; p++) {
520 struct worktree *wt = *p;
Nguyễn Thái Ngọc Duyab3e1f72018-10-21 10:08:56 +0200521 struct object_id oid;
522 int flag;
Nguyễn Thái Ngọc Duyd0c39a42017-08-23 19:36:59 +0700523
524 if (wt->is_current)
525 continue;
526
Martin Ågrenef2d5542020-09-27 15:15:44 +0200527 strbuf_reset(&refname);
528 strbuf_worktree_ref(wt, &refname, "HEAD");
Ævar Arnfjörð Bjarmasonf1da24c2021-10-16 11:39:27 +0200529 if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
Ævar Arnfjörð Bjarmason76887df2021-10-16 11:39:14 +0200530 refname.buf,
531 RESOLVE_REF_READING,
Ævar Arnfjörð Bjarmasonce14de02022-01-26 15:37:01 +0100532 &oid, &flag))
Martin Ågrenef2d5542020-09-27 15:15:44 +0200533 ret = fn(refname.buf, &oid, flag, cb_data);
Nguyễn Thái Ngọc Duyd0c39a42017-08-23 19:36:59 +0700534 if (ret)
535 break;
536 }
537 free_worktrees(worktrees);
Martin Ågrenef2d5542020-09-27 15:15:44 +0200538 strbuf_release(&refname);
Nguyễn Thái Ngọc Duyd0c39a42017-08-23 19:36:59 +0700539 return ret;
540}
Eric Sunshinebdd1f3e2020-08-31 02:57:57 -0400541
542/*
543 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
544 * pointing at <repo>/worktrees/<id>.
545 */
546static void repair_gitfile(struct worktree *wt,
547 worktree_repair_fn fn, void *cb_data)
548{
549 struct strbuf dotgit = STRBUF_INIT;
550 struct strbuf repo = STRBUF_INIT;
551 char *backlink;
552 const char *repair = NULL;
553 int err;
554
555 /* missing worktree can't be repaired */
556 if (!file_exists(wt->path))
557 return;
558
559 if (!is_directory(wt->path)) {
560 fn(1, wt->path, _("not a directory"), cb_data);
561 return;
562 }
563
564 strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
565 strbuf_addf(&dotgit, "%s/.git", wt->path);
566 backlink = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
567
568 if (err == READ_GITFILE_ERR_NOT_A_FILE)
569 fn(1, wt->path, _(".git is not a file"), cb_data);
570 else if (err)
571 repair = _(".git file broken");
572 else if (fspathcmp(backlink, repo.buf))
573 repair = _(".git file incorrect");
574
575 if (repair) {
576 fn(0, wt->path, repair, cb_data);
577 write_file(dotgit.buf, "gitdir: %s", repo.buf);
578 }
579
580 free(backlink);
581 strbuf_release(&repo);
582 strbuf_release(&dotgit);
583}
584
585static void repair_noop(int iserr, const char *path, const char *msg,
586 void *cb_data)
587{
588 /* nothing */
589}
590
591void repair_worktrees(worktree_repair_fn fn, void *cb_data)
592{
593 struct worktree **worktrees = get_worktrees();
594 struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
595
596 if (!fn)
597 fn = repair_noop;
598 for (; *wt; wt++)
599 repair_gitfile(*wt, fn, cb_data);
600 free_worktrees(worktrees);
601}
Eric Sunshineb214ab52020-08-31 02:57:58 -0400602
603static int is_main_worktree_path(const char *path)
604{
605 struct strbuf target = STRBUF_INIT;
606 struct strbuf maindir = STRBUF_INIT;
607 int cmp;
608
609 strbuf_add_real_path(&target, path);
610 strbuf_strip_suffix(&target, "/.git");
611 strbuf_add_real_path(&maindir, get_git_common_dir());
612 strbuf_strip_suffix(&maindir, "/.git");
613 cmp = fspathcmp(maindir.buf, target.buf);
614
615 strbuf_release(&maindir);
616 strbuf_release(&target);
617 return !cmp;
618}
619
620/*
Eric Sunshinecf76bae2020-12-21 03:16:01 -0500621 * If both the main worktree and linked worktree have been moved, then the
622 * gitfile /path/to/worktree/.git won't point into the repository, thus we
623 * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
624 * be able to infer the gitdir by manually reading /path/to/worktree/.git,
625 * extracting the <id>, and checking if <repo>/worktrees/<id> exists.
626 */
627static char *infer_backlink(const char *gitfile)
628{
629 struct strbuf actual = STRBUF_INIT;
630 struct strbuf inferred = STRBUF_INIT;
631 const char *id;
632
633 if (strbuf_read_file(&actual, gitfile, 0) < 0)
634 goto error;
635 if (!starts_with(actual.buf, "gitdir:"))
636 goto error;
637 if (!(id = find_last_dir_sep(actual.buf)))
638 goto error;
639 strbuf_trim(&actual);
640 id++; /* advance past '/' to point at <id> */
641 if (!*id)
642 goto error;
643 strbuf_git_common_path(&inferred, the_repository, "worktrees/%s", id);
644 if (!is_directory(inferred.buf))
645 goto error;
646
647 strbuf_release(&actual);
648 return strbuf_detach(&inferred, NULL);
649
650error:
651 strbuf_release(&actual);
652 strbuf_release(&inferred);
653 return NULL;
654}
655
656/*
Eric Sunshineb214ab52020-08-31 02:57:58 -0400657 * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
658 * the worktree's path.
659 */
660void repair_worktree_at_path(const char *path,
661 worktree_repair_fn fn, void *cb_data)
662{
663 struct strbuf dotgit = STRBUF_INIT;
664 struct strbuf realdotgit = STRBUF_INIT;
665 struct strbuf gitdir = STRBUF_INIT;
666 struct strbuf olddotgit = STRBUF_INIT;
667 char *backlink = NULL;
668 const char *repair = NULL;
669 int err;
670
671 if (!fn)
672 fn = repair_noop;
673
674 if (is_main_worktree_path(path))
675 goto done;
676
677 strbuf_addf(&dotgit, "%s/.git", path);
678 if (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {
679 fn(1, path, _("not a valid path"), cb_data);
680 goto done;
681 }
682
683 backlink = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));
684 if (err == READ_GITFILE_ERR_NOT_A_FILE) {
685 fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
686 goto done;
Eric Sunshinecf76bae2020-12-21 03:16:01 -0500687 } else if (err == READ_GITFILE_ERR_NOT_A_REPO) {
688 if (!(backlink = infer_backlink(realdotgit.buf))) {
689 fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
690 goto done;
691 }
Eric Sunshineb214ab52020-08-31 02:57:58 -0400692 } else if (err) {
693 fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
694 goto done;
695 }
696
697 strbuf_addf(&gitdir, "%s/gitdir", backlink);
698 if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
699 repair = _("gitdir unreadable");
700 else {
701 strbuf_rtrim(&olddotgit);
702 if (fspathcmp(olddotgit.buf, realdotgit.buf))
703 repair = _("gitdir incorrect");
704 }
705
706 if (repair) {
707 fn(0, gitdir.buf, repair, cb_data);
708 write_file(gitdir.buf, "%s", realdotgit.buf);
709 }
710done:
711 free(backlink);
712 strbuf_release(&olddotgit);
713 strbuf_release(&gitdir);
714 strbuf_release(&realdotgit);
715 strbuf_release(&dotgit);
716}
Rafael Silvaa29a8b72021-01-19 22:27:33 +0100717
718int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire)
719{
720 struct stat st;
721 char *path;
722 int fd;
723 size_t len;
724 ssize_t read_result;
725
726 *wtpath = NULL;
727 if (!is_directory(git_path("worktrees/%s", id))) {
728 strbuf_addstr(reason, _("not a valid directory"));
729 return 1;
730 }
731 if (file_exists(git_path("worktrees/%s/locked", id)))
732 return 0;
733 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
734 strbuf_addstr(reason, _("gitdir file does not exist"));
735 return 1;
736 }
737 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
738 if (fd < 0) {
739 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
740 strerror(errno));
741 return 1;
742 }
743 len = xsize_t(st.st_size);
744 path = xmallocz(len);
745
746 read_result = read_in_full(fd, path, len);
747 if (read_result < 0) {
748 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
749 strerror(errno));
750 close(fd);
751 free(path);
752 return 1;
753 }
754 close(fd);
755
756 if (read_result != len) {
757 strbuf_addf(reason,
758 _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
759 (uintmax_t)len, (uintmax_t)read_result);
760 free(path);
761 return 1;
762 }
763 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
764 len--;
765 if (!len) {
766 strbuf_addstr(reason, _("invalid gitdir file"));
767 free(path);
768 return 1;
769 }
770 path[len] = '\0';
771 if (!file_exists(path)) {
772 if (stat(git_path("worktrees/%s/index", id), &st) ||
773 st.st_mtime <= expire) {
774 strbuf_addstr(reason, _("gitdir file points to non-existent location"));
775 free(path);
776 return 1;
777 } else {
778 *wtpath = path;
779 return 0;
780 }
781 }
782 *wtpath = path;
783 return 0;
784}
Derrick Stolee615a84a2022-02-07 21:32:59 +0000785
786static int move_config_setting(const char *key, const char *value,
787 const char *from_file, const char *to_file)
788{
789 if (git_config_set_in_file_gently(to_file, key, value))
790 return error(_("unable to set %s in '%s'"), key, to_file);
791 if (git_config_set_in_file_gently(from_file, key, NULL))
792 return error(_("unable to unset %s in '%s'"), key, from_file);
793 return 0;
794}
795
796int init_worktree_config(struct repository *r)
797{
798 int res = 0;
799 int bare = 0;
800 struct config_set cs = { { 0 } };
801 const char *core_worktree;
802 char *common_config_file;
803 char *main_worktree_file;
804
805 /*
806 * If the extension is already enabled, then we can skip the
807 * upgrade process.
808 */
809 if (repository_format_worktree_config)
810 return 0;
811 if ((res = git_config_set_gently("extensions.worktreeConfig", "true")))
812 return error(_("failed to set extensions.worktreeConfig setting"));
813
814 common_config_file = xstrfmt("%s/config", r->commondir);
815 main_worktree_file = xstrfmt("%s/config.worktree", r->commondir);
816
817 git_configset_init(&cs);
818 git_configset_add_file(&cs, common_config_file);
819
820 /*
821 * If core.bare is true in the common config file, then we need to
822 * move it to the main worktree's config file or it will break all
823 * worktrees. If it is false, then leave it in place because it
824 * _could_ be negating a global core.bare=true.
825 */
826 if (!git_configset_get_bool(&cs, "core.bare", &bare) && bare) {
827 if ((res = move_config_setting("core.bare", "true",
828 common_config_file,
829 main_worktree_file)))
830 goto cleanup;
831 }
832 /*
833 * If core.worktree is set, then the main worktree is located
834 * somewhere different than the parent of the common Git dir.
835 * Relocate that value to avoid breaking all worktrees with this
836 * upgrade to worktree config.
837 */
838 if (!git_configset_get_value(&cs, "core.worktree", &core_worktree)) {
839 if ((res = move_config_setting("core.worktree", core_worktree,
840 common_config_file,
841 main_worktree_file)))
842 goto cleanup;
843 }
844
845 /*
846 * Ensure that we use worktree config for the remaining lifetime
847 * of the current process.
848 */
849 repository_format_worktree_config = 1;
850
851cleanup:
852 git_configset_clear(&cs);
853 free(common_config_file);
854 free(main_worktree_file);
855 return res;
856}