Skip to content

Instantly share code, notes, and snippets.

@n3r0-ch
Created June 19, 2016 15:04
Show Gist options
  • Save n3r0-ch/ae2ee7fbc94b26ad5431ac0d7f8f74a4 to your computer and use it in GitHub Desktop.
Save n3r0-ch/ae2ee7fbc94b26ad5431ac0d7f8f74a4 to your computer and use it in GitHub Desktop.
Git Repo Cleanup
#!/bin/bash
# Configuration
BASE_DIR=$(dirname "$0")
MAX_DEPTH=3
# Change directory to base dir
cd $BASE_DIR
# Define cleanup function
function cleanupRepo {
echo "Starting cleanup task of git repository $1..."
# Cleanup node_modules
for i in $(find "$1" -name "node_modules" -type d); do
NODE_MODULES_DIR="$(dirname "$i")/node_modules"
if [ -d "$NODE_MODULES_DIR" ]; then
rm -rf "$NODE_MODULES_DIR"
fi
done
# Cleanup bower_components
for i in $(find "$1" -name "bower_components" -type d); do
BOWER_COMPONENTS_DIR="$(dirname "$i")/bower_components"
if [ -d "$BOWER_COMPONENTS_DIR" ]; then
rm -rf "$BOWER_COMPONENTS_DIR"
fi
done
# Delete vendor directories in composer projects
for i in $(find "$1" -name "composer.json"); do
VENDOR_DIR="$(dirname "$i")/vendor"
if [ -d "$VENDOR_DIR" ]; then
rm -rf "$VENDOR_DIR"
fi
done
# Cleanup Vagrant VM's
for i in $(find "$1" -name "Vagrantfile"); do
export VAGRANT_CWD=$(dirname "$i")
vagrant destroy --force
unset VAGRANT_CWD
done
}
# Enumerate all repositories
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for i in $(find "$BASE_DIR" -maxdepth $MAX_DEPTH -type d); do
if [ -d "$i/.git" ]; then
read -p "Cleanup $i (y/n)? " CONT
if [ "$CONT" == "y" ]; then
cleanupRepo $i;
fi
fi
done
IFS=$SAVEIFS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment