From b712935571355df44e126c5b27c98ccd53d1d18b Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Wed, 5 Aug 2020 08:32:06 +0200 Subject: tools/check-commits.sh: fix commit range detection We currently check from github.event.pull_request.base.sha to github.event.pull_request.head.sha, but they may be in different branches if the PR commits are branched not from the latest master HEAD (at the time of PR creation). Then GH will create a merge commit, and the range we try to check is not valid. Check github.event.pull_request.commits commits backwards from github.event.pull_request.head.sha commit. --- tools/check-commits.sh | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'tools/check-commits.sh') diff --git a/tools/check-commits.sh b/tools/check-commits.sh index e2734d3dd..aa06eed5a 100755 --- a/tools/check-commits.sh +++ b/tools/check-commits.sh @@ -4,22 +4,21 @@ set -e -# GITHUB_PR_BASE_SHA is exported in .github/workflows/ci.yml for pull requests. -# If it is not set, check against refs/heads/master (presumably a local run), -# otherwise skip the checks (presumably CI run on a fork commit). -if [ "${GITHUB_PR_BASE_SHA}" == "" ]; then - GITHUB_PR_BASE_SHA="refs/heads/master" - HAVE_MASTER=0 - git show-ref ${GITHUB_PR_BASE_SHA} 1>/dev/null 2>&1 || HAVE_MASTER=$? - if [[ HAVE_MASTER -ne 0 ]]; then - echo "skipping commit checks: GITHUB_PR_BASE_SHA is not set and ${GITHUB_PR_BASE_SHA} does not exist" - exit 0 +# .github/workflows/ci.yml passes GITHUB_PR_BASE_SHA and GITHUB_PR_COMMITS for pull requests. +# That's the range we want to check for PRs. If these are not set, we check from the current HEAD +# to the master branch (presumably a local run). If master does not exist (presumably CI run on +# a commit into a fork tree), check HEAD commit only. +GITHUB_PR_BASE_SHA="${GITHUB_PR_BASE_SHA:-HEAD}" +if [ "${GITHUB_PR_COMMITS}" == "" ]; then + GITHUB_PR_COMMITS=`git log --oneline master..${GITHUB_PR_BASE_SHA} | wc -l` + if [ "${GITHUB_PR_COMMITS}" == "" ] || [ "${GITHUB_PR_COMMITS}" == "0" ]; then + GITHUB_PR_COMMITS=1 fi fi COMMITS=0 FAILED="" -HASHES=$(git log --format="%h" ${GITHUB_PR_BASE_SHA}..HEAD) +HASHES=$(git log --format="%h" -n ${GITHUB_PR_COMMITS} ${GITHUB_PR_BASE_SHA}) for HASH in ${HASHES}; do ((COMMITS+=1)) SUBJECT=$(git show --format="%s" --no-patch ${HASH}) -- cgit mrf-deployment