-
Notifications
You must be signed in to change notification settings - Fork 0
/
differ.ps1
204 lines (189 loc) Β· 5.63 KB
/
differ.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
param(
[string]$RepoURL = "https://github.com/jvcss/jvcss.git",
[string]$DiretorioDestino = "source"
)
$ignore = @(
".fvm/flutter_sdk",
".fvm/fvm_config.json",
".gitignore"
)
#π₯
function Get-ContentDiffReverse {
param (
[string]$File,
[string]$Branch,
[string]$BaseA = "main"
)
#displays the changes that have been made in the $BaseA that are not present in the $Branch
$difference = git diff $Branch..$BaseA -- $File
return $difference
}
#π₯
function Get-Branches {
param(
[string]$BaseA = "main"
)
$branchesRemote = git branch -r | ForEach-Object {
$_ -replace 'origin/', ''
} | Where-Object { $_ -notmatch 'HEAD' } | Where-Object { $_ -notmatch $BaseA } | ForEach-Object { $_.Trim() }
return $branchesRemote
}
#π₯
function Get-FileDiffs {
param(
[string]$Branch
)
$diffs = git diff main..$Branch --name-only
$filteredDiffs = New-Object System.Collections.ArrayList
foreach ($item in $diffs) {
$matched = $false
foreach ($pattern in $ignore) {
if ($item -match $pattern) {
$matched = $true
break
}
}
if (-not $matched) {
#$filteredDiffs.Add($item)
$filteredDiffs += $item
}
}
return $filteredDiffs
}
#π₯
function Get-ContentDiff {
param (
[string]$File,
[string]$Branch,
[string]$BaseA = "main"
)
#displays the changes that have been made in the $Branch that are not present in the $BaseA
$difference = git diff "$($BaseA)..$($Branch)" -- $File
return $difference
}
#π₯
function Save-Diffs {
param(
[array]$Content,
[string]$File,
[string]$Branch,
[string]$BaseA = "main",
[string]$OutputFolder = "C:\work\expert\GitAutomations"
)
#should save in the diffs.json the new object with its new content property
$outputFile = "$($OutputFolder)\diffs.json"
#the new content to include
$arrayContent = New-Object System.Collections.ArrayList
for ($i = 0; $i -lt $Content.Count; $i++) {
$arrayContent += $Content[$i]
}
#get the name of the pair
$pair = "$($BaseA)_$($Branch)"
# Create a hash table for the branch if it doesn't exist
if (-not (Test-Path $outputFile)) {
$diffs = @{}
# Create a new object
$newObject = @{
$pair = @{
$File = $arrayContent
}
}
# Add the new object
$diffs = $newObject
# save in the file
$diffs | ConvertTo-Json -Depth 100 | Set-Content -Path $outputFile
}
# Read the existing JSON data from the file
else {
# Convert $diffs into a hash table
$diffsHash = Get-Content -Raw $outputFile | ConvertFrom-Json
$hashTable = @{}
$diffsHash.psobject.properties | ForEach-Object { $hashTable[$_.Name] = $_.Value }
# The Pair exist
if( $hashTable.$pair ){
# need transfor to hashtable too
$htPair = @{}
$hashTable.$pair.psobject.properties | ForEach-Object { $htPair[$_.Name] = $_.Value }
$addNewDiff = @{
$File = $arrayContent
}
$htPair += $addNewDiff
$hashTable.$pair = $htPair
}
# Create a Pair
else{
$addObject = @{
$pair = @{
$File = $arrayContent
}
}
# Add the new object to the hash table
$hashTable += $addObject
}
# convert to json and save the file
$hashTable | ConvertTo-Json -Depth 100 | Set-Content -Path $outputFile
}
Write-Host "Saved the encrypted diff for $File in branch $Branch." -ForegroundColor Green
}
#π₯
function Set-Checkout {
param (
[array]$Branches
)
foreach ($branch in $Branches) {
# Check if the branch exists locally
$branchExists = git branch --list $branch
if ($branchExists) {
Write-Host "Branch already in local repository" -ForegroundColor Green
}
else {
try {
git checkout $branch
Start-Sleep -Seconds 1
}
catch {
Set-ForceCheckout -Branch $branch
}
}
}
}
#π₯
function Set-ForceCheckout {
param(
[string]$Branch
)
git fetch origin
Start-Sleep -Seconds 1
git checkout -b $Branch origin/$Branch
Start-Sleep -Seconds 1
git branch --set-upstream-to=origin/$Branch $Branch
Write-Host "Forced Checkout to [${$Branch}]" -ForegroundColor Yellow
}
#π₯
function Get-Repository {
param (
[string]$URL,
[string]$OutputFolder = "source"
)
if (-not (Test-Path ".\${$OutputFolder}")) {
git clone $URL $OutputFolder
}
else {
Write-Host "Project folder Already Exist" -ForegroundColor Cyan
}
}
Get-Repository -URL $RepoURL -OutputFolder $DiretorioDestino
Push-Location $DiretorioDestino
# $contentDiffers = Get-ContentDiff -File "smart/android/app/src/main/kotlin/br/com/app/MainActivity.kt" -Branch "cead"
# Save-Diffs -Content $contentDiffers -File "smart/android/app/src/main/kotlin/br/com/app/MainActivity.kt" -Branch "cead"
$branches = Get-Branches
Set-Checkout -Branches $branches
foreach ($branch in $branches) {
$fileDiffers = Get-FileDiffs -branch $branch
foreach ($file in $fileDiffers) {
$contentDiffers = Get-ContentDiff -File $file -Branch $branch
# apply patches automatically
Save-Diffs -Content $contentDiffers -File $file -Branch $branch
}
}
Pop-Location