Skip to content

Instantly share code, notes, and snippets.

@ColinRyan
Last active December 15, 2016 17:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ColinRyan/0bc69d579f0fc35b123a46ce06617b67 to your computer and use it in GitHub Desktop.
Save ColinRyan/0bc69d579f0fc35b123a46ce06617b67 to your computer and use it in GitHub Desktop.
automation of a common scenario that occurs when dealing with pull request reviews
# 1) Pick a branch ($1)
# 2) stash
# 3) checkout
# 4) updated files from last commit
# 5) commit and push if new edits made
# 6) back to previous branch
# 7) pop stash
g () {
cd $(git rev-parse --show-toplevel)
echo "" > ~/.g3old
echo "" > ~/.g3new
oldStashCount=$(git stash list | wc -l)
if [ ! -z "$1" ]; then
git stash
git checkout $1
fi
oldIFS="$IFS"
IFS='
'
IFS=${IFS:0:1}
options=('done')
options+=($(git diff --name-only HEAD~1 HEAD))
IFS="$oldIFS"
echo ""
PS3='Select a file to edit:'
select opt in "${options[@]}"
do
case $opt in
'done') break;;
*) md5sum $opt >>~/.g3old;vim $opt;md5sum $opt >> ~/.g3new;
esac
done
echo ""
old=$(md5sum ~/.g3old | cut -d" " -f1)
new=$(md5sum ~/.g3new | cut -d" " -f1)
if [ "$old" != "$new" ] ; then
git add -u
options=("*BUG-FIX*" "*REFACTORING*" "*CONFIG*" "*FEATURE*")
PS3='Select a commit tag:'
select opt in "${options[@]}"
do
case $opt in
*) tag=$opt; break;;
esac
done
read -p 'Create a commit message:' msg
git commit -m "$tag:$msg"
git push origin HEAD
fi
if [ ! -z "$1" ]; then
git checkout -
newStashCount=$(git stash list | wc -l)
if [ $newStashCount -gt $oldStashCount ]; then
git stash pop
fi
fi
cd -
}
__git_complete g _git_checkout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment