Skip to content

Commit

Permalink
test: commitmessage: Simplify, actually run without PR
Browse files Browse the repository at this point in the history
Also now shows the title line of offensive commit message.
  • Loading branch information
ffrank committed Mar 6, 2024
1 parent e0aec36 commit a885fa0
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions test/test-commit-message.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ commit_title_regex='^\([a-z0-9]\(\(, \)\|[a-z0-9]\)*[a-z0-9]: \)\+[A-Z0-9][^:]\+

test_commit_message() {
echo "Testing commit message $1"
if ! git log --format=%s $1 | head -n 1 | grep -q "$commit_title_regex"
title=$(git log --format=%s $1 | head -n 1)
if ! echo "$title" | grep -q "$commit_title_regex"
then
echo "FAIL: Commit message should match the following regex: '$commit_title_regex'"
echo "('$title' does not match)"
echo
echo "eg:"
echo "valid examples:"
echo "prometheus: Implement rest api"
echo "resources: svc: Fix a race condition with reloads"
exit 1
Expand Down Expand Up @@ -155,42 +157,30 @@ test_commit_message_body() {
if [[ -n "$TRAVIS_PULL_REQUEST_SHA" ]]
then
commits=$(git log --format=%H origin/${TRAVIS_BRANCH}..${TRAVIS_PULL_REQUEST_SHA})
[[ -n "$commits" ]]

for commit in $commits
do
test_commit_message $commit
test_commit_message_body $commit
test_commit_message_common_bugs $commit
done
elif [[ -n "$GITHUB_SHA" ]]
then
# GITHUB_SHA is the HEAD of the branch
# GITHUB_REF: The branch or tag ref that triggered the workflow. For example, refs/heads/feature-branch-1. If neither a branch or tag is available for the event type, the variable will not exist.
# GITHUB_REF: The branch or tag ref that triggered the workflow. For example, refs/heads/feature-branch-1. If neither a branch or tag is available for the event type, the variable will not exist. (Not used.)
# GITHUB_BASE_REF: Only set for pull request events. The name of the base branch.
head=${GITHUB_SHA}
if [[ -n "${GITHUB_BASE_REF}" ]]; then
ref=${GITHUB_BASE_REF}
head=${GITHUB_SHA}
else
ref=$(echo $GITHUB_REF | cut -d/ -f3-)
head=""
# this is just a push, no PR. Check commits outside origin/master
ref=master
fi
commits=$(git log --no-merges --format=%H origin/${ref}..${head})
if [[ -n "$commits" ]]; then
for commit in $commits
do
test_commit_message $commit
test_commit_message_body $commit
test_commit_message_common_bugs $commit
done
fi
else # assume local branch
commits=$(git log --no-merges --format=%H origin/master..HEAD)
fi

if [[ -n "$commits" ]]; then
for commit in $commits
do
test_commit_message $commit
test_commit_message_body $commit
test_commit_message_common_bugs $commit
done
fi

echo 'PASS'

0 comments on commit a885fa0

Please sign in to comment.