Skip to content

Instantly share code, notes, and snippets.

@archgrove
Created September 30, 2013 16:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save archgrove/6766129 to your computer and use it in GitHub Desktop.
Save archgrove/6766129 to your computer and use it in GitHub Desktop.
Replays a git history, passing the repository at each state to a given script
#!/bin/bash
# Takes a repository and script from the command line
# and executes the script for each git log entry in reverse chronological order
# Use temporary files of the following format
TMP_TEMPLATE="/tmp/gitreplay-XXXXXXXX"
# Validate command line parameters
if [ -z $1 -o -z $2 ]; then
echo "Usage: gitReplay.sh REPOSITORY SCRIPT"
exit
fi
if [ ! -x $2 ]; then
echo "'$2' does not exist as an executable file"
exit
fi
REPOSITORY=$1
SCRIPT=$2
# Generate and move to a temporary folder
tmp_loc=`mktemp -d $TMP_TEMPLATE`
pushd $tmp_loc > /dev/null
# Checkout the repository, and bail out if it fails
git clone $REPOSITORY . 2>&1
if [ $? -ne 0 ]; then
echo "Could not checkout repository"
popd > /dev/null
exit;
fi
# Determine all the hashes of the repository history
hashes=`git log --reverse --pretty=format:%H`
# For every hash, move to that revision and pass it to the repository
index=0
for hash in $hashes; do
git checkout $hash
bash $SCRIPT $hash $index $tmp_loc
if [ $? -ne 0 ]; then
echo "Early terminating based on script return code"
break
fi
let index=$index+1
done
popd > /dev/null
rm -rf $tmp_loc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment