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