-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-tool-audit-prune.ps1
executable file
·120 lines (103 loc) · 3.36 KB
/
git-tool-audit-prune.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env pwsh
Param(
[switch] $noFetch,
[switch] $quiet,
[switch] $dryRun
)
Import-Module -Scope Local "$PSScriptRoot/utils/query-state.psm1"
Import-Module -Scope Local "$PSScriptRoot/utils/framework.psm1"
Import-Module -Scope Local "$PSScriptRoot/utils/actions.psm1"
$diagnostics = New-Diagnostics
$config = Get-Configuration
if (-not $noFetch) {
Update-GitRemote -quiet:$quiet
}
$commonParams = @{
diagnostics = $diagnostics
}
# Get all branches in upstreams
$originalUpstreams = Invoke-LocalAction @commonParams @{
type = 'get-all-upstreams'
parameters= @{}
}
Assert-Diagnostics $diagnostics
# Get branches that actually exist
$allBranches = Select-Branches
# For all keys (downstream) in the upstreams:
# - If the downstream does not exist, replace it with its downstreams in all other upstreams
[string[]]$configuredBranches = @() + $originalUpstreams.Keys
$resultUpstreams = @{}
foreach ($branch in $configuredBranches) {
if ($branch -in $allBranches) { continue }
[string[]]$upstreams = $resultUpstreams[$branch] ?? $originalUpstreams[$branch]
foreach ($downstream in $configuredBranches) {
[string[]]$initial = $resultUpstreams[$downstream] ?? $originalUpstreams[$downstream]
if ($branch -notin $initial) { continue }
$resultUpstreams[$downstream] = Invoke-LocalAction @commonParams @{
type = 'filter-branches'
parameters = @{
include = $initial + $upstreams
exclude = @($branch)
}
}
}
}
# For all keys (downstream) in the upstreams:
# - Remove entire branch configuration if the branch does not exist
# - Remove upstreams that do not exist
foreach ($branch in $configuredBranches) {
if ($branch -notin $allBranches) {
$resultUpstreams[$branch] = $null
continue
}
[string[]]$upstreams = $resultUpstreams[$branch] ?? $originalUpstreams[$branch]
[string[]]$resultUpstream = @()
foreach ($upstream in $upstreams) {
if ($upstream -in $allBranches) {
$resultUpstream = $resultUpstream + @($upstream)
}
}
}
# Simplify changed upstreams
foreach ($branch in $configuredBranches) {
if (-not $resultUpstreams[$branch]) { continue }
[string[]]$result = Invoke-LocalAction @commonParams @{
type = 'simplify-upstream'
parameters = @{
upstreamBranches = $resultUpstreams[$branch]
overrideUpstreams = $resultUpstreams
branchName = $branch
}
}
if ($result.length -ne ([string[]]$resultUpstreams[$branch]).length) {
$resultUpstreams[$branch] = $result
}
}
Assert-Diagnostics $diagnostics
# Set upstream branch
if ($resultUpstreams.Count -ne 0) {
$upstreamHash = Invoke-LocalAction @commonParams @{
type = 'set-upstream'
parameters = @{
upstreamBranches = $resultUpstreams
message = "Applied changes from 'prune' audit"
}
}
Assert-Diagnostics $diagnostics
}
# Finalize: Push upstream branch
$commonParams = @{
diagnostics = $diagnostics
dryRun = $dryRun
}
if ($resultUpstreams.Count -ne 0) {
Invoke-FinalizeAction @commonParams @{
type = 'set-branches'
parameters = @{
branches = @{
"$($config.upstreamBranch)" = $upstreamHash.commit
}
}
}
Assert-Diagnostics $diagnostics
}