Skip to content

Commit

Permalink
Add an option to delete branches with closed status (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
seachicken authored Nov 22, 2024
1 parent 95dae5f commit 15a62bf
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 46 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ gh extension install seachicken/gh-poi
## Usage

- `gh poi` Delete the merged local branches
- `gh poi --state (closed|merged)` Specify the PR state to delete (default merged)
- `gh poi --dry-run` You can check the branch to be deleted without actually deleting it
- `gh poi --debug` Enable debug logs
- `gh poi protect <branchname>...` Protect local branches from deletion
Expand Down
22 changes: 14 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func GetRemote(ctx context.Context, connection shared.Connection) (shared.Remote
}
}

func GetBranches(ctx context.Context, remote shared.Remote, connection shared.Connection, dryRun bool) ([]shared.
func GetBranches(ctx context.Context, remote shared.Remote, connection shared.Connection, state shared.PullRequestState, dryRun bool) ([]shared.
Branch, error) {
var repoNames []string
var defaultBranchName string
Expand All @@ -76,7 +76,7 @@ func GetBranches(ctx context.Context, remote shared.Remote, connection shared.Co
return nil, err
}

branches = checkDeletion(branches)
branches = checkDeletion(branches, state)

branches, err = switchToDefaultBranchIfDeleted(ctx, branches, defaultBranchName, connection, dryRun)
if err != nil {
Expand Down Expand Up @@ -441,16 +441,16 @@ func toUncommittedChange(changes []string) []UncommittedChange {
return results
}

func checkDeletion(branches []shared.Branch) []shared.Branch {
func checkDeletion(branches []shared.Branch, state shared.PullRequestState) []shared.Branch {
results := []shared.Branch{}
for _, branch := range branches {
branch.State = getDeleteStatus(branch)
branch.State = getDeleteStatus(branch, state)
results = append(results, branch)
}
return results
}

func getDeleteStatus(branch shared.Branch) shared.BranchState {
func getDeleteStatus(branch shared.Branch, state shared.PullRequestState) shared.BranchState {
if branch.IsProtected {
return shared.NotDeletable
}
Expand All @@ -468,7 +468,7 @@ func getDeleteStatus(branch shared.Branch) shared.BranchState {
if pr.State == shared.Open {
return shared.NotDeletable
}
if isFullyMerged(branch, pr) {
if isFullyMerged(branch, pr, state) {
fullyMergedCnt++
}
}
Expand All @@ -479,8 +479,14 @@ func getDeleteStatus(branch shared.Branch) shared.BranchState {
return shared.Deletable
}

func isFullyMerged(branch shared.Branch, pr shared.PullRequest) bool {
if pr.State != shared.Merged || len(branch.Commits) == 0 {
func isFullyMerged(branch shared.Branch, pr shared.PullRequest, state shared.PullRequestState) bool {
if len(branch.Commits) == 0 {
return false
}
if (state == shared.Merged && pr.State != shared.Merged) ||
// In the GitHub interface, closed status includes merged status, so we make it behave the same way.
// https://github.com/cli/cli/issues/8102
(state == shared.Closed && pr.State != shared.Closed && pr.State != shared.Merged) {
return false
}

Expand Down
Loading

0 comments on commit 15a62bf

Please sign in to comment.