-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghrm.ps1
72 lines (57 loc) · 1.99 KB
/
ghrm.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<#
.SYNOPSIS
rm GitHub branches that have been merged
.DESCRIPTION
Searches local branches for matching GitHub PRs.
Delete local&remote branches that match a merged PR.
git branch --merged doesn't play well with squash commits: https://stackoverflow.com/a/19309568/771768
#>
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[string]$repo = $null
)
if ($repo) { throw "TODO: implement repo parameter" }
$script:ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$defBranch = Get-GitDefaultBranch
# TODO filtering out default branch feels like hack. Maybe want to go back to using the "commit was merged logic" from older version
$branches = git branch --format='%(refname:short)' | Where-Object { $_ -ne $defBranch }
$query = $branches | % { "head:$_" } | Join-String -Separator ' '
$result = gh pr list --search $query --state all --json 'state,headRefName' | ConvertFrom-Json
if ($ENV:GHRM_DEBUG) {
git branch --format='%(refname:short) %(objectname)'
""
$result = gh pr list --search $query --state all --json 'commits,state,headRefName' | ConvertFrom-Json
foreach ($r in $result) {
foreach ($c in $r.commits) {
foreach ($a in $c.authors) {
"$($r.state) $($r.headRefName) -- $($c.oid.SubString(0, 6)) $($a.login) $($c.messageHeadline)"
}
}
}
}
$branch2status = @{}
foreach ($r in $result) {
$branch2status[$r.headRefName] = $r.state
}
foreach ($branch in $branches) {
$status = $branch2status[$branch]
if (!$status) { continue }
$color = switch ($status) {
"OPEN" { "DarkYellow" }
"CLOSED" { "DarkRed" }
"MERGED" { "DarkGreen" }
default { "White" }
}
write-host ($branch + ": " + $status) -ForegroundColor $color
if ($status -ne 'MERGED') { continue }
$toDelete = $branch
if ($toDelete -eq $defBranch) {
throw "Can't delete default branch $Branch"
}
if ($toDelete -eq (Get-GitBranch)) {
git checkout $defBranch
git pull --recurse-submodules=false
}
DeleteLocalRemoteGitBranch $toDelete -ignoreRemoteNotFound
}