Skip to content

Instantly share code, notes, and snippets.

@nicokruger
Created March 18, 2012 16:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicokruger/2077166 to your computer and use it in GitHub Desktop.
Save nicokruger/2077166 to your computer and use it in GitHub Desktop.
Execute a command for all commits in a git repository.
#!/bin/bash
if ! [ -d ".git" ]; then
echo "Script must be run from within a git repository"
exit 1
fi
if [ $# -ne 1 ]; then
echo "Please specify a script to execute for each commit"
echo "Script will recieve <repo_dir> <commit ref> as arguments"
echo "where repo_dir will already be checked out to the appropriate commit"
exit 1
fi
SCRIPT=$1
TMP=$(mktemp -d -q)
REPO=$(pwd)
# clone the git repository
pushd $TMP > /dev/null 2>&1
git clone $REPO 2> /dev/null 1>&2
cd $(basename $REPO)
# get all
for F in $(git log --children | grep commit\ | sed "s;commit ;;" | tac | xargs echo); do
git checkout $F 2> /dev/null 1>&2
bash $SCRIPT $F
done
popd > /dev/null
rm -rf $TMP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment