Skip to content

Instantly share code, notes, and snippets.

@felixrabe
Last active December 23, 2015 09:09
Show Gist options
  • Save felixrabe/6612477 to your computer and use it in GitHub Desktop.
Save felixrabe/6612477 to your computer and use it in GitHub Desktop.
git-exec
#!/bin/bash
# Author: Felix Rabe
# Public Domain, available from https://gist.github.com/felixrabe/6612477
# Like 'git cherry-pick' with added execution support.
# When it sees that the commit message starts with '$ ', it will execute the
# commit, then create a new commit with all the changes, instead of just
# cherry-picking.
if [[ $# -lt 1 ]] ; then
echo "Usage: git-exec <commit>..." >&2
exit 1
fi
set -e
function execute_commit() {
commit="$1"
message=$(git cat-file commit "$commit" | sed '1,/^$/d') # http://stackoverflow.com/a/11314667/1034080
if [[ $message == \$\ * ]] ; then
# execute and commit
echo "$message"
/bin/bash -c "${message#$ }"
git add -A . && git c --allow-empty -C "$commit"
else
# just cherry-pick
git cherry-pick "$commit"
fi
}
while [[ $# -gt 0 ]] ; do
commit="$1"
shift
if [[ $commit == *..* ]] ; then # range of commits
for commit in $(git rev-list --reverse "$commit") ; do
execute_commit "$commit"
done
else # single commit
execute_commit "$commit"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment