#!/bin/bash

#
# USE WITH CAUTION.  DO NOT USE WHILE YOUR WORLD IS RUNNING.
# IF YOU DON"T UNDERSTAND WHAT THIS DOES, YOU PROBABLY SHOULDN'T
# USE IT.
#
# Attempts to reclaim space by removing any extraneous objects that may be
# referencing purged snapshot commits.
#

ROOT=$(git rev-parse --show-toplevel)

if [ ! -f "$ROOT/fastback/world.uuid" ]; then
    echo 'This command should only be run in a Minecraft world that has been backed up with FastBack.'
    exit 1
fi

echo 'Size before:'
du -sh "$ROOT/.git"
echo

# delete all tracking branches
git branch -r | xargs -L 1 git branch -rD

# expire all reflogs
git reflog expire --expire=all --all

# gc, pruning all unreferenced loose objects
git gc --prune=now

echo
echo 'Size after:'
du -sh "$ROOT/.git"
