diff options
| author | Patrick Steinhardt <ps@pks.im> | 2015-10-21 13:53:18 +0200 |
|---|---|---|
| committer | Patrick Steinhardt <ps@pks.im> | 2017-02-13 11:02:03 +0100 |
| commit | f0cfc34105fd68af9eb6e2024459c40c45e7d3a0 (patch) | |
| tree | f8487a9f042180a253e79fcc6be7c19a744b2f5f /src/worktree.c | |
| parent | 2a503485fae6c93c76bd0465c8b3fad5d9e19f6d (diff) | |
| download | libgit2-f0cfc34105fd68af9eb6e2024459c40c45e7d3a0.tar.gz | |
worktree: implement `git_worktree_prune`
Implement the `git_worktree_prune` function. This function can be
used to delete working trees from a repository. According to the
flags passed to it, it can either delete the working tree's
gitdir only or both gitdir and the working directory.
Diffstat (limited to 'src/worktree.c')
| -rw-r--r-- | src/worktree.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/worktree.c b/src/worktree.c index fa5a916c0..95a2757fe 100644 --- a/src/worktree.c +++ b/src/worktree.c @@ -356,3 +356,67 @@ out: return ret; } + +int git_worktree_prune(git_worktree *wt, unsigned flags) +{ + git_buf reason = GIT_BUF_INIT, path = GIT_BUF_INIT; + char *wtpath; + int err; + + if ((flags & GIT_WORKTREE_PRUNE_LOCKED) == 0 && + git_worktree_is_locked(&reason, wt)) + { + if (!reason.size) + git_buf_attach_notowned(&reason, "no reason given", 15); + giterr_set(GITERR_WORKTREE, "Not pruning locked working tree: '%s'", reason.ptr); + + err = -1; + goto out; + } + + if ((flags & GIT_WORKTREE_PRUNE_VALID) == 0 && + git_worktree_validate(wt) == 0) + { + giterr_set(GITERR_WORKTREE, "Not pruning valid working tree"); + err = -1; + goto out; + } + + /* Delete gitdir in parent repository */ + if ((err = git_buf_printf(&path, "%s/worktrees/%s", wt->parent_path, wt->name)) < 0) + goto out; + if (!git_path_exists(path.ptr)) + { + giterr_set(GITERR_WORKTREE, "Worktree gitdir '%s' does not exist", path.ptr); + err = -1; + goto out; + } + if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0) + goto out; + + /* Skip deletion of the actual working tree if it does + * not exist or deletion was not requested */ + if ((flags & GIT_WORKTREE_PRUNE_WORKING_TREE) == 0 || + !git_path_exists(wt->gitlink_path)) + { + goto out; + } + + if ((wtpath = git_path_dirname(wt->gitlink_path)) == NULL) + goto out; + git_buf_attach(&path, wtpath, 0); + if (!git_path_exists(path.ptr)) + { + giterr_set(GITERR_WORKTREE, "Working tree '%s' does not exist", path.ptr); + err = -1; + goto out; + } + if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0) + goto out; + +out: + git_buf_free(&reason); + git_buf_free(&path); + + return err; +} |
