Files

125 lines
4.7 KiB
PowerShell

# PowerShell script to purge nested git configurations
# Run this from your repository root
param(
[string]$RepoPath = ".",
[switch]$DryRun = $false
)
$ErrorActionPreference = "Continue"
$itemsRemoved = @()
$itemsFailed = @()
# Get absolute path
$absPath = Resolve-Path $RepoPath
Write-Host "Scanning repository: $absPath" -ForegroundColor Cyan
Write-Host "Mode: $(if ($DryRun) { 'DRY RUN (no changes will be made)' } else { 'LIVE (files will be deleted)' })" -ForegroundColor Yellow
Write-Host ""
# Find nested .git directories (excluding root)
Write-Host "Scanning for nested .git directories..." -ForegroundColor Cyan
$gitDirs = Get-ChildItem -Path $absPath -Recurse -Directory -Force -Filter ".git" -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -ne "$absPath\.git" }
if ($gitDirs) {
Write-Host "Found $($gitDirs.Count) nested .git directory(ies):" -ForegroundColor Yellow
foreach ($dir in $gitDirs) {
$relativePath = $dir.FullName.Substring($absPath.Path.Length + 1)
Write-Host " - $relativePath" -ForegroundColor Gray
if (-not $DryRun) {
try {
Remove-Item -Recurse -Force $dir.FullName -ErrorAction Stop
$itemsRemoved += $relativePath
Write-Host " DELETED" -ForegroundColor Green
} catch {
$itemsFailed += $relativePath
Write-Host " FAILED: $_" -ForegroundColor Red
}
}
}
} else {
Write-Host "No nested .git directories found." -ForegroundColor Green
}
Write-Host ""
# Find nested .gitignore files (excluding root and .specstory)
Write-Host "Scanning for nested .gitignore files..." -ForegroundColor Cyan
$gitignores = Get-ChildItem -Path $absPath -Recurse -File -Force -Filter ".gitignore" -ErrorAction SilentlyContinue |
Where-Object {
$_.FullName -ne "$absPath\.gitignore" -and
$_.FullName -notlike "*\.specstory\*"
}
if ($gitignores) {
Write-Host "Found $($gitignores.Count) nested .gitignore file(s):" -ForegroundColor Yellow
foreach ($file in $gitignores) {
$relativePath = $file.FullName.Substring($absPath.Path.Length + 1)
Write-Host " - $relativePath" -ForegroundColor Gray
if (-not $DryRun) {
try {
Remove-Item -Force $file.FullName -ErrorAction Stop
$itemsRemoved += $relativePath
Write-Host " DELETED" -ForegroundColor Green
} catch {
$itemsFailed += $relativePath
Write-Host " FAILED: $_" -ForegroundColor Red
}
}
}
} else {
Write-Host "No nested .gitignore files found." -ForegroundColor Green
}
Write-Host ""
# Find nested .gitattributes files (excluding root)
Write-Host "Scanning for nested .gitattributes files..." -ForegroundColor Cyan
$gitattributes = Get-ChildItem -Path $absPath -Recurse -File -Force -Filter ".gitattributes" -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -ne "$absPath\.gitattributes" }
if ($gitattributes) {
Write-Host "Found $($gitattributes.Count) nested .gitattributes file(s):" -ForegroundColor Yellow
foreach ($file in $gitattributes) {
$relativePath = $file.FullName.Substring($absPath.Path.Length + 1)
Write-Host " - $relativePath" -ForegroundColor Gray
if (-not $DryRun) {
try {
Remove-Item -Force $file.FullName -ErrorAction Stop
$itemsRemoved += $relativePath
Write-Host " DELETED" -ForegroundColor Green
} catch {
$itemsFailed += $relativePath
Write-Host " FAILED: $_" -ForegroundColor Red
}
}
}
} else {
Write-Host "No nested .gitattributes files found." -ForegroundColor Green
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
# Summary
$totalFound = ($gitDirs.Count + $gitignores.Count + $gitattributes.Count)
if ($totalFound -eq 0) {
Write-Host "Repository is clean! No nested git configurations found." -ForegroundColor Green
} elseif ($DryRun) {
Write-Host "DRY RUN COMPLETE" -ForegroundColor Yellow
Write-Host "Found $totalFound item(s) that would be removed." -ForegroundColor Yellow
Write-Host "Run without -DryRun to actually delete these files." -ForegroundColor Cyan
} else {
Write-Host "PURGE COMPLETE" -ForegroundColor Green
Write-Host "Items removed: $($itemsRemoved.Count)" -ForegroundColor Green
if ($itemsFailed.Count -gt 0) {
Write-Host "Items failed: $($itemsFailed.Count)" -ForegroundColor Red
foreach ($item in $itemsFailed) {
Write-Host " - $item" -ForegroundColor Red
}
}
}
Write-Host "========================================" -ForegroundColor Cyan