From 99a83665dc5a30ad5e41a5f31731232078dc7e6a Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 8 Jul 2025 15:51:09 +0200 Subject: [PATCH 1/4] 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 2/4] 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 3/4] 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 4/4] 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 +}