GASATHON/scripts/clean_shit_up.ps1

132 lines
4.7 KiB
PowerShell
Raw Normal View History

2024-10-18 13:09:08 -07:00
# PowerShell script to remove Content directory from Git history
2024-10-18 13:26:08 -07:00
clear-host
2024-10-18 12:43:22 -07:00
2024-10-18 13:09:08 -07:00
# Script setup
$ErrorActionPreference = "Stop"
2024-10-18 13:26:08 -07:00
$VerbosePreference = "Continue"
2024-10-18 12:43:22 -07:00
2024-10-18 13:09:08 -07:00
# Path setup
$path_scripts = $PSScriptRoot
$path_helpers = Join-Path $path_scripts 'helpers'
$path_root = Split-Path -Parent -Path $path_scripts
$path_project = Join-Path $path_root 'Project'
$path_content = Join-Path $path_project 'Content'
2024-10-18 12:43:22 -07:00
# Function to remove Content directory from Git history
function Remove-ContentFromGitHistory {
2024-10-18 13:11:57 -07:00
param([string]$path_repo)
2024-10-18 13:09:08 -07:00
# Git commands and flags
$cgit_filter_branch = 'filter-branch'
$cgit_for_each_ref = 'for-each-ref'
$cgit_update_ref = 'update-ref'
$cgit_reflog_expire = 'reflog expire'
$cgit_garbage_collect = 'gc'
$fgit_aggressive = '--aggressive'
$fgit_all = '--all'
$fgit_cached = '--cached'
$fgit_format = '--format'
$fgit_expire_now = '--expire=now'
$fgit_ignore_unmatch = '--ignore-unmatch'
$fgit_index_filter = '--index-filter'
$fgit_force = '--force'
2024-10-18 12:43:22 -07:00
$fgit_prune_empty = '--prune-empty'
2024-10-18 13:09:08 -07:00
$fgit_prune_now = '--prune=now'
2024-10-18 12:43:22 -07:00
$fgit_tag_name_filter = '--tag-name-filter'
2024-10-18 13:09:08 -07:00
$fgit_stdin = '--stdin'
$fgit_filter_concat = 'cat'
$fgit_filter_separate = '--'
$fmt_delete_refs = 'delete %(refname)'
$original_refs = 'refs/original'
2024-10-18 13:26:08 -07:00
Write-Verbose "Received path_repo: $path_repo"
if ([string]::IsNullOrWhiteSpace($path_repo)) {
throw "Repository path is null or empty"
}
if (-not (Test-Path $path_repo)) {
throw "Repository path does not exist: $path_repo"
}
2024-10-18 13:09:08 -07:00
# Navigate to repository root
2024-10-18 13:11:57 -07:00
Push-Location $path_repo
2024-10-18 13:26:08 -07:00
try {
2024-10-18 13:11:57 -07:00
Write-Verbose "Current directory: $(Get-Location)"
# Check if we're in a git repository
if (-not (Test-Path (Join-Path $path_repo '.git'))) {
throw "Not in a git repository. Please run this script from the root of your git repository."
}
# Check if the Content directory exists
if (-not (Test-Path $path_content)) {
throw "Content directory not found at $path_content"
}
# Get the relative path of the content directory
2024-10-18 13:26:08 -07:00
$path_content_relative = Resolve-Path -Relative -Path $path_content
Write-Verbose "Relative content path: $path_content_relative"
2024-10-18 13:09:08 -07:00
Write-Verbose "Removing Content directory from Git history..."
2024-10-18 13:11:57 -07:00
2024-10-18 13:09:08 -07:00
# Construct and execute filter-branch command
2024-10-18 13:28:09 -07:00
$filter_command = "git rm -r --cached --ignore-unmatch `"$path_content_relative`""
$filter_branch_cmd = "git $cgit_filter_branch $fgit_force $fgit_index_filter '$filter_command' $fgit_prune_empty $fgit_tag_name_filter $fgit_filter_concat $fgit_filter_separate $fgit_all"
2024-10-18 13:26:08 -07:00
Write-Verbose "Executing command: $filter_branch_cmd"
Invoke-Expression $filter_branch_cmd
2024-10-18 13:09:08 -07:00
Write-Verbose "Cleaning up refs..."
2024-10-18 13:26:08 -07:00
# Clean up refs using git directly
2024-10-18 13:28:09 -07:00
$refs = & git show-ref --heads | ForEach-Object { $_.Split()[1] }
2024-10-18 13:26:08 -07:00
foreach ($ref in $refs) {
$originalRef = "$original_refs/$ref"
2024-10-18 13:28:09 -07:00
if (& git show-ref --verify --quiet $originalRef) {
2024-10-18 13:26:08 -07:00
Write-Verbose "Deleting ref: $originalRef"
2024-10-18 13:28:09 -07:00
& git $cgit_update_ref -d $originalRef
2024-10-18 13:26:08 -07:00
}
}
# Remove any remaining refs/original directory
$originalRefsPath = Join-Path $path_repo ".git\$original_refs"
if (Test-Path $originalRefsPath) {
Write-Verbose "Removing refs/original directory"
Remove-Item -Recurse -Force $originalRefsPath
}
2024-10-18 13:09:08 -07:00
Write-Verbose "Expiring reflog..."
2024-10-18 13:28:09 -07:00
& git $cgit_reflog_expire $fgit_expire_now $fgit_all
2024-10-18 13:09:08 -07:00
Write-Verbose "Running garbage collection..."
2024-10-18 13:28:09 -07:00
& git $cgit_garbage_collect $fgit_prune_now $fgit_aggressive
2024-10-18 13:09:08 -07:00
Write-Verbose "Content removal from Git history completed successfully."
}
catch {
Write-Error "An error occurred: $_"
}
finally {
Pop-Location
}
2024-10-18 12:43:22 -07:00
}
2024-10-18 13:09:08 -07:00
# Main execution
Write-Host "This script will remove the Content directory from your Git history."
Write-Host "This action is IRREVERSIBLE. Make sure you have a backup before proceeding."
$confirmation = Read-Host "Are you sure you want to continue? (y/n)"
if ($confirmation -ne 'y') {
Write-Host "Operation cancelled."
exit
}
# Execute the function
2024-10-18 13:26:08 -07:00
Write-Verbose "Calling Remove-ContentFromGitHistory with path_root: $path_root"
2024-10-18 13:09:08 -07:00
Remove-ContentFromGitHistory $path_root
Write-Host "Script execution completed. Please review your repository to ensure the desired outcome."
Write-Host "Remember to force-push these changes if you want to update the remote repository."