Skip to content

Commit 498c4ab

Browse files
authored
Merge pull request #13 from SystangoTechnologies/fix/updatescript
Fix/updatescript
2 parents de552d4 + c573626 commit 498c4ab

2 files changed

Lines changed: 53 additions & 14 deletions

File tree

install-gitleaks-global.ps1

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,21 @@ Write-Host ""
8888

8989
# Step 2: Global config
9090
Write-Step "Step 2: Setting up global configuration..."
91-
New-Item -ItemType Directory -Path $CONFIG_DIR -Force | Out-Null
91+
try {
92+
New-Item -ItemType Directory -Path $CONFIG_DIR -Force -ErrorAction Stop | Out-Null
93+
} catch {
94+
Write-Fail "Failed to create config directory: $CONFIG_DIR - $($_.Exception.Message)"
95+
exit 1
96+
}
9297
$configSource = Join-Path $PSScriptRoot ".gitleaks.toml"
9398
if (Test-Path $configSource) {
94-
Copy-Item -Path $configSource -Destination (Join-Path $CONFIG_DIR "gitleaks.toml") -Force
95-
Write-Ok "Copied gitleaks config to $CONFIG_DIR\gitleaks.toml"
99+
try {
100+
Copy-Item -Path $configSource -Destination (Join-Path $CONFIG_DIR "gitleaks.toml") -Force -ErrorAction Stop
101+
Write-Ok "Copied gitleaks config to $CONFIG_DIR\gitleaks.toml"
102+
} catch {
103+
Write-Fail "Failed to copy config file: $($_.Exception.Message)"
104+
exit 1
105+
}
96106
} else {
97107
Write-Warn ".gitleaks.toml not found in script dir; config not copied. Create $CONFIG_DIR\gitleaks.toml manually if needed."
98108
}
@@ -101,7 +111,12 @@ Write-Host ""
101111

102112
# Step 3: Git template and hooks
103113
Write-Step "Step 3: Creating git template directory..."
104-
New-Item -ItemType Directory -Path $TEMPLATE_HOOKS -Force | Out-Null
114+
try {
115+
New-Item -ItemType Directory -Path $TEMPLATE_HOOKS -Force -ErrorAction Stop | Out-Null
116+
} catch {
117+
Write-Fail "Failed to create template hooks directory: $TEMPLATE_HOOKS - $($_.Exception.Message)"
118+
exit 1
119+
}
105120

106121
$preCommitHook = @'
107122
#!/bin/bash
@@ -210,17 +225,28 @@ exit 0
210225
# Write hooks with LF line endings (Git for Windows runs them with bash)
211226
$preCommitPath = Join-Path $TEMPLATE_HOOKS "pre-commit"
212227
$commitMsgPath = Join-Path $TEMPLATE_HOOKS "commit-msg"
213-
[System.IO.File]::WriteAllText($preCommitPath, $preCommitHook.Replace("`r`n", "`n"))
214-
[System.IO.File]::WriteAllText($commitMsgPath, $commitMsgHook.Replace("`r`n", "`n"))
215-
Write-Ok "Created pre-commit and commit-msg hooks in $TEMPLATE_HOOKS"
228+
try {
229+
[System.IO.File]::WriteAllText($preCommitPath, $preCommitHook.Replace("`r`n", "`n"))
230+
[System.IO.File]::WriteAllText($commitMsgPath, $commitMsgHook.Replace("`r`n", "`n"))
231+
Write-Ok "Created pre-commit and commit-msg hooks in $TEMPLATE_HOOKS"
232+
} catch {
233+
Write-Fail "Failed to write hook files: $($_.Exception.Message)"
234+
exit 1
235+
}
216236

217237
Write-Host ""
218238

219239
# Step 4: Configure git template
220240
Write-Step "Step 4: Configuring git to use template directory..."
221241
$templateDirNorm = $TEMPLATE_DIR -replace '\\', '/'
222-
git config --global init.templateDir $templateDirNorm
223-
Write-Ok "Set global git template directory"
242+
try {
243+
git config --global init.templateDir $templateDirNorm
244+
if ($LASTEXITCODE -ne 0) { throw "git config exited with code $LASTEXITCODE" }
245+
Write-Ok "Set global git template directory"
246+
} catch {
247+
Write-Fail "Failed to set git template directory: $($_.Exception.Message)"
248+
Write-Warn "You can set it manually: git config --global init.templateDir '$templateDirNorm'"
249+
}
224250

225251
Write-Host ""
226252
Write-Host "========================================" -ForegroundColor Green

update-all-repos.ps1

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,12 @@ function Get-GitRepos {
100100
if ($Depth -eq 1) {
101101
Write-Host " Scanning $Path" -ForegroundColor Gray
102102
}
103-
if (Test-Path (Join-Path $Path ".git")) {
104-
$script:ReposFoundList.Add($Path) | Out-Null
103+
try {
104+
if (Test-Path (Join-Path $Path ".git") -ErrorAction SilentlyContinue) {
105+
$script:ReposFoundList.Add($Path) | Out-Null
106+
}
107+
} catch {
108+
# Silently skip paths we don't have permission to read
105109
}
106110
if ($MaxDepth -gt 0 -and $Depth -ge $MaxDepth) { return }
107111
Get-ChildItem -Path $Path -Directory -ErrorAction SilentlyContinue | ForEach-Object {
@@ -117,12 +121,21 @@ function Get-GitRepos {
117121
function Install-Hooks {
118122
param([string]$RepoDir)
119123
$hooksDir = Join-Path $RepoDir ".git\hooks"
120-
if (-not (Test-Path $hooksDir)) { return $false }
124+
# Create hooks directory if it doesn't exist
125+
if (-not (Test-Path $hooksDir)) {
126+
try {
127+
New-Item -ItemType Directory -Path $hooksDir -Force -ErrorAction Stop | Out-Null
128+
} catch {
129+
Write-Fail "Cannot create hooks dir: $hooksDir - $($_.Exception.Message)"
130+
return $false
131+
}
132+
}
121133
try {
122-
Copy-Item -Path $preCommitSrc -Destination (Join-Path $hooksDir "pre-commit") -Force
123-
Copy-Item -Path $commitMsgSrc -Destination (Join-Path $hooksDir "commit-msg") -Force
134+
Copy-Item -Path $preCommitSrc -Destination (Join-Path $hooksDir "pre-commit") -Force -ErrorAction Stop
135+
Copy-Item -Path $commitMsgSrc -Destination (Join-Path $hooksDir "commit-msg") -Force -ErrorAction Stop
124136
return $true
125137
} catch {
138+
Write-Fail "Hook copy error: $($_.Exception.Message)"
126139
return $false
127140
}
128141
}

0 commit comments

Comments
 (0)