Update script powershell controllo repo locale vs remoto

This commit is contained in:
Samuele Locatelli
2025-07-08 16:40:34 +02:00
parent 190f816973
commit fc3ec2dc62
+13 -12
View File
@@ -10,7 +10,6 @@ param (
[string]$RootPath = "C:\Users\samuele.steamw\source", # Default alla cartella dei sorgenti, da passare invocando il comando [string]$RootPath = "C:\Users\samuele.steamw\source", # Default alla cartella dei sorgenti, da passare invocando il comando
[switch]$SwitchToMain, [switch]$SwitchToMain,
[string]$OutputFile = "$PSScriptRoot\outdated_repos.txt", [string]$OutputFile = "$PSScriptRoot\outdated_repos.txt",
[switch]$FormatAsTable,
[switch]$Help [switch]$Help
) )
@@ -44,8 +43,7 @@ Examples:
function Check-GitStatus { function Check-GitStatus {
param ( param (
[string]$repoPath, [string]$repoPath,
[string]$OutputFile, [string]$OutputFile
[switch]$FormatAsTable
) )
Write-Host "`nChecking repository at: $repoPath" -ForegroundColor Cyan Write-Host "`nChecking repository at: $repoPath" -ForegroundColor Cyan
@@ -53,24 +51,25 @@ function Check-GitStatus {
if (-not (Test-Path ".git")) { if (-not (Test-Path ".git")) {
Write-Host "Not a Git repository." -ForegroundColor Yellow 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 Pop-Location
return return
} }
git fetch origin *> $null 2>&1 git fetch origin *> $null 2>&1
$originalBranch = git rev-parse --abbrev-ref HEAD 2>$null $originalBranch = git rev-parse --abbrev-ref HEAD 2>$null
$remoteBranches = git ls-remote --heads origin | ForEach-Object { $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 $targetBranch = $branchesToCheck | Where-Object { $remoteBranches -contains $_ } | Select-Object -First 1
if (-not $targetBranch) { 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 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 Pop-Location
return return
} }
@@ -80,6 +79,7 @@ function Check-GitStatus {
git checkout $targetBranch *> $null 2>&1 git checkout $targetBranch *> $null 2>&1
} }
$status = git status -sb 2>$null
$aheadBehind = git rev-list --left-right --count origin/$targetBranch...HEAD 2>$null $aheadBehind = git rev-list --left-right --count origin/$targetBranch...HEAD 2>$null
if ($aheadBehind) { if ($aheadBehind) {
@@ -88,9 +88,9 @@ function Check-GitStatus {
$ahead = [int]$parts[1] $ahead = [int]$parts[1]
if ($ahead -gt 0 -or $behind -gt 0) { 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 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 $script:hasOutdated = $true
} else { } else {
Write-Host "✅ Repo is up to date with origin/$targetBranch" -ForegroundColor Green Write-Host "✅ Repo is up to date with origin/$targetBranch" -ForegroundColor Green
@@ -116,7 +116,7 @@ $script:hasOutdated = $false
# Check each repository # Check each repository
foreach ($repo in $repos) { foreach ($repo in $repos) {
Check-GitStatus -repoPath $repo.FullName -OutputFile $OutputFile -FormatAsTable:$FormatAsTable Check-GitStatus -repoPath $repo.FullName -OutputFile $OutputFile
} }
# Final summary # Final summary
@@ -125,5 +125,6 @@ if ($script:hasOutdated) {
} else { } else {
$msg = "🎉 All repositories are up to date!" $msg = "🎉 All repositories are up to date!"
Write-Host "`n$msg" -ForegroundColor Green 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
}