worktree: extract git_worktree_is_prunable

This commit is contained in:
Patrick Steinhardt 2017-02-03 13:52:23 +01:00
parent 3f3a4ce7bc
commit 1ba242c9ab
2 changed files with 35 additions and 7 deletions

View File

@ -125,6 +125,24 @@ typedef enum {
GIT_WORKTREE_PRUNE_WORKING_TREE = 1u << 2, GIT_WORKTREE_PRUNE_WORKING_TREE = 1u << 2,
} git_worktree_prune_t; } git_worktree_prune_t;
/**
* Is the worktree prunable with the given set of flags?
*
* A worktree is not prunable in the following scenarios:
*
* - the worktree is linking to a valid on-disk worktree. The
* GIT_WORKTREE_PRUNE_VALID flag will cause this check to be
* ignored.
* - the worktree is not valid but locked. The
* GIT_WORKRTEE_PRUNE_LOCKED flag will cause this check to be
* ignored.
*
* If the worktree is not valid and not locked or if the above
* flags have been passed in, this function will return a
* positive value.
*/
GIT_EXTERN(int) git_worktree_is_prunable(git_worktree *wt, unsigned flags);
/** /**
* Prune working tree * Prune working tree
* *

View File

@ -357,11 +357,9 @@ out:
return ret; return ret;
} }
int git_worktree_prune(git_worktree *wt, unsigned flags) int git_worktree_is_prunable(git_worktree *wt, unsigned flags)
{ {
git_buf reason = GIT_BUF_INIT, path = GIT_BUF_INIT; git_buf reason = GIT_BUF_INIT;
char *wtpath;
int err;
if ((flags & GIT_WORKTREE_PRUNE_LOCKED) == 0 && if ((flags & GIT_WORKTREE_PRUNE_LOCKED) == 0 &&
git_worktree_is_locked(&reason, wt)) git_worktree_is_locked(&reason, wt))
@ -369,15 +367,28 @@ int git_worktree_prune(git_worktree *wt, unsigned flags)
if (!reason.size) if (!reason.size)
git_buf_attach_notowned(&reason, "no reason given", 15); git_buf_attach_notowned(&reason, "no reason given", 15);
giterr_set(GITERR_WORKTREE, "Not pruning locked working tree: '%s'", reason.ptr); giterr_set(GITERR_WORKTREE, "Not pruning locked working tree: '%s'", reason.ptr);
git_buf_free(&reason);
err = -1; return 0;
goto out;
} }
if ((flags & GIT_WORKTREE_PRUNE_VALID) == 0 && if ((flags & GIT_WORKTREE_PRUNE_VALID) == 0 &&
git_worktree_validate(wt) == 0) git_worktree_validate(wt) == 0)
{ {
giterr_set(GITERR_WORKTREE, "Not pruning valid working tree"); giterr_set(GITERR_WORKTREE, "Not pruning valid working tree");
return 0;
}
return 1;
}
int git_worktree_prune(git_worktree *wt, unsigned flags)
{
git_buf path = GIT_BUF_INIT;
char *wtpath;
int err;
if (!git_worktree_is_prunable(wt, flags)) {
err = -1; err = -1;
goto out; goto out;
} }
@ -415,7 +426,6 @@ int git_worktree_prune(git_worktree *wt, unsigned flags)
goto out; goto out;
out: out:
git_buf_free(&reason);
git_buf_free(&path); git_buf_free(&path);
return err; return err;