Skip to content

Instantly share code, notes, and snippets.

@brentlintner
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentlintner/7380c877ae996de18309 to your computer and use it in GitHub Desktop.
Save brentlintner/7380c877ae996de18309 to your computer and use it in GitHub Desktop.
run a command for every commit in a branch
#!/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
@brentlintner
Copy link
Author

Example:

~/src/some_project(some_branch)$ git-for-each some_branch "grunt dev:test"
starting: 36df9df75a9221ef87cf2ee4424567680b6be149
succeeded
starting: 11e95fb860a2a38e9dc7649e5e1ee39463801b54
failed
starting: 9a815f51623064b099ce74b24e77a6ead36a7655
failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment