forked from gitleaks/gitleaks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-all-repos.ps1
More file actions
177 lines (157 loc) Β· 6.56 KB
/
update-all-repos.ps1
File metadata and controls
177 lines (157 loc) Β· 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#Requires -Version 5.1
# Installs gitleaks pre-commit hooks in existing repositories (Windows).
# Usage:
# .\update-all-repos.ps1 # All local drives (C:\, D:\, E:\, etc.) - single command for all repos
# .\update-all-repos.ps1 C:\Projects # Only repos under C:\Projects
# .\update-all-repos.ps1 C:\Projects C:\Sites
# Optional: $MAX_DEPTH = 3; .\update-all-repos.ps1 C:\ # Limit depth on a drive
param(
[Parameter(ValueFromRemainingArguments = $true)]
[string[]]$TargetPaths
)
$ErrorActionPreference = "Stop"
$TEMPLATE_DIR = Join-Path $env:USERPROFILE ".git-template"
$TEMPLATE_HOOKS = Join-Path $TEMPLATE_DIR "hooks"
function Write-Step { param($Message) Write-Host $Message -ForegroundColor Cyan }
function Write-Ok { param($Message) Write-Host " OK $Message" -ForegroundColor Green }
function Write-Warn { param($Message) Write-Host " !! $Message" -ForegroundColor Yellow }
function Write-Fail { param($Message) Write-Host " X $Message" -ForegroundColor Red }
if (-not (Get-Command gitleaks -ErrorAction SilentlyContinue)) {
Write-Fail "gitleaks is not installed or not in PATH."
Write-Host "Run .\install-gitleaks-global.ps1 first, or install from https://github.com/gitleaks/gitleaks/releases"
exit 1
}
if (-not (Test-Path (Join-Path $TEMPLATE_HOOKS "pre-commit"))) {
Write-Fail "Git template hooks not found at $TEMPLATE_HOOKS"
Write-Host "Run .\install-gitleaks-global.ps1 first."
exit 1
}
# Function to sync global gitleaks config from repository
function Sync-GlobalConfig {
$scriptDir = Split-Path -Parent $PSCommandPath
$sourceConfig = Join-Path $scriptDir ".gitleaks.toml"
$configDir = Join-Path $env:USERPROFILE ".config\gitleaks"
$targetConfig = Join-Path $configDir "gitleaks.toml"
# Check if source config exists
if (-not (Test-Path $sourceConfig)) {
Write-Warn "Source config not found: $sourceConfig"
Write-Host " Skipping config sync" -ForegroundColor Gray
return $false
}
# Create config directory if it doesn't exist
try {
New-Item -ItemType Directory -Path $configDir -Force -ErrorAction Stop | Out-Null
} catch {
Write-Fail "Failed to create config directory: $configDir"
return $false
}
# Copy the config file
try {
Copy-Item -Path $sourceConfig -Destination $targetConfig -Force -ErrorAction Stop
Write-Ok "Synced global config: $targetConfig"
return $true
} catch {
Write-Fail "Failed to sync config to: $targetConfig"
return $false
}
}
$preCommitSrc = Join-Path $TEMPLATE_HOOKS "pre-commit"
$commitMsgSrc = Join-Path $TEMPLATE_HOOKS "commit-msg"
# Sync global gitleaks config from repository
Write-Step "Syncing global gitleaks configuration..."
Sync-GlobalConfig | Out-Null
Write-Host ""
# No path given = scan all local fixed drives (C:\, D:\, E:\, etc.)
if ($TargetPaths.Count -eq 0) {
# Use .Name (e.g. "C:") to avoid null .Root on some Windows setups
$TargetPaths = [System.IO.DriveInfo]::GetDrives() | Where-Object { $_.DriveType -eq 'Fixed' -and $_.IsReady } | ForEach-Object { $_.Name + '\' }
if ($TargetPaths.Count -eq 0) {
Write-Fail "No local drives found."
exit 1
}
Write-Host "No path specified: scanning all local drives ( $($TargetPaths -join ', ') )" -ForegroundColor Cyan
Write-Host "This may take a while on large drives. Press Ctrl+C to cancel." -ForegroundColor Gray
Write-Host ""
}
$script:Updated = 0
$script:Skipped = 0
$script:Failed = 0
function Get-GitRepos {
param([string]$Root, [int]$MaxDepth = 0)
$script:ReposFoundList = [System.Collections.Generic.List[string]]::new()
function Search-Dirs {
param([string]$Path, [int]$Depth)
# Log only parent-level folders (direct children of drive or of the given root)
if ($Depth -eq 1) {
Write-Host " Scanning $Path" -ForegroundColor Gray
}
try {
if (Test-Path (Join-Path $Path ".git") -ErrorAction SilentlyContinue) {
$script:ReposFoundList.Add($Path) | Out-Null
}
} catch {
# Silently skip paths we don't have permission to read
}
if ($MaxDepth -gt 0 -and $Depth -ge $MaxDepth) { return }
Get-ChildItem -Path $Path -Directory -ErrorAction SilentlyContinue | ForEach-Object {
Search-Dirs -Path $_.FullName -Depth ($Depth + 1)
}
}
Search-Dirs -Path $Root -Depth 0
Write-Host " Scan complete: found $($script:ReposFoundList.Count) repos" -ForegroundColor Cyan
$script:ReposFoundList | Sort-Object -Unique
}
function Install-Hooks {
param([string]$RepoDir)
$hooksDir = Join-Path $RepoDir ".git\hooks"
# Create hooks directory if it doesn't exist
if (-not (Test-Path $hooksDir)) {
try {
New-Item -ItemType Directory -Path $hooksDir -Force -ErrorAction Stop | Out-Null
} catch {
Write-Fail "Cannot create hooks dir: $hooksDir - $($_.Exception.Message)"
return $false
}
}
try {
Copy-Item -Path $preCommitSrc -Destination (Join-Path $hooksDir "pre-commit") -Force -ErrorAction Stop
Copy-Item -Path $commitMsgSrc -Destination (Join-Path $hooksDir "commit-msg") -Force -ErrorAction Stop
return $true
} catch {
Write-Fail "Hook copy error: $($_.Exception.Message)"
return $false
}
}
foreach ($target in $TargetPaths) {
$root = $target
if (-not [System.IO.Path]::IsPathRooted($root)) {
$root = Join-Path (Get-Location).Path $root
}
if (-not (Test-Path $root -PathType Container)) {
Write-Fail "Directory does not exist: $root"
continue
}
$maxDepth = if ($env:MAX_DEPTH) { [int]$env:MAX_DEPTH } else { 0 }
Write-Step "Scanning $root for git repositories..."
$repos = Get-GitRepos -Root $root -MaxDepth $maxDepth
$i = 0
foreach ($repo in $repos) {
$i++
Write-Host " [$i] $repo" -ForegroundColor Gray
if (Install-Hooks -RepoDir $repo) {
$script:Updated++
Write-Ok "Hooks installed"
} else {
$script:Failed++
Write-Fail "Failed to install hooks"
}
}
}
Write-Host ""
Write-Host "Done. Updated: $($script:Updated), Failed: $($script:Failed)" -ForegroundColor Cyan
if ($script:Updated -eq 0 -and $script:Failed -eq 0) {
Write-Host ""
Write-Warn "No git repositories were found in the scanned path(s)."
Write-Host " To scan only a specific folder: .\update-all-repos.ps1 C:\Projects" -ForegroundColor Gray
}
if ($script:Failed -gt 0) { exit 1 }