Created
January 7, 2024 11:35
-
-
Save ivan-osipov/57fc43b9a1192fcd61df1e113e22d19f to your computer and use it in GitHub Desktop.
Git Hook "prepare-commit-msg" to add branch name to commit body
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/sh | |
# get file path | |
COMMIT_MSG_FILE="$1" | |
# get file content | |
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE") | |
# find the current branch name | |
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) | |
# format extra content for commit message | |
SUFFIX=$(printf "\n\nBranch: %s" "$BRANCH_NAME") | |
# handle rebase process and amend commits | |
if [ "${COMMIT_MSG%"$SUFFIX"}" = "$COMMIT_MSG" ] && [ "$BRANCH_NAME" != "HEAD" ]; then | |
printf "%s" "$SUFFIX" >> "$COMMIT_MSG_FILE" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment