-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.ps1
More file actions
167 lines (140 loc) · 4.72 KB
/
sync.ps1
File metadata and controls
167 lines (140 loc) · 4.72 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
$branchPattern = [Regex]::new('(?<=ref: refs/heads/)\S+(?=\s+HEAD)')
$shaPattern = [Regex]::new('\b[0-9a-f]{40}\b')
class BranchInfo {
[string] $SHA
[string] $Name
BranchInfo($sha, $name) {
$this.SHA = $sha
$this.Name = $name
}
}
function Get-CurrentLocation {
[CmdletBinding()]
param ()
begin {
}
process {
(Get-Item .).FullName
}
end {
}
}
function Get-DefaultBranch {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0 )]
[string]$Url
)
$output = git ls-remote --symref $Url HEAD
if ($output) {
[BranchInfo]::new($shaPattern.Match($output).Value, $branchPattern.Match($output).Value)
}
}
function Get-OutdatedRemotes {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0 )]
$project,
[Parameter(Mandatory = $true, Position = 1 )]
$masterBranch
)
$project.dst | ? {
$remoteName = $_.name
$remoteUrl = $_.url
Write-Host "`t$remoteName " -ForegroundColor Cyan -NoNewline
$branchRemote = Get-DefaultBranch $remoteUrl
if ($branchRemote) {
if ($branchRemote.SHA -eq $masterBranch.SHA) {
Write-Host "[OK]" -ForegroundColor Green
}
else {
Write-Host "[MISSMATCH]" -ForegroundColor Red
Write-Host "Master SHA: $($masterBranch.SHA)" -ForegroundColor Yellow
Write-Host "Remote SHA: $($branchRemote.SHA)" -ForegroundColor Yellow
$_
}
}
else {
Write-Host "[MISSING]" -ForegroundColor Red
write-host "Default branch not found" -ForegroundColor Yellow
$_
}
}
}
Clear-Host
$configuration = Get-Content -Raw -Path .\config.json | ConvertFrom-Json
$rootDirectoryPath = Get-CurrentLocation
$rootDirectoryPath = Join-Path $rootDirectoryPath "repo"
if (!(Test-Path $rootDirectoryPath)) {
mkdir $rootDirectoryPath
}
$configuration.projects | % {
Set-Location $rootDirectoryPath
Write-Host ""
Write-Host "Processing: '$($_.name)'" -ForegroundColor Red
$sha = $_.sha
$branch = Get-DefaultBranch $_.url
# deprecated - SHA validation will be done against the master branch and remotes
if (![string]::IsNullOrWhiteSpace($sha)) {
if ($sha -eq $branch.SHA) {
Write-Host "Validating SHA [OK]"
return
}
else {
Write-Host "Validating SHA [MISSMATCH]"
Write-Host "Master SHA: $($branch.SHA)" -ForegroundColor Yellow
Write-Host "Config SHA: $sha" -ForegroundColor Yellow
}
}
# end deprecated
Write-Host "Getting outdated remotes" -ForegroundColor Yellow
$remotes = Get-OutdatedRemotes -Project $_ -MasterBranch $branch
if (!$remotes) {
Write-Host "No remotes to update" -ForegroundColor Green
return
}
$repoPath = Join-Path $rootDirectoryPath $_.name
if (Test-Path $repoPath) {
Write-Host "Repository folder exists" -ForegroundColor Green
Set-Location $rootDirectoryPath
Set-Location $repoPath
Write-Host "Current Location $(Get-CurrentLocation)" -ForegroundColor Gray
Write-Host "Reseting status. . ." -ForegroundColor Yellow
git reset --hard | Out-Null
git clean -f -d | Out-Null
Write-Host "Fetching . . ." -ForegroundColor Yellow
git fetch --tags
git checkout $branch.Name
Write-Host "Pulling . . ." -ForegroundColor Yellow
git pull origin $branch.Name
}
else {
Write-Host "Repository does not exists" -ForegroundColor Red
Write-Host "Clonning . . ." -ForegroundColor Yellow
git clone $_.url $_.name
}
Write-Host "Master SHA: $($branch.SHA) [$($branch.Name)]" -ForegroundColor Yellow
Set-Location $rootDirectoryPath
Set-Location $repoPath
Write-Host "Current Location $(Get-CurrentLocation)" -ForegroundColor Gray
Write-Host "Validating remotes" -ForegroundColor Cyan
$remotes | % {
$remoteName = $_.name
$remoteUrl = $_.url
Write-Host "`t$remoteName" -ForegroundColor Cyan -NoNewline
$remotes = git remote
$remote = $remotes | ? { $_ -eq $remoteName } | Select-Object -First 1
if ($remote) {
Write-Host "[OK]" -ForegroundColor Green
}
else {
Write-Host "[MISSING]" -ForegroundColor Red
Write-Host "Adding remote" -ForegroundColor Yellow
git remote add $remoteName $remoteUrl
}
Write-Host "Pushing to backup . . ." -ForegroundColor Green
git push -u $remoteName --all --force
}
}
Set-Location $rootDirectoryPath
Set-Location ..