-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghrm.ps1
62 lines (50 loc) · 1.71 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
<#
.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
#>
$script:ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$defBranch = Get-GitDefaultBranch
$branches = @{}
foreach ($line in git branch --format='%(refname:short) %(objectname)') {
$name, $hash = $line -split ' '
if ($name -eq $defBranch) { continue }
$branches[$name] = $hash
}
$query = $branches.Keys | % { "head:$_" } | Join-String -Separator ' '
$result = gh pr list --search $query --state all --json 'commits,state' | ConvertFrom-Json
write-warning "TODO should filter that the local branch name exactly matches the PR branch" # TODO
$commit2status = @{}
foreach ($r in $result) {
foreach ($c in $r.commits) {
$commit2status[$c.oid] = $r.state
}
}
foreach ($b in $branches.GetEnumerator() | Sort-Object -Property Name) {
$status = $commit2status[$b.Value] ?? "no-pr"
if ($status -eq 'no-pr') { continue }
$color = switch ($status) {
# "no-pr" { "Gray" }
"OPEN" { "DarkYellow" }
"CLOSED" { "DarkRed" }
"MERGED" { "DarkGreen" }
default { "White" }
}
write-host ($b.Key + ": " + $status) -ForegroundColor $color
}
foreach ($b in $branches.GetEnumerator() | Sort-Object -Property Name) {
if ($commit2status[$b.Value] -ne 'MERGED') { continue }
$toDelete = $b.Key
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
}