Blame src/worktree.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
Packit Service 20376f
#include "git2/branch.h"
Packit Service 20376f
#include "git2/commit.h"
Packit Service 20376f
#include "git2/worktree.h"
Packit Service 20376f
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
#include "worktree.h"
Packit Service 20376f
Packit Service 20376f
static bool is_worktree_dir(const char *dir)
Packit Service 20376f
{
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_sets(&buf, dir) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	error = git_path_contains_file(&buf, "commondir")
Packit Service 20376f
		&& git_path_contains_file(&buf, "gitdir")
Packit Service 20376f
		&& git_path_contains_file(&buf, "HEAD");
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_worktree_list(git_strarray *wts, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_vector worktrees = GIT_VECTOR_INIT;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	char *worktree;
Packit Service 20376f
	unsigned i, len;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(wts && repo);
Packit Service 20376f
Packit Service 20376f
	wts->count = 0;
Packit Service 20376f
	wts->strings = NULL;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_printf(&path, "%s/worktrees/", repo->commondir)) < 0)
Packit Service 20376f
		goto exit;
Packit Service 20376f
	if (!git_path_exists(path.ptr) || git_path_is_empty_dir(path.ptr))
Packit Service 20376f
		goto exit;
Packit Service 20376f
	if ((error = git_path_dirload(&worktrees, path.ptr, path.size, 0x0)) < 0)
Packit Service 20376f
		goto exit;
Packit Service 20376f
Packit Service 20376f
	len = path.size;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&worktrees, i, worktree) {
Packit Service 20376f
		git_buf_truncate(&path, len);
Packit Service 20376f
		git_buf_puts(&path, worktree);
Packit Service 20376f
Packit Service 20376f
		if (!is_worktree_dir(path.ptr)) {
Packit Service 20376f
			git_vector_remove(&worktrees, i);
Packit Service 20376f
			git__free(worktree);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	wts->strings = (char **)git_vector_detach(&wts->count, NULL, &worktrees);
Packit Service 20376f
Packit Service 20376f
exit:
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
char *git_worktree__read_link(const char *base, const char *file)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT, buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	assert(base && file);
Packit Service 20376f
Packit Service 20376f
	if (git_buf_joinpath(&path, base, file) < 0)
Packit Service 20376f
		goto err;
Packit Service 20376f
	if (git_futils_readbuffer(&buf, path.ptr) < 0)
Packit Service 20376f
		goto err;
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	git_buf_rtrim(&buf;;
Packit Service 20376f
Packit Service 20376f
	if (!git_path_is_relative(buf.ptr))
Packit Service 20376f
		return git_buf_detach(&buf;;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_sets(&path, base) < 0)
Packit Service 20376f
		goto err;
Packit Service 20376f
	if (git_path_apply_relative(&path, buf.ptr) < 0)
Packit Service 20376f
		goto err;
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
Packit Service 20376f
	return git_buf_detach(&path);
Packit Service 20376f
Packit Service 20376f
err:
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int write_wtfile(const char *base, const char *file, const git_buf *buf)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	int err;
Packit Service 20376f
Packit Service 20376f
	assert(base && file && buf);
Packit Service 20376f
Packit Service 20376f
	if ((err = git_buf_joinpath(&path, base, file)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	if ((err = git_futils_writebuffer(buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return err;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int open_worktree_dir(git_worktree **out, const char *parent, const char *dir, const char *name)
Packit Service 20376f
{
Packit Service 20376f
	git_buf gitdir = GIT_BUF_INIT;
Packit Service 20376f
	git_worktree *wt = NULL;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (!is_worktree_dir(dir)) {
Packit Service 20376f
		error = -1;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((wt = git__calloc(1, sizeof(struct git_repository))) == NULL) {
Packit Service 20376f
		error = -1;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((wt->name = git__strdup(name)) == NULL
Packit Service 20376f
	    || (wt->commondir_path = git_worktree__read_link(dir, "commondir")) == NULL
Packit Service 20376f
	    || (wt->gitlink_path = git_worktree__read_link(dir, "gitdir")) == NULL
Packit Service 20376f
	    || (wt->parent_path = git__strdup(parent)) == NULL) {
Packit Service 20376f
		error = -1;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_path_prettify_dir(&gitdir, dir, NULL)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
	wt->gitdir_path = git_buf_detach(&gitdir);
Packit Service 20376f
Packit Service 20376f
	wt->locked = !!git_worktree_is_locked(NULL, wt);
Packit Service 20376f
Packit Service 20376f
	*out = wt;
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	if (error)
Packit Service 20376f
		git_worktree_free(wt);
Packit Service 20376f
	git_buf_free(&gitdir);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_worktree_lookup(git_worktree **out, git_repository *repo, const char *name)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	git_worktree *wt = NULL;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(repo && name);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_printf(&path, "%s/worktrees/%s", repo->commondir, name)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	if ((error = (open_worktree_dir(out, git_repository_workdir(repo), path.ptr, name))) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	if (error)
Packit Service 20376f
		git_worktree_free(wt);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_worktree_open_from_repository(git_worktree **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_buf parent = GIT_BUF_INIT;
Packit Service 20376f
	const char *gitdir, *commondir;
Packit Service 20376f
	char *name = NULL;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (!git_repository_is_worktree(repo)) {
Packit Service 20376f
		giterr_set(GITERR_WORKTREE, "cannot open worktree of a non-worktree repo");
Packit Service 20376f
		error = -1;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	gitdir = git_repository_path(repo);
Packit Service 20376f
	commondir = git_repository_commondir(repo);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_path_prettify_dir(&parent, "..", commondir)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	/* The name is defined by the last component in '.git/worktree/%s' */
Packit Service 20376f
	name = git_path_basename(gitdir);
Packit Service 20376f
Packit Service 20376f
	if ((error = open_worktree_dir(out, parent.ptr, gitdir, name)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	git__free(name);
Packit Service 20376f
	git_buf_free(&parent);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_worktree_free(git_worktree *wt)
Packit Service 20376f
{
Packit Service 20376f
	if (!wt)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	git__free(wt->commondir_path);
Packit Service 20376f
	git__free(wt->gitlink_path);
Packit Service 20376f
	git__free(wt->gitdir_path);
Packit Service 20376f
	git__free(wt->parent_path);
Packit Service 20376f
	git__free(wt->name);
Packit Service 20376f
	git__free(wt);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_worktree_validate(const git_worktree *wt)
Packit Service 20376f
{
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
	int err = 0;
Packit Service 20376f
Packit Service 20376f
	assert(wt);
Packit Service 20376f
Packit Service 20376f
	git_buf_puts(&buf, wt->gitdir_path);
Packit Service 20376f
	if (!is_worktree_dir(buf.ptr)) {
Packit Service 20376f
		giterr_set(GITERR_WORKTREE,
Packit Service 20376f
			"Worktree gitdir ('%s') is not valid",
Packit Service 20376f
			wt->gitlink_path);
Packit Service 20376f
		err = -1;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!git_path_exists(wt->parent_path)) {
Packit Service 20376f
		giterr_set(GITERR_WORKTREE,
Packit Service 20376f
			"Worktree parent directory ('%s') does not exist ",
Packit Service 20376f
			wt->parent_path);
Packit Service 20376f
		err = -2;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!git_path_exists(wt->commondir_path)) {
Packit Service 20376f
		giterr_set(GITERR_WORKTREE,
Packit Service 20376f
			"Worktree common directory ('%s') does not exist ",
Packit Service 20376f
			wt->commondir_path);
Packit Service 20376f
		err = -3;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
Packit Service 20376f
	return err;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_worktree_add_init_options(git_worktree_add_options *opts,
Packit Service 20376f
	unsigned int version)
Packit Service 20376f
{
Packit Service 20376f
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(opts, version,
Packit Service 20376f
		git_worktree_add_options, GIT_WORKTREE_ADD_OPTIONS_INIT);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_worktree_add(git_worktree **out, git_repository *repo,
Packit Service 20376f
	const char *name, const char *worktree,
Packit Service 20376f
	const git_worktree_add_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_buf gitdir = GIT_BUF_INIT, wddir = GIT_BUF_INIT, buf = GIT_BUF_INIT;
Packit Service 20376f
	git_reference *ref = NULL, *head = NULL;
Packit Service 20376f
	git_commit *commit = NULL;
Packit Service 20376f
	git_repository *wt = NULL;
Packit Service 20376f
	git_checkout_options coopts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	git_worktree_add_options wtopts = GIT_WORKTREE_ADD_OPTIONS_INIT;
Packit Service 20376f
	int err;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_VERSION(
Packit Service 20376f
		opts, GIT_WORKTREE_ADD_OPTIONS_VERSION, "git_worktree_add_options");
Packit Service 20376f
Packit Service 20376f
	if (opts)
Packit Service 20376f
		memcpy(&wtopts, opts, sizeof(wtopts));
Packit Service 20376f
Packit Service 20376f
	assert(out && repo && name && worktree);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	/* Create gitdir directory ".git/worktrees/<name>" */
Packit Service 20376f
	if ((err = git_buf_joinpath(&gitdir, repo->commondir, "worktrees")) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
	if (!git_path_exists(gitdir.ptr))
Packit Service 20376f
		if ((err = git_futils_mkdir(gitdir.ptr, 0755, GIT_MKDIR_EXCL)) < 0)
Packit Service 20376f
			goto out;
Packit Service 20376f
	if ((err = git_buf_joinpath(&gitdir, gitdir.ptr, name)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
	if ((err = git_futils_mkdir(gitdir.ptr, 0755, GIT_MKDIR_EXCL)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
	if ((err = git_path_prettify_dir(&gitdir, gitdir.ptr, NULL)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	/* Create worktree work dir */
Packit Service 20376f
	if ((err = git_futils_mkdir(worktree, 0755, GIT_MKDIR_EXCL)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
	if ((err = git_path_prettify_dir(&wddir, worktree, NULL)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	if (wtopts.lock) {
Packit Service 20376f
		int fd;
Packit Service 20376f
Packit Service 20376f
		if ((err = git_buf_joinpath(&buf, gitdir.ptr, "locked")) < 0)
Packit Service 20376f
			goto out;
Packit Service 20376f
Packit Service 20376f
		if ((fd = p_creat(buf.ptr, 0644)) < 0) {
Packit Service 20376f
			err = fd;
Packit Service 20376f
			goto out;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		p_close(fd);
Packit Service 20376f
		git_buf_clear(&buf;;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Create worktree .git file */
Packit Service 20376f
	if ((err = git_buf_printf(&buf, "gitdir: %s\n", gitdir.ptr)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
	if ((err = write_wtfile(wddir.ptr, ".git", &buf)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	/* Create gitdir files */
Packit Service 20376f
	if ((err = git_path_prettify_dir(&buf, repo->commondir, NULL) < 0)
Packit Service 20376f
	    || (err = git_buf_putc(&buf, '\n')) < 0
Packit Service 20376f
	    || (err = write_wtfile(gitdir.ptr, "commondir", &buf)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
	if ((err = git_buf_joinpath(&buf, wddir.ptr, ".git")) < 0
Packit Service 20376f
	    || (err = git_buf_putc(&buf, '\n')) < 0
Packit Service 20376f
	    || (err = write_wtfile(gitdir.ptr, "gitdir", &buf)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	/* Create new branch */
Packit Service 20376f
	if ((err = git_repository_head(&head, repo)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
	if ((err = git_commit_lookup(&commit, repo, &head->target.oid)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
	if ((err = git_branch_create(&ref, repo, name, commit, false)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	/* Set worktree's HEAD */
Packit Service 20376f
	if ((err = git_repository_create_head(gitdir.ptr, git_reference_name(ref))) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
	if ((err = git_repository_open(&wt, wddir.ptr)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	/* Checkout worktree's HEAD */
Packit Service 20376f
	coopts.checkout_strategy = GIT_CHECKOUT_FORCE;
Packit Service 20376f
	if ((err = git_checkout_head(wt, &coopts)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	/* Load result */
Packit Service 20376f
	if ((err = git_worktree_lookup(out, repo, name)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	git_buf_free(&gitdir);
Packit Service 20376f
	git_buf_free(&wddir);
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
	git_reference_free(ref);
Packit Service 20376f
	git_reference_free(head);
Packit Service 20376f
	git_commit_free(commit);
Packit Service 20376f
	git_repository_free(wt);
Packit Service 20376f
Packit Service 20376f
	return err;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_worktree_lock(git_worktree *wt, char *creason)
Packit Service 20376f
{
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
Packit Service 20376f
	int err;
Packit Service 20376f
Packit Service 20376f
	assert(wt);
Packit Service 20376f
Packit Service 20376f
	if ((err = git_worktree_is_locked(NULL, wt)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	if ((err = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	if (creason)
Packit Service 20376f
		git_buf_attach_notowned(&buf, creason, strlen(creason));
Packit Service 20376f
Packit Service 20376f
	if ((err = git_futils_writebuffer(&buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	wt->locked = 1;
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return err;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_worktree_unlock(git_worktree *wt)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	assert(wt);
Packit Service 20376f
Packit Service 20376f
	if (!git_worktree_is_locked(NULL, wt))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_joinpath(&path, wt->gitdir_path, "locked") < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (p_unlink(path.ptr) != 0) {
Packit Service 20376f
		git_buf_free(&path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	wt->locked = 0;
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_worktree_is_locked(git_buf *reason, const git_worktree *wt)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	assert(wt);
Packit Service 20376f
Packit Service 20376f
	if (reason)
Packit Service 20376f
		git_buf_clear(reason);
Packit Service 20376f
Packit Service 20376f
	if ((ret = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
	if ((ret = git_path_exists(path.ptr)) && reason)
Packit Service 20376f
		git_futils_readbuffer(reason, path.ptr);
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_worktree_prune_init_options(
Packit Service 20376f
	git_worktree_prune_options *opts,
Packit Service 20376f
	unsigned int version)
Packit Service 20376f
{
Packit Service 20376f
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(opts, version,
Packit Service 20376f
		git_worktree_prune_options, GIT_WORKTREE_PRUNE_OPTIONS_INIT);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_worktree_is_prunable(git_worktree *wt,
Packit Service 20376f
	git_worktree_prune_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_buf reason = GIT_BUF_INIT;
Packit Service 20376f
	git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_VERSION(
Packit Service 20376f
		opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION,
Packit Service 20376f
		"git_worktree_prune_options");
Packit Service 20376f
Packit Service 20376f
	if (opts)
Packit Service 20376f
		memcpy(&popts, opts, sizeof(popts));
Packit Service 20376f
Packit Service 20376f
	if ((popts.flags & GIT_WORKTREE_PRUNE_LOCKED) == 0 &&
Packit Service 20376f
		git_worktree_is_locked(&reason, wt))
Packit Service 20376f
	{
Packit Service 20376f
		if (!reason.size)
Packit Service 20376f
			git_buf_attach_notowned(&reason, "no reason given", 15);
Packit Service 20376f
		giterr_set(GITERR_WORKTREE, "Not pruning locked working tree: '%s'", reason.ptr);
Packit Service 20376f
		git_buf_free(&reason);
Packit Service 20376f
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((popts.flags & GIT_WORKTREE_PRUNE_VALID) == 0 &&
Packit Service 20376f
		git_worktree_validate(wt) == 0)
Packit Service 20376f
	{
Packit Service 20376f
		giterr_set(GITERR_WORKTREE, "Not pruning valid working tree");
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_worktree_prune(git_worktree *wt,
Packit Service 20376f
	git_worktree_prune_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	char *wtpath;
Packit Service 20376f
	int err;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_VERSION(
Packit Service 20376f
		opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION,
Packit Service 20376f
		"git_worktree_prune_options");
Packit Service 20376f
Packit Service 20376f
	if (opts)
Packit Service 20376f
		memcpy(&popts, opts, sizeof(popts));
Packit Service 20376f
Packit Service 20376f
	if (!git_worktree_is_prunable(wt, &popts)) {
Packit Service 20376f
		err = -1;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Delete gitdir in parent repository */
Packit Service 20376f
	if ((err = git_buf_printf(&path, "%s/worktrees/%s", wt->commondir_path, wt->name)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
	if (!git_path_exists(path.ptr))
Packit Service 20376f
	{
Packit Service 20376f
		giterr_set(GITERR_WORKTREE, "Worktree gitdir '%s' does not exist", path.ptr);
Packit Service 20376f
		err = -1;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
	if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	/* Skip deletion of the actual working tree if it does
Packit Service 20376f
	 * not exist or deletion was not requested */
Packit Service 20376f
	if ((popts.flags & GIT_WORKTREE_PRUNE_WORKING_TREE) == 0 ||
Packit Service 20376f
		!git_path_exists(wt->gitlink_path))
Packit Service 20376f
	{
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((wtpath = git_path_dirname(wt->gitlink_path)) == NULL)
Packit Service 20376f
		goto out;
Packit Service 20376f
	git_buf_attach(&path, wtpath, 0);
Packit Service 20376f
	if (!git_path_exists(path.ptr))
Packit Service 20376f
	{
Packit Service 20376f
		giterr_set(GITERR_WORKTREE, "Working tree '%s' does not exist", path.ptr);
Packit Service 20376f
		err = -1;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
	if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return err;
Packit Service 20376f
}