Skip to content

Instantly share code, notes, and snippets.

@tomaszprasolek
Last active November 6, 2018 05:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tomaszprasolek/34ab73106d7e8e61853436ae1f18eead to your computer and use it in GitHub Desktop.
#!/bin/bash
USAGE="USAGE: git append TEXT_TO_APPEND NUMBER_OF_COMMITS\nExample: git append \"#3301\" 5"
show_usage () {
echo -e "\n$USAGE"
}
case $1 in
help | -h | "/?")
show_usage
exit 1
esac
if [ x = x${1} ] && [ x = x${2} ]; then
echo ">>> Both parameters are empty."
show_usage
exit 1
fi
if [ x = x${2} ]; then
echo ">>> Second parameter is empty."
show_usage
exit 1
fi
# Check if is integer
# https://unix.stackexchange.com/questions/151654/checking-if-an-input-number-is-an-integer
if ! [ "$2" -eq "$2" ] 2> /dev/null
then
echo ">>> Second parameter must be an integer!"
show_usage
exit 1
fi
echo "You want to append text: \"$1\" to last $2 commits."
# https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script
while true; do
read -p "Do you want to continue [y/n]? " yn
case $yn in
[Yy]* ) git filter-branch -f --msg-filter 'sed "s/\(.*\)/\1 '$1'/g"' HEAD~$2..HEAD; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes [y] or no [n].";;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment