Update script powershell controllo repo locale vs remoto
This commit is contained in:
@@ -10,7 +10,6 @@ param (
|
||||
[string]$RootPath = "C:\Users\samuele.steamw\source", # Default alla cartella dei sorgenti, da passare invocando il comando
|
||||
[switch]$SwitchToMain,
|
||||
[string]$OutputFile = "$PSScriptRoot\outdated_repos.txt",
|
||||
[switch]$FormatAsTable,
|
||||
[switch]$Help
|
||||
)
|
||||
|
||||
@@ -44,8 +43,7 @@ Examples:
|
||||
function Check-GitStatus {
|
||||
param (
|
||||
[string]$repoPath,
|
||||
[string]$OutputFile,
|
||||
[switch]$FormatAsTable
|
||||
[string]$OutputFile
|
||||
)
|
||||
|
||||
Write-Host "`nChecking repository at: $repoPath" -ForegroundColor Cyan
|
||||
@@ -53,24 +51,25 @@ function Check-GitStatus {
|
||||
|
||||
if (-not (Test-Path ".git")) {
|
||||
Write-Host "Not a Git repository." -ForegroundColor Yellow
|
||||
"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Not a Git repository." | Out-File -FilePath $OutputFile -Append -Encoding UTF8
|
||||
"$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) - Not a Git repository." | Out-File -FilePath $OutputFile -Append -Encoding UTF8
|
||||
Pop-Location
|
||||
return
|
||||
}
|
||||
|
||||
git fetch origin *> $null 2>&1
|
||||
|
||||
$originalBranch = git rev-parse --abbrev-ref HEAD 2>$null
|
||||
|
||||
$remoteBranches = git ls-remote --heads origin | ForEach-Object {
|
||||
($_ -split "\\s+")[1] -replace "refs/heads/", ""
|
||||
($_ -split "\s+")[1] -replace "refs/heads/", ""
|
||||
}
|
||||
|
||||
$targetBranch = $branchesToCheck | Where-Object { $remoteBranches -contains $_ } | Select-Object -First 1
|
||||
|
||||
if (-not $targetBranch) {
|
||||
$message = "No 'main' or 'master' branch found on remote."
|
||||
$message = "$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) - MISSING | $repoPath No 'main' or 'master' branch found on remote."
|
||||
Write-Host $message -ForegroundColor Red
|
||||
"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $message" | Out-File -FilePath $OutputFile -Append -Encoding UTF8
|
||||
$message | Out-File -FilePath $OutputFile -Append -Encoding UTF8
|
||||
Pop-Location
|
||||
return
|
||||
}
|
||||
@@ -80,6 +79,7 @@ function Check-GitStatus {
|
||||
git checkout $targetBranch *> $null 2>&1
|
||||
}
|
||||
|
||||
$status = git status -sb 2>$null
|
||||
$aheadBehind = git rev-list --left-right --count origin/$targetBranch...HEAD 2>$null
|
||||
|
||||
if ($aheadBehind) {
|
||||
@@ -88,9 +88,9 @@ function Check-GitStatus {
|
||||
$ahead = [int]$parts[1]
|
||||
|
||||
if ($ahead -gt 0 -or $behind -gt 0) {
|
||||
$message = "🔺 $repoPath is not up to date with origin/$targetBranch (ahead: $ahead, behind: $behind)"
|
||||
$message = "$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) - UPDATE | $repoPath is not up to date with origin/$targetBranch (ahead: $ahead, behind: $behind)"
|
||||
Write-Host $message -ForegroundColor Yellow
|
||||
"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $message" | Out-File -FilePath $OutputFile -Append -Encoding UTF8
|
||||
$message | Out-File -FilePath $OutputFile -Append -Encoding UTF8
|
||||
$script:hasOutdated = $true
|
||||
} else {
|
||||
Write-Host "✅ Repo is up to date with origin/$targetBranch" -ForegroundColor Green
|
||||
@@ -116,7 +116,7 @@ $script:hasOutdated = $false
|
||||
|
||||
# Check each repository
|
||||
foreach ($repo in $repos) {
|
||||
Check-GitStatus -repoPath $repo.FullName -OutputFile $OutputFile -FormatAsTable:$FormatAsTable
|
||||
Check-GitStatus -repoPath $repo.FullName -OutputFile $OutputFile
|
||||
}
|
||||
|
||||
# Final summary
|
||||
@@ -125,5 +125,6 @@ if ($script:hasOutdated) {
|
||||
} else {
|
||||
$msg = "🎉 All repositories are up to date!"
|
||||
Write-Host "`n$msg" -ForegroundColor Green
|
||||
"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $msg" | Out-File -FilePath $OutputFile -Append -Encoding UTF8
|
||||
}
|
||||
$msg = "$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) - 🎉 All repositories are up to date!"
|
||||
$msg | Out-File -FilePath $OutputFile -Append -Encoding UTF8
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user