From db43d8b2cd37135c2a053157db86cc5b9343ee23 Mon Sep 17 00:00:00 2001 From: "marco.locatelli@steamware.net" Date: Tue, 17 Jun 2025 17:08:27 +0200 Subject: [PATCH 1/6] test --- ApiGit/Mirrors/GitlabForcePushMirror.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ApiGit/Mirrors/GitlabForcePushMirror.ps1 b/ApiGit/Mirrors/GitlabForcePushMirror.ps1 index 5bc7b04..d15eaba 100644 --- a/ApiGit/Mirrors/GitlabForcePushMirror.ps1 +++ b/ApiGit/Mirrors/GitlabForcePushMirror.ps1 @@ -14,7 +14,7 @@ $mainStopWatch = [system.diagnostics.stopwatch]::StartNew() #contatore ciclo do while $projectCount = 1 #numero massimo di progetti -$projectNumber = 250 +$projectNumber = 300 #conteggio progetti trovati $existingProjects = 0 #contatore mirror trovati From 200fb08bf964994b3f3022ba63a0bf69c4cf7bb8 Mon Sep 17 00:00:00 2001 From: "marco.locatelli@steamware.net" Date: Thu, 19 Jun 2025 10:24:43 +0200 Subject: [PATCH 2/6] improved script per rimuovere protezione branch su nembro/azzano --- ApiGit/Mirrors/CheckGitlabCommits.ps1 | 2 +- ApiGit/Mirrors/CheckGitlabMirroring.ps1 | 6 +- ApiGit/Mirrors/GitlabCreateMissingMirrors.ps1 | 2 +- ApiGit/Misc/CheckGitlabProtectedBranches.ps1 | 99 ++++++++++--------- .../ReposCreationAzzanoNembro.ps1 | 6 +- 5 files changed, 61 insertions(+), 54 deletions(-) diff --git a/ApiGit/Mirrors/CheckGitlabCommits.ps1 b/ApiGit/Mirrors/CheckGitlabCommits.ps1 index 0c2f401..3cb34e8 100644 --- a/ApiGit/Mirrors/CheckGitlabCommits.ps1 +++ b/ApiGit/Mirrors/CheckGitlabCommits.ps1 @@ -15,7 +15,7 @@ $mainStopWatch = [system.diagnostics.stopwatch]::StartNew() #contatore ciclo do while $projectCount = 1 #numero massimo di progetti da analizzare -$projectNumber = 250 +$projectNumber = 300 #conteggio progetti trovati $projectsFound = 0 #conteggio progetti con commit negli ultimi giorni diff --git a/ApiGit/Mirrors/CheckGitlabMirroring.ps1 b/ApiGit/Mirrors/CheckGitlabMirroring.ps1 index 24ab53a..7882040 100644 --- a/ApiGit/Mirrors/CheckGitlabMirroring.ps1 +++ b/ApiGit/Mirrors/CheckGitlabMirroring.ps1 @@ -15,7 +15,7 @@ $mainStopWatch = [system.diagnostics.stopwatch]::StartNew() #contatore ciclo do while $projectCount = 1 #numero massimo di progetti da analizzare -$projectNumber = 300 +$projectNumber = 250 #conteggio progetti trovati $existingProjects = 0 #conteggio mirroring in errore @@ -58,12 +58,12 @@ do { $parsedResponse = $Response.Content | ConvertFrom-Json #scrivo il numero del progetto nel terminale e su file foreach ($item in $parsedResponse) { - #verifico se i mirroring trovati per il progetto corrente hanno status "finished" + #verifico se i mirroring trovati per il progetto corrente hanno status "finished" e scrivo log if ($item.update_status.Equals("finished")) { WriteLogOutput $logFile 2 "Mirror: $($item.url) - Status: $($item.update_status) - Last Success: $($item.last_successful_update_at)" $mirrorSuccessCount = $mirrorSuccessCount + 1 } - #i mirroring che non hanno status "finished" vengono loggati con relativo errore + #per i mirroring che non hanno status "finished" scrivo log con relativo errore else { WriteLogOutput $logFile 1 "Mirror: $($item.url) - Status: $($item.update_status) - Last Success: $($item.last_successful_update_at) - Last Attempt: $($item.last_update_started_at)" WriteLogOutput $logFile 1 "Error: $($item.last_error)" diff --git a/ApiGit/Mirrors/GitlabCreateMissingMirrors.ps1 b/ApiGit/Mirrors/GitlabCreateMissingMirrors.ps1 index 5a302e8..601f995 100644 --- a/ApiGit/Mirrors/GitlabCreateMissingMirrors.ps1 +++ b/ApiGit/Mirrors/GitlabCreateMissingMirrors.ps1 @@ -17,7 +17,7 @@ $projectCount = 200 #numero massimo di progetti da analizzare $projectNumber = 250 #nome file di log -$logFile = "GiteaNewMirrors.log" +$logFile = "NewMirrorsCreation.log" #creazione folder di Log se non già esistente if (Test-Path $logFolder) { diff --git a/ApiGit/Misc/CheckGitlabProtectedBranches.ps1 b/ApiGit/Misc/CheckGitlabProtectedBranches.ps1 index 1ababd4..1fb19c5 100644 --- a/ApiGit/Misc/CheckGitlabProtectedBranches.ps1 +++ b/ApiGit/Misc/CheckGitlabProtectedBranches.ps1 @@ -13,7 +13,7 @@ $startTime = (Get-Date).toString("yyyy/MM/dd HH:mm:ss") # avvio stopwatch $mainStopWatch = [system.diagnostics.stopwatch]::StartNew() #contatore ciclo do while -$projectCount = 200 +$projectCount = 1 #numero massimo di progetti da analizzare $projectNumber = 300 #conteggio progetti trovati @@ -25,19 +25,6 @@ $deleteProtection = 1 #nome file di log $logFile = "GitlabProtectedBranches.log" -#specifica quale installazione di gitlab va controllata -$remote = "Nembro" -#$remote = "Azzano" - -if ($remote -eq "Nembro"){ -$gitlabIstance = $destinationNembro -$head = $nembroHead -} -if ($remote -eq "Azzano"){ -$gitlabIstance = $destinationAzzano -$head = $azzanoHead -} - #creazione folder di Log se non già esistente if (Test-Path $logFolder) { } @@ -60,42 +47,62 @@ WriteLogOutput $logFile 0 "Inizio Esecuzione Script: $startTime" WriteLogOutput $logFile 0 "" WriteLogOutput $logFile 0 "Gitlab di Destinazione: $gitlabIstance" -#ciclo principale do/while che cicla da 1 a N projectNumber -do { - #chiamata api che restituisce informazioni sullo stato dei mirror - $callUrl = "https://" + $gitlabIstance + "/api/v4/projects/" + $projectCount + "/protected_branches" - try { - #parsing della risposta api convertita da json - $Response = Invoke-WebRequest -URI $callUrl -Headers $head -ContentType "application/json" -UseBasicParsing - $parsedResponse = $Response.Content | ConvertFrom-Json - #scrivo il numero del progetto - WriteLogOutput $logFile 1 "" - WriteLogOutput $logFile 1 "--------------------" - WriteLogOutput $logFile 1 "**PROGETTO $projectCount**" - foreach ($item in $parsedResponse) { - WriteLogOutput $logFile 1 "Protected Branch Name: $($item.name) - Allow Force Push: $($item.allow_force_push)" - $protectedProjects = $protectedProjects + 1 - if ($deleteProtection -eq 1) { - #nuovo URL x delete - $callUrlDelete = "https://" + $gitlabIstance + "/api/v4/projects/" + $projectCount + "/protected_branches/" + $item.name - #chiamo method DELETE - Invoke-WebRequest -Method Delete -URI $callUrlDelete -Headers $head -ContentType "application/json" -UseBasicParsing +#specifica quale installazione di gitlab va controllata +#$remote = "Nembro" +#$remote = "Azzano" + +$locations = @('Nembro','Azzano') + +#ciclo for per lavorare su gitlab nembro e poi su gitlab azzano +for ( $i = 0; $i -le 1; $i++) +{ + $projectCount = 1 + $remote = $locations[$i] + if ($remote -eq "Nembro"){ + $gitlabIstance = $destinationNembro + $head = $nembroHead + } + if ($remote -eq "Azzano"){ + $gitlabIstance = $destinationAzzano + $head = $azzanoHead + } + #ciclo principale do/while che cicla da 1 a N projectNumber + do { + #chiamata api che restituisce informazioni sullo stato dei mirror + $callUrl = "https://" + $gitlabIstance + "/api/v4/projects/" + $projectCount + "/protected_branches" + try { + #parsing della risposta api convertita da json + $Response = Invoke-WebRequest -URI $callUrl -Headers $head -ContentType "application/json" -UseBasicParsing + $parsedResponse = $Response.Content | ConvertFrom-Json + #scrivo il numero del progetto + WriteLogOutput $logFile 1 "" + WriteLogOutput $logFile 1 "--------------------" + WriteLogOutput $logFile 1 "**PROGETTO $projectCount**" + foreach ($item in $parsedResponse) { + WriteLogOutput $logFile 1 "Protected Branch Name: $($item.name) - Allow Force Push: $($item.allow_force_push)" + $protectedProjects = $protectedProjects + 1 + if ($deleteProtection -eq 1) { + #nuovo URL x delete + $callUrlDelete = "https://" + $gitlabIstance + "/api/v4/projects/" + $projectCount + "/protected_branches/" + $item.name + #chiamo method DELETE + Invoke-WebRequest -Method Delete -URI $callUrlDelete -Headers $head -ContentType "application/json" -UseBasicParsing + } } + $existingProjects = $existingProjects + 1 } - $existingProjects = $existingProjects + 1 + #scrivo se trovo un errore durante il try/catch + catch { + $parsedError = $_ | ConvertFrom-Json + WriteLogOutput $logFile 3 "" + WriteLogOutput $logFile 3 "--------------------" + WriteLogOutput $logFile 3 "**ERRORE NEL PROGETTO $projectCount**" + WriteLogOutput $logFile 3 $($parsedError.message) + } + $projectCount = $projectCount + 1 } - #scrivo se trovo un errore durante il try/catch - catch { - $parsedError = $_ | ConvertFrom-Json - WriteLogOutput $logFile 3 "" - WriteLogOutput $logFile 3 "--------------------" - WriteLogOutput $logFile 3 "**ERRORE NEL PROGETTO $projectCount**" - WriteLogOutput $logFile 3 $($parsedError.message) - } - $projectCount = $projectCount + 1 + #fine ciclo principale + while ($projectCount -le $projectNumber) } -#fine ciclo principale -while ($projectCount -le $projectNumber) # fermo stopwatch e calcolo durata script $mainStopWatch.Stop() diff --git a/ApiGit/ReposCreation/ReposCreationAzzanoNembro.ps1 b/ApiGit/ReposCreation/ReposCreationAzzanoNembro.ps1 index 591e95f..955c725 100644 --- a/ApiGit/ReposCreation/ReposCreationAzzanoNembro.ps1 +++ b/ApiGit/ReposCreation/ReposCreationAzzanoNembro.ps1 @@ -15,13 +15,13 @@ $mainStopWatch = [system.diagnostics.stopwatch]::StartNew() #contatore ciclo do while $projectCount = 200 #numero massimo di progetti da analizzare (messo a 1 per "sicura inserita" siccome questo script è potenzialmente pericoloso) -$projectNumber = 300 +$projectNumber = 250 #nome file di log $logFile = "AzzanoNembroReposCreation.log" #specifica quale installazione di gitlab va controllata -$remote = "Nembro" -#$remote = "Azzano" +#$remote = "Nembro" +$remote = "Azzano" if ($remote -eq "Nembro"){ $gitlabIstance = $destinationNembro From 99a83665dc5a30ad5e41a5f31731232078dc7e6a Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 8 Jul 2025 15:51:09 +0200 Subject: [PATCH 3/6] Aggiunta script test localGitRepo --- CheckLocalGitStatus.ps1 | 123 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 CheckLocalGitStatus.ps1 diff --git a/CheckLocalGitStatus.ps1 b/CheckLocalGitStatus.ps1 new file mode 100644 index 0000000..aaafe0d --- /dev/null +++ b/CheckLocalGitStatus.ps1 @@ -0,0 +1,123 @@ +# CheckLocalGitStatus +# +# Verifica di un set di repo GIT locali rispetto al server origin remoto +# per stabilire quali necessitino di aggiornamento +# +# esempio chiamata: + +# parametri in ingresso +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]$Help +) + + +# Mostra help message se --help or -h passato come argomento +if ($Help -or $args -contains "--help" -or $args -contains "-h") { + Write-Host @" +Usage: .\Check-GitRepos.ps1 [-RootPath ] [-SwitchToMain] [-OutputFile ] [--help] + +Checks all Git repositories under the specified folder and compares their main/master branch +with the remote origin. Optionally switches to main/master to perform the check. + +Parameters: + -RootPath Path to the root folder containing Git repositories (default: current script path) + -SwitchToMain Temporarily switch to main/master branch to perform the check + -OutputFile Path to the output file listing outdated repositories + --help, -h Show this help message + +Examples: + .\Check-GitRepos.ps1 -RootPath "D:\Dev\Projects" + .\Check-GitRepos.ps1 -SwitchToMain + .\Check-GitRepos.ps1 -RootPath "C:\Code" -SwitchToMain -OutputFile "C:\outdated.txt" +"@ -ForegroundColor Cyan + exit +} + +$branchesToCheck = @("main", "master") +$outdatedRepos = @() + +# Function to check Git status +function Check-GitStatus { + param ( + [string]$repoPath + ) + + Write-Host "`nChecking repository at: $repoPath" -ForegroundColor Cyan + Push-Location $repoPath + + if (-not (Test-Path ".git")) { + Write-Host "Not a Git repository." -ForegroundColor Yellow + Pop-Location + return + } + + git fetch origin *> $null 2>&1 + + $originalBranch = git rev-parse --abbrev-ref HEAD 2>$null + + # Check which of 'main' or 'master' exists on the remote + $remoteBranches = git ls-remote --heads origin | ForEach-Object { + ($_ -split "\s+")[1] -replace "refs/heads/", "" + } + + $targetBranch = $branchesToCheck | Where-Object { $remoteBranches -contains $_ } | Select-Object -First 1 + + if (-not $targetBranch) { + Write-Host "No 'main' or 'master' branch found on remote." -ForegroundColor Red + Pop-Location + return + } + + if ($SwitchToMain -and $originalBranch -ne $targetBranch) { + git stash push -u -m "Temp stash before switching branches" *> $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 + + if ($aheadBehind) { + $parts = $aheadBehind -split "`t" + $behind = [int]$parts[0] + $ahead = [int]$parts[1] + + if ($ahead -gt 0 -or $behind -gt 0) { + Write-Host "🔺 Repo is not up to date with origin/$targetBranch (ahead: $ahead, behind: $behind)" -ForegroundColor Yellow + $outdatedRepos += $repoPath + } else { + Write-Host "✅ Repo is up to date with origin/$targetBranch" -ForegroundColor Green + } + } + + if ($SwitchToMain -and $originalBranch -ne $targetBranch) { + git checkout $originalBranch *> $null 2>&1 + git stash pop *> $null 2>&1 + } + + Pop-Location +} + +# Get all subdirectories containing .git folders +$repos = Get-ChildItem -Path $RootPath -Recurse -Directory | Where-Object { + Test-Path "$($_.FullName)\.git" +} + +# Check each repository +foreach ($repo in $repos) { + Check-GitStatus -repoPath $repo.FullName +} + +# Output outdated repos to file +if ($outdatedRepos.Count -gt 0) { + "`n📄 Outdated repositories:" | Out-File -FilePath $OutputFile -Encoding UTF8 + foreach ($repo in $outdatedRepos) { + $repo | Tee-Object -FilePath $OutputFile -Append + } + Write-Host "`n📄 Outdated repositories saved to: $OutputFile" -ForegroundColor Cyan +} else { + "All repositories are up to date!" | Out-File -FilePath $OutputFile -Encoding UTF8 + Write-Host "`n🎉 All repositories are up to date!" -ForegroundColor Green +} From bf3500e3d416228b21b4d8c53d148c74fc63928e Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 8 Jul 2025 15:52:27 +0200 Subject: [PATCH 4/6] Spostamento script in folder GitLocalUtils --- CheckLocalGitStatus.ps1 => GitLocalUtils/CheckLocalGitStatus.ps1 | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename CheckLocalGitStatus.ps1 => GitLocalUtils/CheckLocalGitStatus.ps1 (100%) diff --git a/CheckLocalGitStatus.ps1 b/GitLocalUtils/CheckLocalGitStatus.ps1 similarity index 100% rename from CheckLocalGitStatus.ps1 rename to GitLocalUtils/CheckLocalGitStatus.ps1 From 190f816973a34d6df97b55d9b2e9207b90940758 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 8 Jul 2025 16:26:05 +0200 Subject: [PATCH 5/6] test versione con output tabellare e log timestamp (ma non corretta) --- GitLocalUtils/CheckLocalGitStatus.ps1 | 46 +++++++++++++++------------ 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/GitLocalUtils/CheckLocalGitStatus.ps1 b/GitLocalUtils/CheckLocalGitStatus.ps1 index aaafe0d..8455144 100644 --- a/GitLocalUtils/CheckLocalGitStatus.ps1 +++ b/GitLocalUtils/CheckLocalGitStatus.ps1 @@ -10,9 +10,12 @@ 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 ) +$branchesToCheck = @("main", "master") +$outdatedRepos = @() # Mostra help message se --help or -h passato come argomento if ($Help -or $args -contains "--help" -or $args -contains "-h") { @@ -36,13 +39,13 @@ Examples: exit } -$branchesToCheck = @("main", "master") -$outdatedRepos = @() # Function to check Git status function Check-GitStatus { param ( - [string]$repoPath + [string]$repoPath, + [string]$OutputFile, + [switch]$FormatAsTable ) Write-Host "`nChecking repository at: $repoPath" -ForegroundColor Cyan @@ -50,23 +53,24 @@ 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 Pop-Location return } git fetch origin *> $null 2>&1 - $originalBranch = git rev-parse --abbrev-ref HEAD 2>$null - # Check which of 'main' or 'master' exists on the remote $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) { - Write-Host "No 'main' or 'master' branch found on remote." -ForegroundColor Red + $message = "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 Pop-Location return } @@ -76,7 +80,6 @@ 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) { @@ -85,8 +88,10 @@ function Check-GitStatus { $ahead = [int]$parts[1] if ($ahead -gt 0 -or $behind -gt 0) { - Write-Host "🔺 Repo is not up to date with origin/$targetBranch (ahead: $ahead, behind: $behind)" -ForegroundColor Yellow - $outdatedRepos += $repoPath + $message = "🔺 $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 + $script:hasOutdated = $true } else { Write-Host "✅ Repo is up to date with origin/$targetBranch" -ForegroundColor Green } @@ -105,19 +110,20 @@ $repos = Get-ChildItem -Path $RootPath -Recurse -Directory | Where-Object { Test-Path "$($_.FullName)\.git" } +# Clear or create output file +"" | Out-File -FilePath $OutputFile -Encoding UTF8 +$script:hasOutdated = $false + # Check each repository foreach ($repo in $repos) { - Check-GitStatus -repoPath $repo.FullName + Check-GitStatus -repoPath $repo.FullName -OutputFile $OutputFile -FormatAsTable:$FormatAsTable } -# Output outdated repos to file -if ($outdatedRepos.Count -gt 0) { - "`n📄 Outdated repositories:" | Out-File -FilePath $OutputFile -Encoding UTF8 - foreach ($repo in $outdatedRepos) { - $repo | Tee-Object -FilePath $OutputFile -Append - } +# Final summary +if ($script:hasOutdated) { Write-Host "`n📄 Outdated repositories saved to: $OutputFile" -ForegroundColor Cyan } else { - "All repositories are up to date!" | Out-File -FilePath $OutputFile -Encoding UTF8 - Write-Host "`n🎉 All repositories are up to date!" -ForegroundColor Green -} + $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 +} \ No newline at end of file From fc3ec2dc62ebf805a9ef85e11f2f202b27854bf7 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 8 Jul 2025 16:40:34 +0200 Subject: [PATCH 6/6] Update script powershell controllo repo locale vs remoto --- GitLocalUtils/CheckLocalGitStatus.ps1 | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/GitLocalUtils/CheckLocalGitStatus.ps1 b/GitLocalUtils/CheckLocalGitStatus.ps1 index 8455144..820725b 100644 --- a/GitLocalUtils/CheckLocalGitStatus.ps1 +++ b/GitLocalUtils/CheckLocalGitStatus.ps1 @@ -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 -} \ No newline at end of file + $msg = "$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) - 🎉 All repositories are up to date!" + $msg | Out-File -FilePath $OutputFile -Append -Encoding UTF8 +}