Skip to content

Commit 1ba242c

Browse files
committed
worktree: extract git_worktree_is_prunable
1 parent 3f3a4ce commit 1ba242c

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

include/git2/worktree.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ typedef enum {
125125
GIT_WORKTREE_PRUNE_WORKING_TREE = 1u << 2,
126126
} git_worktree_prune_t;
127127

128+
/**
129+
* Is the worktree prunable with the given set of flags?
130+
*
131+
* A worktree is not prunable in the following scenarios:
132+
*
133+
* - the worktree is linking to a valid on-disk worktree. The
134+
* GIT_WORKTREE_PRUNE_VALID flag will cause this check to be
135+
* ignored.
136+
* - the worktree is not valid but locked. The
137+
* GIT_WORKRTEE_PRUNE_LOCKED flag will cause this check to be
138+
* ignored.
139+
*
140+
* If the worktree is not valid and not locked or if the above
141+
* flags have been passed in, this function will return a
142+
* positive value.
143+
*/
144+
GIT_EXTERN(int) git_worktree_is_prunable(git_worktree *wt, unsigned flags);
145+
128146
/**
129147
* Prune working tree
130148
*

src/worktree.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,27 +357,38 @@ int git_worktree_is_locked(git_buf *reason, const git_worktree *wt)
357357
return ret;
358358
}
359359

360-
int git_worktree_prune(git_worktree *wt, unsigned flags)
360+
int git_worktree_is_prunable(git_worktree *wt, unsigned flags)
361361
{
362-
git_buf reason = GIT_BUF_INIT, path = GIT_BUF_INIT;
363-
char *wtpath;
364-
int err;
362+
git_buf reason = GIT_BUF_INIT;
365363

366364
if ((flags & GIT_WORKTREE_PRUNE_LOCKED) == 0 &&
367365
git_worktree_is_locked(&reason, wt))
368366
{
369367
if (!reason.size)
370368
git_buf_attach_notowned(&reason, "no reason given", 15);
371369
giterr_set(GITERR_WORKTREE, "Not pruning locked working tree: '%s'", reason.ptr);
370+
git_buf_free(&reason);
372371

373-
err = -1;
374-
goto out;
372+
return 0;
375373
}
376374

377375
if ((flags & GIT_WORKTREE_PRUNE_VALID) == 0 &&
378376
git_worktree_validate(wt) == 0)
379377
{
380378
giterr_set(GITERR_WORKTREE, "Not pruning valid working tree");
379+
return 0;
380+
}
381+
382+
return 1;
383+
}
384+
385+
int git_worktree_prune(git_worktree *wt, unsigned flags)
386+
{
387+
git_buf path = GIT_BUF_INIT;
388+
char *wtpath;
389+
int err;
390+
391+
if (!git_worktree_is_prunable(wt, flags)) {
381392
err = -1;
382393
goto out;
383394
}
@@ -415,7 +426,6 @@ int git_worktree_prune(git_worktree *wt, unsigned flags)
415426
goto out;
416427

417428
out:
418-
git_buf_free(&reason);
419429
git_buf_free(&path);
420430

421431
return err;

0 commit comments

Comments
 (0)