Created
October 27, 2016 19:40
-
-
Save kenny-evitt/ba24611cc6d015521211c1d441eb54c9 to your computer and use it in GitHub Desktop.
Add `git diff --cached` output as a comment to commit messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
commit_message_file="$1" | |
commit_type="$2" | |
commit_hash="$3" # This is empty if an existing commit is not being amended | |
diff_cached_output=$(git diff --cached) | |
diff_cached_output=$(echo "$diff_cached_output" | sed 's/^/# /') | |
diff_cached_message="# \`git diff --cached\` output: | |
$diff_cached_output" | |
# Quote a string to be used as substitution for a `sed` program | |
quoteSedSubstitution() { | |
IFS= read -d '' -r < <(sed -e ':a' -e '$!{N;ba' -e '}' -e 's/[&/\]/\\&/g; s/\n/\\&/g' <<<"$1") | |
printf %s "${REPLY%$'\n'}" | |
} | |
sed -i -e "\$a$(quoteSedSubstitution "$diff_cached_message")" "$commit_message_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment