Last active
August 13, 2024 19:19
-
-
Save MortalHappiness/841817e4dcb0ac20aad76ac8d2ecece8 to your computer and use it in GitHub Desktop.
commit-msg git hook to add Sign-off-by trailer to every commit message automatically
This file contains hidden or 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
#!/usr/bin/env bash | |
# Usage: | |
# 1. Download this file and save it as .git/hooks/commit-msg under current repo. | |
# 2. Run `chmod +x .git/hooks/commit-msg` | |
COMMIT_MSG_FILE=$1 | |
# If the commit message is fixup! or squash! or Merge, exit without changing it. | |
grep -qE '^(fixup!|squash!|Merge)' "$COMMIT_MSG_FILE" && exit 0 | |
# Add a Signed-off-by line if one does not already exist. | |
SIGN_OFF_BY_TRAILER=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') | |
grep -qs "^$SIGN_OFF_BY_TRAILER" "$COMMIT_MSG_FILE" || | |
git interpret-trailers --in-place --trailer "$SIGN_OFF_BY_TRAILER" "$COMMIT_MSG_FILE" | |
# Detect and abort duplicate Signed-off-by lines. | |
test "" = "$(grep '^Signed-off-by: ' "$COMMIT_MSG_FILE" | | |
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { | |
echo >&2 Duplicate Signed-off-by lines. | |
exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment