diff --git a/GitPullRequest/GitPullRequest.csproj b/GitPullRequest/GitPullRequest.csproj index 7c72576..e7e45f8 100644 --- a/GitPullRequest/GitPullRequest.csproj +++ b/GitPullRequest/GitPullRequest.csproj @@ -10,14 +10,14 @@ true - 1.0.5 + 1.0.6 7.3 - + diff --git a/GitPullRequest/Program.cs b/GitPullRequest/Program.cs index a7e999d..dc419e2 100644 --- a/GitPullRequest/Program.cs +++ b/GitPullRequest/Program.cs @@ -27,6 +27,9 @@ public static int Main(string[] args) [Option("--all", Description = "List all branches with associated pull requests")] public bool All { get; } + [Option("--prune", Description = "Remove pull requests with deleted remote branches")] + public bool Prune { get; } + void OnExecute() { var repoPath = Repository.Discover(TargetDir); @@ -39,6 +42,12 @@ void OnExecute() var service = new GitPullRequestService(); using (var repo = new Repository(repoPath)) { + if (Prune) + { + PruneBranches(service, repo); + return; + } + if (List || All) { ListBranches(service, repo); @@ -122,6 +131,36 @@ void ListBranches(GitPullRequestService service, Repository repo) } } + void PruneBranches(GitPullRequestService service, Repository repo) + { + var gitHubRepositories = service.GetGitHubRepositories(repo); + var prs = repo.Branches + .Where(b => !b.IsRemote) + .SelectMany(b => service.FindPullRequests(gitHubRepositories, b), (b, p) => (Branch: b, PullRequest: p)) + .Where(bp => bp.PullRequest.IsDeleted) + .Where(bp => PullRequestNumber == 0 || bp.PullRequest.Number == PullRequestNumber) + .ToList(); + + if (prs.Count == 0) + { + Console.WriteLine($"Couldn't find any pull requests with deleted remote branches to remove"); + return; + } + + foreach (var bp in prs) + { + if (bp.Branch.IsCurrentRepositoryHead) + { + Console.WriteLine($"Can't remove current repository head #{bp.PullRequest.Number} {bp.Branch.FriendlyName}"); + } + else + { + Console.WriteLine($"Removing #{bp.PullRequest.Number} {bp.Branch.FriendlyName}"); + repo.Branches.Remove(bp.Branch); + } + } + } + void Browse(string pullUrl) { Process.Start(new ProcessStartInfo