Skip to content

Commit 286311a

Browse files
committed
fixed windows script for scanning all drives
1 parent 619a070 commit 286311a

3 files changed

Lines changed: 35 additions & 11 deletions

File tree

IT-DEPLOYMENT-GUIDE.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser # if need
3636
*No admin required. Installs gitleaks to `%LOCALAPPDATA%\gitleaks\bin` and adds it to your user PATH.*
3737

3838
#### 2. Update All Repositories
39+
Run with **no arguments** to scan all local drives (C:\, D:\, E:\, etc.) and install hooks in every repo. One command for the whole machine.
3940
```powershell
40-
.\update-all-repos.ps1 # current directory only
41-
.\update-all-repos.ps1 C:\Projects # all repos under C:\Projects
42-
$env:MAX_DEPTH = 3; .\update-all-repos.ps1 C:\Projects # limit depth
41+
.\update-all-repos.ps1
42+
```
43+
Optional: limit to one drive or folder (faster):
44+
```powershell
45+
.\update-all-repos.ps1 C:\Projects
46+
.\update-all-repos.ps1 C:\ D:\
47+
$env:MAX_DEPTH = 4; .\update-all-repos.ps1 C:\ # limit depth on C:
4348
```
4449

4550
#### 3. Uninstall

install-gitleaks-global.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ Write-Host " * Hooks: pre-commit, commit-msg"
235235

236236
Write-Host "`nNext steps:" -ForegroundColor Cyan
237237
Write-Host " 1. All NEW git repositories will automatically get gitleaks hooks."
238-
Write-Host " 2. For EXISTING repos, run: .\update-all-repos.ps1 [directory]"
238+
Write-Host " 2. For EXISTING repos, run (updates all repos on all local drives):"
239+
Write-Host " .\update-all-repos.ps1" -ForegroundColor Gray
239240
Write-Host " 3. Or in each repo: git init (re-run to apply template)"
240241
Write-Host "`nNote: If gitleaks is not found in a new terminal, close and reopen PowerShell, or run:"
241242
Write-Host " `$env:Path += `";$GITLEAKS_BIN_DIR`""

update-all-repos.ps1

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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

99
param(
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.)
3839
if ($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

4654
function 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

5468
function 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

95108
Write-Host ""
96109
Write-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+
}
97115
if ($script:Failed -gt 0) { exit 1 }

0 commit comments

Comments
 (0)