11# Requires -Version 5.1
22# Installs gitleaks pre-commit hooks in existing repositories (Windows).
33# Usage:
4- # .\update-all-repos.ps1 # Current directory only
5- # .\update-all-repos.ps1 C:\Projects # All repos under C:\Projects
4+ # .\update-all-repos.ps1 # All local drives (C:\, D:\, E:\, etc.) - single command for all repos
5+ # .\update-all-repos.ps1 C:\Projects # Only repos under C:\Projects
66# .\update-all-repos.ps1 C:\Projects C:\Sites
7- # Optional: $MAX_DEPTH = 3; .\update-all-repos.ps1 C:\Projects
7+ # Optional: $MAX_DEPTH = 3; .\update-all-repos.ps1 C:\ # Limit depth on a drive
88
99param (
1010 [Parameter (ValueFromRemainingArguments = $true )]
@@ -35,8 +35,16 @@ if (-not (Test-Path (Join-Path $TEMPLATE_HOOKS "pre-commit"))) {
3535$preCommitSrc = Join-Path $TEMPLATE_HOOKS " pre-commit"
3636$commitMsgSrc = Join-Path $TEMPLATE_HOOKS " commit-msg"
3737
38+ # No path given = scan all local fixed drives (C:\, D:\, E:\, etc.)
3839if ($TargetPaths.Count -eq 0 ) {
39- $TargetPaths = @ (Get-Location ).Path
40+ $TargetPaths = [System.IO.DriveInfo ]::GetDrives() | Where-Object { $_.DriveType -eq ' Fixed' -and $_.IsReady } | ForEach-Object { $_.Root.TrimEnd (' \' ) + ' \' }
41+ if ($TargetPaths.Count -eq 0 ) {
42+ Write-Fail " No local drives found."
43+ exit 1
44+ }
45+ Write-Host " No path specified: scanning all local drives ( $ ( $TargetPaths -join ' , ' ) )" - ForegroundColor Cyan
46+ Write-Host " This may take a while on large drives. Press Ctrl+C to cancel." - ForegroundColor Gray
47+ Write-Host " "
4048}
4149
4250$script :Updated = 0
@@ -45,10 +53,16 @@ $script:Failed = 0
4553
4654function Get-GitRepos {
4755 param ([string ]$Root , [int ]$MaxDepth = 0 )
56+ $repos = @ ()
57+ # Include root if it is itself a git repo (Get-ChildItem -Recurse only returns subdirs, not root)
58+ if (Test-Path (Join-Path $Root " .git" )) {
59+ $repos += $Root
60+ }
4861 $params = @ { Path = $Root ; Directory = $true ; Recurse = $true ; ErrorAction = ' SilentlyContinue' }
4962 if ($MaxDepth -gt 0 ) { $params [' Depth' ] = $MaxDepth }
50- $dirs = Get-ChildItem @params | Where-Object { Test-Path (Join-Path $_.FullName " .git" ) }
51- $dirs | ForEach-Object { $_.FullName }
63+ $subdirs = Get-ChildItem @params | Where-Object { Test-Path (Join-Path $_.FullName " .git" ) }
64+ $repos += ($subdirs | ForEach-Object { $_.FullName })
65+ $repos | Sort-Object - Unique
5266}
5367
5468function Install-Hooks {
@@ -77,7 +91,6 @@ foreach ($target in $TargetPaths) {
7791 $maxDepth = if ($env: MAX_DEPTH ) { [int ]$env: MAX_DEPTH } else { 0 }
7892 Write-Step " Scanning $root for git repositories..."
7993 $repos = Get-GitRepos - Root $root - MaxDepth $maxDepth
80- $repos = $repos | Sort-Object - Unique
8194 $i = 0
8295 foreach ($repo in $repos ) {
8396 $i ++
@@ -94,4 +107,9 @@ foreach ($target in $TargetPaths) {
94107
95108Write-Host " "
96109Write-Host " Done. Updated: $ ( $script :Updated ) , Failed: $ ( $script :Failed ) " - ForegroundColor Cyan
110+ if ($script :Updated -eq 0 -and $script :Failed -eq 0 ) {
111+ Write-Host " "
112+ Write-Warn " No git repositories were found in the scanned path(s)."
113+ Write-Host " To scan only a specific folder: .\update-all-repos.ps1 C:\Projects" - ForegroundColor Gray
114+ }
97115if ($script :Failed -gt 0 ) { exit 1 }
0 commit comments