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 
is_worktree_dir(const char * dir)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 
git_worktree_list(git_strarray * wts,git_repository * repo)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 
git_worktree__read_link(const char * base,const char * file)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 
write_wtfile(const char * base,const char * file,const git_buf * buf)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 
open_worktree_dir(git_worktree ** out,const char * parent,const char * dir,const char * name)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 
git_worktree_lookup(git_worktree ** out,git_repository * repo,const char * name)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 
git_worktree_open_from_repository(git_worktree ** out,git_repository * repo)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 
git_worktree_free(git_worktree * wt)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 
git_worktree_validate(const git_worktree * wt)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 
git_worktree_add_options_init(git_worktree_add_options * opts,unsigned int version)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 
git_worktree_add_init_options(git_worktree_add_options * opts,unsigned int version)274 int git_worktree_add_init_options(git_worktree_add_options *opts,
275 	unsigned int version)
276 {
277 	return git_worktree_add_options_init(opts, version);
278 }
279 
git_worktree_add(git_worktree ** out,git_repository * repo,const char * name,const char * worktree,const git_worktree_add_options * opts)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 	GIT_ERROR_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 	if (wtopts.ref) {
303 		if (!git_reference_is_branch(wtopts.ref)) {
304 			git_error_set(GIT_ERROR_WORKTREE, "reference is not a branch");
305 			err = -1;
306 			goto out;
307 		}
308 
309 		if (git_branch_is_checked_out(wtopts.ref)) {
310 			git_error_set(GIT_ERROR_WORKTREE, "reference is already checked out");
311 			err = -1;
312 			goto out;
313 		}
314 	}
315 
316 	/* Create gitdir directory ".git/worktrees/<name>" */
317 	if ((err = git_buf_joinpath(&gitdir, repo->commondir, "worktrees")) < 0)
318 		goto out;
319 	if (!git_path_exists(gitdir.ptr))
320 		if ((err = git_futils_mkdir(gitdir.ptr, 0755, GIT_MKDIR_EXCL)) < 0)
321 			goto out;
322 	if ((err = git_buf_joinpath(&gitdir, gitdir.ptr, name)) < 0)
323 		goto out;
324 	if ((err = git_futils_mkdir(gitdir.ptr, 0755, GIT_MKDIR_EXCL)) < 0)
325 		goto out;
326 	if ((err = git_path_prettify_dir(&gitdir, gitdir.ptr, NULL)) < 0)
327 		goto out;
328 
329 	/* Create worktree work dir */
330 	if ((err = git_futils_mkdir(worktree, 0755, GIT_MKDIR_EXCL)) < 0)
331 		goto out;
332 	if ((err = git_path_prettify_dir(&wddir, worktree, NULL)) < 0)
333 		goto out;
334 
335 	if (wtopts.lock) {
336 		int fd;
337 
338 		if ((err = git_buf_joinpath(&buf, gitdir.ptr, "locked")) < 0)
339 			goto out;
340 
341 		if ((fd = p_creat(buf.ptr, 0644)) < 0) {
342 			err = fd;
343 			goto out;
344 		}
345 
346 		p_close(fd);
347 		git_buf_clear(&buf);
348 	}
349 
350 	/* Create worktree .git file */
351 	if ((err = git_buf_printf(&buf, "gitdir: %s\n", gitdir.ptr)) < 0)
352 		goto out;
353 	if ((err = write_wtfile(wddir.ptr, ".git", &buf)) < 0)
354 		goto out;
355 
356 	/* Create gitdir files */
357 	if ((err = git_path_prettify_dir(&buf, repo->commondir, NULL) < 0)
358 	    || (err = git_buf_putc(&buf, '\n')) < 0
359 	    || (err = write_wtfile(gitdir.ptr, "commondir", &buf)) < 0)
360 		goto out;
361 	if ((err = git_buf_joinpath(&buf, wddir.ptr, ".git")) < 0
362 	    || (err = git_buf_putc(&buf, '\n')) < 0
363 	    || (err = write_wtfile(gitdir.ptr, "gitdir", &buf)) < 0)
364 		goto out;
365 
366 	/* Set up worktree reference */
367 	if (wtopts.ref) {
368 		if ((err = git_reference_dup(&ref, wtopts.ref)) < 0)
369 			goto out;
370 	} else {
371 		if ((err = git_repository_head(&head, repo)) < 0)
372 			goto out;
373 		if ((err = git_commit_lookup(&commit, repo, &head->target.oid)) < 0)
374 			goto out;
375 		if ((err = git_branch_create(&ref, repo, name, commit, false)) < 0)
376 			goto out;
377 	}
378 
379 	/* Set worktree's HEAD */
380 	if ((err = git_repository_create_head(gitdir.ptr, git_reference_name(ref))) < 0)
381 		goto out;
382 	if ((err = git_repository_open(&wt, wddir.ptr)) < 0)
383 		goto out;
384 
385 	/* Checkout worktree's HEAD */
386 	coopts.checkout_strategy = GIT_CHECKOUT_FORCE;
387 	if ((err = git_checkout_head(wt, &coopts)) < 0)
388 		goto out;
389 
390 	/* Load result */
391 	if ((err = git_worktree_lookup(out, repo, name)) < 0)
392 		goto out;
393 
394 out:
395 	git_buf_dispose(&gitdir);
396 	git_buf_dispose(&wddir);
397 	git_buf_dispose(&buf);
398 	git_reference_free(ref);
399 	git_reference_free(head);
400 	git_commit_free(commit);
401 	git_repository_free(wt);
402 
403 	return err;
404 }
405 
git_worktree_lock(git_worktree * wt,const char * reason)406 int git_worktree_lock(git_worktree *wt, const char *reason)
407 {
408 	git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
409 	int error;
410 
411 	assert(wt);
412 
413 	if ((error = git_worktree_is_locked(NULL, wt)) < 0)
414 		goto out;
415 	if (error) {
416 		error = GIT_ELOCKED;
417 		goto out;
418 	}
419 
420 	if ((error = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
421 		goto out;
422 
423 	if (reason)
424 		git_buf_attach_notowned(&buf, reason, strlen(reason));
425 
426 	if ((error = git_futils_writebuffer(&buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0)
427 		goto out;
428 
429 	wt->locked = 1;
430 
431 out:
432 	git_buf_dispose(&path);
433 
434 	return error;
435 }
436 
git_worktree_unlock(git_worktree * wt)437 int git_worktree_unlock(git_worktree *wt)
438 {
439 	git_buf path = GIT_BUF_INIT;
440 	int error;
441 
442 	assert(wt);
443 
444 	if ((error = git_worktree_is_locked(NULL, wt)) < 0)
445 		return error;
446 	if (!error)
447 		return 1;
448 
449 	if (git_buf_joinpath(&path, wt->gitdir_path, "locked") < 0)
450 		return -1;
451 
452 	if (p_unlink(path.ptr) != 0) {
453 		git_buf_dispose(&path);
454 		return -1;
455 	}
456 
457 	wt->locked = 0;
458 
459 	git_buf_dispose(&path);
460 
461 	return 0;
462 }
463 
git_worktree_is_locked(git_buf * reason,const git_worktree * wt)464 int git_worktree_is_locked(git_buf *reason, const git_worktree *wt)
465 {
466 	git_buf path = GIT_BUF_INIT;
467 	int error, locked;
468 
469 	assert(wt);
470 
471 	if (reason)
472 		git_buf_clear(reason);
473 
474 	if ((error = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
475 		goto out;
476 	locked = git_path_exists(path.ptr);
477 	if (locked && reason &&
478 	    (error = git_futils_readbuffer(reason, path.ptr)) < 0)
479 		goto out;
480 
481 	error = locked;
482 out:
483 	git_buf_dispose(&path);
484 
485 	return error;
486 }
487 
git_worktree_name(const git_worktree * wt)488 const char *git_worktree_name(const git_worktree *wt)
489 {
490 	assert(wt);
491 	return wt->name;
492 }
493 
git_worktree_path(const git_worktree * wt)494 const char *git_worktree_path(const git_worktree *wt)
495 {
496 	assert(wt);
497 	return wt->worktree_path;
498 }
499 
git_worktree_prune_options_init(git_worktree_prune_options * opts,unsigned int version)500 int git_worktree_prune_options_init(
501 	git_worktree_prune_options *opts,
502 	unsigned int version)
503 {
504 	GIT_INIT_STRUCTURE_FROM_TEMPLATE(opts, version,
505 		git_worktree_prune_options, GIT_WORKTREE_PRUNE_OPTIONS_INIT);
506 	return 0;
507 }
508 
git_worktree_prune_init_options(git_worktree_prune_options * opts,unsigned int version)509 int git_worktree_prune_init_options(git_worktree_prune_options *opts,
510 	unsigned int version)
511 {
512 	return git_worktree_prune_options_init(opts, version);
513 }
514 
git_worktree_is_prunable(git_worktree * wt,git_worktree_prune_options * opts)515 int git_worktree_is_prunable(git_worktree *wt,
516 	git_worktree_prune_options *opts)
517 {
518 	git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
519 
520 	GIT_ERROR_CHECK_VERSION(
521 		opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION,
522 		"git_worktree_prune_options");
523 
524 	if (opts)
525 		memcpy(&popts, opts, sizeof(popts));
526 
527 	if ((popts.flags & GIT_WORKTREE_PRUNE_LOCKED) == 0) {
528 		git_buf reason = GIT_BUF_INIT;
529 		int error;
530 
531 		if ((error = git_worktree_is_locked(&reason, wt)) < 0)
532 			return error;
533 
534 		if (error) {
535 			if (!reason.size)
536 				git_buf_attach_notowned(&reason, "no reason given", 15);
537 			git_error_set(GIT_ERROR_WORKTREE, "not pruning locked working tree: '%s'", reason.ptr);
538 			git_buf_dispose(&reason);
539 			return 0;
540 		}
541 	}
542 
543 	if ((popts.flags & GIT_WORKTREE_PRUNE_VALID) == 0 &&
544 	    git_worktree_validate(wt) == 0) {
545 		git_error_set(GIT_ERROR_WORKTREE, "not pruning valid working tree");
546 		return 0;
547 	}
548 
549 	return 1;
550 }
551 
git_worktree_prune(git_worktree * wt,git_worktree_prune_options * opts)552 int git_worktree_prune(git_worktree *wt,
553 	git_worktree_prune_options *opts)
554 {
555 	git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
556 	git_buf path = GIT_BUF_INIT;
557 	char *wtpath;
558 	int err;
559 
560 	GIT_ERROR_CHECK_VERSION(
561 		opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION,
562 		"git_worktree_prune_options");
563 
564 	if (opts)
565 		memcpy(&popts, opts, sizeof(popts));
566 
567 	if (!git_worktree_is_prunable(wt, &popts)) {
568 		err = -1;
569 		goto out;
570 	}
571 
572 	/* Delete gitdir in parent repository */
573 	if ((err = git_buf_printf(&path, "%s/worktrees/%s", wt->commondir_path, wt->name)) < 0)
574 		goto out;
575 	if (!git_path_exists(path.ptr))
576 	{
577 		git_error_set(GIT_ERROR_WORKTREE, "worktree gitdir '%s' does not exist", path.ptr);
578 		err = -1;
579 		goto out;
580 	}
581 	if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0)
582 		goto out;
583 
584 	/* Skip deletion of the actual working tree if it does
585 	 * not exist or deletion was not requested */
586 	if ((popts.flags & GIT_WORKTREE_PRUNE_WORKING_TREE) == 0 ||
587 		!git_path_exists(wt->gitlink_path))
588 	{
589 		goto out;
590 	}
591 
592 	if ((wtpath = git_path_dirname(wt->gitlink_path)) == NULL)
593 		goto out;
594 	git_buf_attach(&path, wtpath, 0);
595 	if (!git_path_exists(path.ptr))
596 	{
597 		git_error_set(GIT_ERROR_WORKTREE, "working tree '%s' does not exist", path.ptr);
598 		err = -1;
599 		goto out;
600 	}
601 	if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0)
602 		goto out;
603 
604 out:
605 	git_buf_dispose(&path);
606 
607 	return err;
608 }
609