]> git.proxmox.com Git - libgit2.git/blob - src/worktree.c
Merge pull request #4254 from pks-t/pks/changelog-v0.26
[libgit2.git] / src / worktree.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "common.h"
9
10 #include "git2/branch.h"
11 #include "git2/commit.h"
12 #include "git2/worktree.h"
13
14 #include "repository.h"
15 #include "worktree.h"
16
17 static bool is_worktree_dir(const char *dir)
18 {
19 git_buf buf = GIT_BUF_INIT;
20 int error;
21
22 if (git_buf_sets(&buf, dir) < 0)
23 return -1;
24
25 error = git_path_contains_file(&buf, "commondir")
26 && git_path_contains_file(&buf, "gitdir")
27 && git_path_contains_file(&buf, "HEAD");
28
29 git_buf_free(&buf);
30 return error;
31 }
32
33 int git_worktree_list(git_strarray *wts, git_repository *repo)
34 {
35 git_vector worktrees = GIT_VECTOR_INIT;
36 git_buf path = GIT_BUF_INIT;
37 char *worktree;
38 unsigned i, len;
39 int error;
40
41 assert(wts && repo);
42
43 wts->count = 0;
44 wts->strings = NULL;
45
46 if ((error = git_buf_printf(&path, "%s/worktrees/", repo->commondir)) < 0)
47 goto exit;
48 if (!git_path_exists(path.ptr) || git_path_is_empty_dir(path.ptr))
49 goto exit;
50 if ((error = git_path_dirload(&worktrees, path.ptr, path.size, 0x0)) < 0)
51 goto exit;
52
53 len = path.size;
54
55 git_vector_foreach(&worktrees, i, worktree) {
56 git_buf_truncate(&path, len);
57 git_buf_puts(&path, worktree);
58
59 if (!is_worktree_dir(path.ptr)) {
60 git_vector_remove(&worktrees, i);
61 git__free(worktree);
62 }
63 }
64
65 wts->strings = (char **)git_vector_detach(&wts->count, NULL, &worktrees);
66
67 exit:
68 git_buf_free(&path);
69
70 return error;
71 }
72
73 char *git_worktree__read_link(const char *base, const char *file)
74 {
75 git_buf path = GIT_BUF_INIT, buf = GIT_BUF_INIT;
76
77 assert(base && file);
78
79 if (git_buf_joinpath(&path, base, file) < 0)
80 goto err;
81 if (git_futils_readbuffer(&buf, path.ptr) < 0)
82 goto err;
83 git_buf_free(&path);
84
85 git_buf_rtrim(&buf);
86
87 if (!git_path_is_relative(buf.ptr))
88 return git_buf_detach(&buf);
89
90 if (git_buf_sets(&path, base) < 0)
91 goto err;
92 if (git_path_apply_relative(&path, buf.ptr) < 0)
93 goto err;
94 git_buf_free(&buf);
95
96 return git_buf_detach(&path);
97
98 err:
99 git_buf_free(&buf);
100 git_buf_free(&path);
101
102 return NULL;
103 }
104
105 static int write_wtfile(const char *base, const char *file, const git_buf *buf)
106 {
107 git_buf path = GIT_BUF_INIT;
108 int err;
109
110 assert(base && file && buf);
111
112 if ((err = git_buf_joinpath(&path, base, file)) < 0)
113 goto out;
114
115 if ((err = git_futils_writebuffer(buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0)
116 goto out;
117
118 out:
119 git_buf_free(&path);
120
121 return err;
122 }
123
124 static int open_worktree_dir(git_worktree **out, const char *parent, const char *dir, const char *name)
125 {
126 git_buf gitdir = GIT_BUF_INIT;
127 git_worktree *wt = NULL;
128 int error = 0;
129
130 if (!is_worktree_dir(dir)) {
131 error = -1;
132 goto out;
133 }
134
135 if ((wt = git__calloc(1, sizeof(struct git_repository))) == NULL) {
136 error = -1;
137 goto out;
138 }
139
140 if ((wt->name = git__strdup(name)) == NULL
141 || (wt->commondir_path = git_worktree__read_link(dir, "commondir")) == NULL
142 || (wt->gitlink_path = git_worktree__read_link(dir, "gitdir")) == NULL
143 || (wt->parent_path = git__strdup(parent)) == NULL) {
144 error = -1;
145 goto out;
146 }
147
148 if ((error = git_path_prettify_dir(&gitdir, dir, NULL)) < 0)
149 goto out;
150 wt->gitdir_path = git_buf_detach(&gitdir);
151
152 wt->locked = !!git_worktree_is_locked(NULL, wt);
153
154 *out = wt;
155
156 out:
157 if (error)
158 git_worktree_free(wt);
159 git_buf_free(&gitdir);
160
161 return error;
162 }
163
164 int git_worktree_lookup(git_worktree **out, git_repository *repo, const char *name)
165 {
166 git_buf path = GIT_BUF_INIT;
167 git_worktree *wt = NULL;
168 int error;
169
170 assert(repo && name);
171
172 *out = NULL;
173
174 if ((error = git_buf_printf(&path, "%s/worktrees/%s", repo->commondir, name)) < 0)
175 goto out;
176
177 if ((error = (open_worktree_dir(out, git_repository_workdir(repo), path.ptr, name))) < 0)
178 goto out;
179
180 out:
181 git_buf_free(&path);
182
183 if (error)
184 git_worktree_free(wt);
185
186 return error;
187 }
188
189 int git_worktree_open_from_repository(git_worktree **out, git_repository *repo)
190 {
191 git_buf parent = GIT_BUF_INIT;
192 const char *gitdir, *commondir;
193 char *name = NULL;
194 int error = 0;
195
196 if (!git_repository_is_worktree(repo)) {
197 giterr_set(GITERR_WORKTREE, "cannot open worktree of a non-worktree repo");
198 error = -1;
199 goto out;
200 }
201
202 gitdir = git_repository_path(repo);
203 commondir = git_repository_commondir(repo);
204
205 if ((error = git_path_prettify_dir(&parent, "..", commondir)) < 0)
206 goto out;
207
208 /* The name is defined by the last component in '.git/worktree/%s' */
209 name = git_path_basename(gitdir);
210
211 if ((error = open_worktree_dir(out, parent.ptr, gitdir, name)) < 0)
212 goto out;
213
214 out:
215 git__free(name);
216 git_buf_free(&parent);
217
218 return error;
219 }
220
221 void git_worktree_free(git_worktree *wt)
222 {
223 if (!wt)
224 return;
225
226 git__free(wt->commondir_path);
227 git__free(wt->gitlink_path);
228 git__free(wt->gitdir_path);
229 git__free(wt->parent_path);
230 git__free(wt->name);
231 git__free(wt);
232 }
233
234 int git_worktree_validate(const git_worktree *wt)
235 {
236 git_buf buf = GIT_BUF_INIT;
237 int err = 0;
238
239 assert(wt);
240
241 git_buf_puts(&buf, wt->gitdir_path);
242 if (!is_worktree_dir(buf.ptr)) {
243 giterr_set(GITERR_WORKTREE,
244 "Worktree gitdir ('%s') is not valid",
245 wt->gitlink_path);
246 err = -1;
247 goto out;
248 }
249
250 if (!git_path_exists(wt->parent_path)) {
251 giterr_set(GITERR_WORKTREE,
252 "Worktree parent directory ('%s') does not exist ",
253 wt->parent_path);
254 err = -2;
255 goto out;
256 }
257
258 if (!git_path_exists(wt->commondir_path)) {
259 giterr_set(GITERR_WORKTREE,
260 "Worktree common directory ('%s') does not exist ",
261 wt->commondir_path);
262 err = -3;
263 goto out;
264 }
265
266 out:
267 git_buf_free(&buf);
268
269 return err;
270 }
271
272 int git_worktree_add_init_options(git_worktree_add_options *opts,
273 unsigned int version)
274 {
275 GIT_INIT_STRUCTURE_FROM_TEMPLATE(opts, version,
276 git_worktree_add_options, GIT_WORKTREE_ADD_OPTIONS_INIT);
277 return 0;
278 }
279
280 int git_worktree_add(git_worktree **out, git_repository *repo,
281 const char *name, const char *worktree,
282 const git_worktree_add_options *opts)
283 {
284 git_buf gitdir = GIT_BUF_INIT, wddir = GIT_BUF_INIT, buf = GIT_BUF_INIT;
285 git_reference *ref = NULL, *head = NULL;
286 git_commit *commit = NULL;
287 git_repository *wt = NULL;
288 git_checkout_options coopts = GIT_CHECKOUT_OPTIONS_INIT;
289 git_worktree_add_options wtopts = GIT_WORKTREE_ADD_OPTIONS_INIT;
290 int err;
291
292 GITERR_CHECK_VERSION(
293 opts, GIT_WORKTREE_ADD_OPTIONS_VERSION, "git_worktree_add_options");
294
295 if (opts)
296 memcpy(&wtopts, opts, sizeof(wtopts));
297
298 assert(out && repo && name && worktree);
299
300 *out = NULL;
301
302 /* Create gitdir directory ".git/worktrees/<name>" */
303 if ((err = git_buf_joinpath(&gitdir, repo->commondir, "worktrees")) < 0)
304 goto out;
305 if (!git_path_exists(gitdir.ptr))
306 if ((err = git_futils_mkdir(gitdir.ptr, 0755, GIT_MKDIR_EXCL)) < 0)
307 goto out;
308 if ((err = git_buf_joinpath(&gitdir, gitdir.ptr, name)) < 0)
309 goto out;
310 if ((err = git_futils_mkdir(gitdir.ptr, 0755, GIT_MKDIR_EXCL)) < 0)
311 goto out;
312 if ((err = git_path_prettify_dir(&gitdir, gitdir.ptr, NULL)) < 0)
313 goto out;
314
315 /* Create worktree work dir */
316 if ((err = git_futils_mkdir(worktree, 0755, GIT_MKDIR_EXCL)) < 0)
317 goto out;
318 if ((err = git_path_prettify_dir(&wddir, worktree, NULL)) < 0)
319 goto out;
320
321 if (wtopts.lock) {
322 int fd;
323
324 if ((err = git_buf_joinpath(&buf, gitdir.ptr, "locked")) < 0)
325 goto out;
326
327 if ((fd = p_creat(buf.ptr, 0644)) < 0) {
328 err = fd;
329 goto out;
330 }
331
332 p_close(fd);
333 git_buf_clear(&buf);
334 }
335
336 /* Create worktree .git file */
337 if ((err = git_buf_printf(&buf, "gitdir: %s\n", gitdir.ptr)) < 0)
338 goto out;
339 if ((err = write_wtfile(wddir.ptr, ".git", &buf)) < 0)
340 goto out;
341
342 /* Create gitdir files */
343 if ((err = git_path_prettify_dir(&buf, repo->commondir, NULL) < 0)
344 || (err = git_buf_putc(&buf, '\n')) < 0
345 || (err = write_wtfile(gitdir.ptr, "commondir", &buf)) < 0)
346 goto out;
347 if ((err = git_buf_joinpath(&buf, wddir.ptr, ".git")) < 0
348 || (err = git_buf_putc(&buf, '\n')) < 0
349 || (err = write_wtfile(gitdir.ptr, "gitdir", &buf)) < 0)
350 goto out;
351
352 /* Create new branch */
353 if ((err = git_repository_head(&head, repo)) < 0)
354 goto out;
355 if ((err = git_commit_lookup(&commit, repo, &head->target.oid)) < 0)
356 goto out;
357 if ((err = git_branch_create(&ref, repo, name, commit, false)) < 0)
358 goto out;
359
360 /* Set worktree's HEAD */
361 if ((err = git_repository_create_head(gitdir.ptr, git_reference_name(ref))) < 0)
362 goto out;
363 if ((err = git_repository_open(&wt, wddir.ptr)) < 0)
364 goto out;
365
366 /* Checkout worktree's HEAD */
367 coopts.checkout_strategy = GIT_CHECKOUT_FORCE;
368 if ((err = git_checkout_head(wt, &coopts)) < 0)
369 goto out;
370
371 /* Load result */
372 if ((err = git_worktree_lookup(out, repo, name)) < 0)
373 goto out;
374
375 out:
376 git_buf_free(&gitdir);
377 git_buf_free(&wddir);
378 git_buf_free(&buf);
379 git_reference_free(ref);
380 git_reference_free(head);
381 git_commit_free(commit);
382 git_repository_free(wt);
383
384 return err;
385 }
386
387 int git_worktree_lock(git_worktree *wt, char *creason)
388 {
389 git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
390 int err;
391
392 assert(wt);
393
394 if ((err = git_worktree_is_locked(NULL, wt)) < 0)
395 goto out;
396
397 if ((err = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
398 goto out;
399
400 if (creason)
401 git_buf_attach_notowned(&buf, creason, strlen(creason));
402
403 if ((err = git_futils_writebuffer(&buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0)
404 goto out;
405
406 wt->locked = 1;
407
408 out:
409 git_buf_free(&path);
410
411 return err;
412 }
413
414 int git_worktree_unlock(git_worktree *wt)
415 {
416 git_buf path = GIT_BUF_INIT;
417
418 assert(wt);
419
420 if (!git_worktree_is_locked(NULL, wt))
421 return 0;
422
423 if (git_buf_joinpath(&path, wt->gitdir_path, "locked") < 0)
424 return -1;
425
426 if (p_unlink(path.ptr) != 0) {
427 git_buf_free(&path);
428 return -1;
429 }
430
431 wt->locked = 0;
432
433 git_buf_free(&path);
434
435 return 0;
436 }
437
438 int git_worktree_is_locked(git_buf *reason, const git_worktree *wt)
439 {
440 git_buf path = GIT_BUF_INIT;
441 int ret;
442
443 assert(wt);
444
445 if (reason)
446 git_buf_clear(reason);
447
448 if ((ret = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
449 goto out;
450 if ((ret = git_path_exists(path.ptr)) && reason)
451 git_futils_readbuffer(reason, path.ptr);
452
453 out:
454 git_buf_free(&path);
455
456 return ret;
457 }
458
459 int git_worktree_prune_init_options(
460 git_worktree_prune_options *opts,
461 unsigned int version)
462 {
463 GIT_INIT_STRUCTURE_FROM_TEMPLATE(opts, version,
464 git_worktree_prune_options, GIT_WORKTREE_PRUNE_OPTIONS_INIT);
465 return 0;
466 }
467
468 int git_worktree_is_prunable(git_worktree *wt,
469 git_worktree_prune_options *opts)
470 {
471 git_buf reason = GIT_BUF_INIT;
472 git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
473
474 GITERR_CHECK_VERSION(
475 opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION,
476 "git_worktree_prune_options");
477
478 if (opts)
479 memcpy(&popts, opts, sizeof(popts));
480
481 if ((popts.flags & GIT_WORKTREE_PRUNE_LOCKED) == 0 &&
482 git_worktree_is_locked(&reason, wt))
483 {
484 if (!reason.size)
485 git_buf_attach_notowned(&reason, "no reason given", 15);
486 giterr_set(GITERR_WORKTREE, "Not pruning locked working tree: '%s'", reason.ptr);
487 git_buf_free(&reason);
488
489 return 0;
490 }
491
492 if ((popts.flags & GIT_WORKTREE_PRUNE_VALID) == 0 &&
493 git_worktree_validate(wt) == 0)
494 {
495 giterr_set(GITERR_WORKTREE, "Not pruning valid working tree");
496 return 0;
497 }
498
499 return 1;
500 }
501
502 int git_worktree_prune(git_worktree *wt,
503 git_worktree_prune_options *opts)
504 {
505 git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
506 git_buf path = GIT_BUF_INIT;
507 char *wtpath;
508 int err;
509
510 GITERR_CHECK_VERSION(
511 opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION,
512 "git_worktree_prune_options");
513
514 if (opts)
515 memcpy(&popts, opts, sizeof(popts));
516
517 if (!git_worktree_is_prunable(wt, &popts)) {
518 err = -1;
519 goto out;
520 }
521
522 /* Delete gitdir in parent repository */
523 if ((err = git_buf_printf(&path, "%s/worktrees/%s", wt->commondir_path, wt->name)) < 0)
524 goto out;
525 if (!git_path_exists(path.ptr))
526 {
527 giterr_set(GITERR_WORKTREE, "Worktree gitdir '%s' does not exist", path.ptr);
528 err = -1;
529 goto out;
530 }
531 if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0)
532 goto out;
533
534 /* Skip deletion of the actual working tree if it does
535 * not exist or deletion was not requested */
536 if ((popts.flags & GIT_WORKTREE_PRUNE_WORKING_TREE) == 0 ||
537 !git_path_exists(wt->gitlink_path))
538 {
539 goto out;
540 }
541
542 if ((wtpath = git_path_dirname(wt->gitlink_path)) == NULL)
543 goto out;
544 git_buf_attach(&path, wtpath, 0);
545 if (!git_path_exists(path.ptr))
546 {
547 giterr_set(GITERR_WORKTREE, "Working tree '%s' does not exist", path.ptr);
548 err = -1;
549 goto out;
550 }
551 if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0)
552 goto out;
553
554 out:
555 git_buf_free(&path);
556
557 return err;
558 }