Skip to content

Instantly share code, notes, and snippets.

@crra
Created November 3, 2020 15:52
Show Gist options
  • Save crra/61407803670d472a8a617a72500b0126 to your computer and use it in GitHub Desktop.
Save crra/61407803670d472a8a617a72500b0126 to your computer and use it in GitHub Desktop.
Git-hook implemented as bash-script that adds a string to message based on the filetype
#!/usr/bin/env bash
# Simple git hook that appends/prepends a commit message with a tag
set -o errexit
set -o errtrace
set -o nounset
set -o pipefail
declare -r tag="#docu"
declare -r filefilter="\.(html|adoc)$"
declare -r commit_message_file="${1:-}"
declare -r commit_source="${2:-}"
if [ -n "$commit_message_file" ] && [ -n "$commit_source" ]; then
while IFS= read -r line || [ -n "$line" ]; do
if [[ "$line" =~ $filefilter ]]; then
readonly commit_message=$(<"$commit_message_file")
case "$commit_source" in
# message was given (via -m or -F), append the tag at the end
message)
echo "$commit_message $tag" >"$commit_message_file"
;;
merge) ;;
template) ;;
# prepend the tag to the message
*)
echo "$tag $commit_message" >"$commit_message_file"
;;
esac
break # Needs to run only once
fi
done < <(git diff --name-only --staged)
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment