Skip to content

Instantly share code, notes, and snippets.

@shivansh
Created December 2, 2016 07:20
Show Gist options
  • Save shivansh/accca4ad922645b61d5741135bd1ae01 to your computer and use it in GitHub Desktop.
Save shivansh/accca4ad922645b61d5741135bd1ae01 to your computer and use it in GitHub Desktop.
zsh plugin for recovering lost commit message when `git commit` exits unsuccessfully
git() {
setopt localtraps;
trap "(){}" SIGHUP SIGINT SIGTERM
commit=
function clean_up() {
unset commit; # declare it local first
rm -f .git/COMMIT_EDITMSG_BAK
}
command git "$@";
if (( $? != 0 )) && [[ $1 == "commit" ]]; then
# Ignore commented lines while copying the commit message
sed -n '/Please enter the commit message /q;p' .git/COMMIT_EDITMSG >! .git/COMMIT_EDITMSG_BAK
commit="true" # don't export
# Prompt the user in the next invocation of `git commit`
elif [[ -z $commit ]] && [[ $1 == "commit" ]]; then # $commit is referenced but never set
# TODO: At this point we have to interrupt the usual flow of `git commit`
# and then proceed to avoid overwriting .git/COMMIT_EDITMSG
read -q "Old commit message found. Do you wish to use it?" yn
case $yn in
[Yy]* ) git commit -F .git/COMMIT_EDITMSG_BAK; clean_up;;
[Nn]* ) clean_up;; # function is undefined at this point in the code (first call to git() in a given shell session)
* ) echo "Please answer yes or no.";;
esac
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment