Last active
August 29, 2015 14:15
-
-
Save brentlintner/7380c877ae996de18309 to your computer and use it in GitHub Desktop.
run a command for every commit in a branch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
branch=$1 | |
cmd=$2 | |
usage() { | |
echo "git-for-each [-h] branch command" | |
echo " => run a command for each commit in branch (off master)"; | |
} | |
for_each() { | |
for commit in $(git log --pretty=oneline --no-merges master..$branch | awk '{ print $1 }') | |
do | |
git checkout $commit > /dev/null 2>&1 | |
if [ "$?" -gt 0 ]; then | |
echo "git checkout $commit failed (aborting)." | |
echo "do you have diffs preventing a clean checkout?" | |
exit 1 | |
fi | |
echo "starting: $commit" | |
eval $cmd > /dev/null 2>&1 | |
status=$? | |
git checkout $branch > /dev/null 2>&1 | |
if [ "$?" -gt 0 ]; then | |
echo "git checkout $branch failed (aborting)." | |
exit 1 | |
fi | |
if [ "$status" -gt 0 ]; then | |
echo "failed" | |
else | |
echo "succeeded" | |
fi | |
done | |
} | |
run() { | |
if [ "$branch" = "--help" ] || [ "$branch" = "-h" ] ; then | |
usage; exit; | |
fi | |
if [ -z "${branch}" ]; then | |
echo "branch is empty\n"; usage; exit 1; | |
fi | |
if [ -z "${cmd}" ]; then | |
echo "command is empty\n"; usage; exit 1; | |
fi | |
for_each | |
} | |
run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: