From e6195f4b8d368c8fc074f04025f8eefa9121e49a Mon Sep 17 00:00:00 2001 From: sanj <67624670+iodrift@users.noreply.github.com> Date: Tue, 30 Jul 2024 20:47:09 -0700 Subject: [PATCH] Auto-update: Tue Jul 30 20:47:09 PDT 2024 --- gitpurge | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 gitpurge diff --git a/gitpurge b/gitpurge new file mode 100755 index 0000000..8590631 --- /dev/null +++ b/gitpurge @@ -0,0 +1,51 @@ +#!/bin/bash + +# Ensure we're in a git repository +if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then + echo "Error: This script must be run inside a Git repository." + exit 1 +fi + +# Get the current branch name +current_branch=$(git rev-parse --abbrev-ref HEAD) + +# Switch to the main branch +git checkout main + +# Get a list of all files that have ever existed in the repository +all_files=$(git log --pretty=format: --name-only --diff-filter=A | sort -u) + +# Get a list of files that currently exist in the repository +current_files=$(git ls-files) + +# Find files that no longer exist +files_to_remove=$(comm -23 <(echo "$all_files") <(echo "$current_files")) + +# If there are no files to remove, exit +if [ -z "$files_to_remove" ]; then + echo "No files to remove." + git checkout "$current_branch" + exit 0 +fi + +# Create a new branch for our changes +new_branch="cleanup-$(date +%Y%m%d%H%M%S)" +git checkout -b "$new_branch" + +# Remove the files from all commits +git filter-branch --force --index-filter \ + 'git rm --cached --ignore-unmatch $(cat <