diff --git a/scripts/check-commitlog.sh b/scripts/check-commitlog.sh index e12589250..23cd3f4bb 100755 --- a/scripts/check-commitlog.sh +++ b/scripts/check-commitlog.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # Check that every non-merge commit after the specified base commit has a commit # message ending with a valid Change-Id line. A valid Change-Id line must be the @@ -8,6 +8,16 @@ # # Merge commits are excluded from this check. +# Ensure that the common script exists and is readable, then verify it has no +# syntax errors and defines the required function. +common_script="$(dirname "$0")/common.sh" +[ -r "$common_script" ] || { echo "[!] '$common_script' not found or not readable." >&2; exit 1; } +bash -n "$common_script" >/dev/null 2>&1 || { echo "[!] '$common_script' contains syntax errors." >&2; exit 1; } +source "$common_script" +declare -F set_colors >/dev/null 2>&1 || { echo "[!] '$common_script' does not define the required function." >&2; exit 1; } + +set_colors + # Base commit from which to start checking. BASE_COMMIT="0b8be2c15160c216e8b6ec82c99a000e81c0e429" @@ -33,10 +43,7 @@ for commit in $commits; do done if [ $failed -ne 0 ]; then - echo - echo "Some commits are missing a valid Change-Id. Please amend the commit messages accordingly." - echo - exit 1 + throw "Some commits are missing a valid Change-Id. Please amend the commit messages accordingly." fi exit 0 diff --git a/scripts/check-repo.sh b/scripts/check-repo.sh index 28d40f995..b0f9846bd 100755 --- a/scripts/check-repo.sh +++ b/scripts/check-repo.sh @@ -1,7 +1,14 @@ #!/usr/bin/env bash -# Source the common utilities -source "$(dirname "$0")/common.sh" +# Ensure that the common script exists and is readable, then verify it has no +# syntax errors and defines the required function. +common_script="$(dirname "$0")/common.sh" +[ -r "$common_script" ] || { echo "[!] '$common_script' not found or not readable." >&2; exit 1; } +bash -n "$common_script" >/dev/null 2>&1 || { echo "[!] '$common_script' contains syntax errors." >&2; exit 1; } +source "$common_script" +declare -F set_colors >/dev/null 2>&1 || { echo "[!] '$common_script' does not define the required function." >&2; exit 1; } + +set_colors check_github_actions diff --git a/scripts/checksums b/scripts/checksums index 87ff044d7..533a53afd 100644 --- a/scripts/checksums +++ b/scripts/checksums @@ -1,3 +1,3 @@ 398b59c23f699ff1bf1e73a94503a9a91caa9207 queue.h 9be9666430f392924f5d27caa71a412527bf9267 list.h -3bb0192cee08d165fd597a9f6fbb404533e28fcf scripts/check-commitlog.sh +1029c2784b4cae3909190c64f53a06cba12ea38e scripts/check-commitlog.sh diff --git a/scripts/commit-msg.hook b/scripts/commit-msg.hook index 498cc7680..71ffad1c5 100755 --- a/scripts/commit-msg.hook +++ b/scripts/commit-msg.hook @@ -11,31 +11,20 @@ HOOK_EDITOR= SKIP_DISPLAY_WARNINGS=0 WARNINGS= -RED= -YELLOW= -BLUE= -WHITE= -CYAN= -NC= - -# Set colour variables if the output should be coloured. -set_colors() { - local default_color=$(git config --get hooks.goodcommit.color || git config --get color.ui || echo 'auto') - if [[ $default_color == 'always' ]] || [[ $default_color == 'auto' && -t 1 ]]; then - RED='\033[1;31m' - YELLOW='\033[1;33m' - BLUE='\033[1;34m' - WHITE='\033[1;37m' - CYAN='\033[1;36m' - NC='\033[0m' # No Color - fi -} +# Ensure that the common script exists and is readable, then verify it has no +# syntax errors and defines the required function. +common_script="$(dirname "$0")/../../scripts/common.sh" +[ -r "$common_script" ] || { echo "[!] '$common_script' not found or not readable." >&2; exit 1; } +bash -n "$common_script" >/dev/null 2>&1 || { echo "[!] '$common_script' contains syntax errors." >&2; exit 1; } +source "$common_script" +declare -F set_colors >/dev/null 2>&1 || { echo "[!] '$common_script' does not define the required function." >&2; exit 1; } # Set the hook editor, using the same approach as git. set_editor() { - # $GIT_EDITOR appears to always be set to `:` when the hook is executed by Git? - # ref: http://stackoverflow.com/q/41468839/885540 - # ref: https://github.com/tommarshall/git-good-commit/issues/11 + # $GIT_EDITOR appears to always be set to ':' when the hook is executed by Git? + # Reference: + # - http://stackoverflow.com/q/41468839/885540 + # - https://github.com/tommarshall/git-good-commit/issues/11 # HOOK_EDITOR=$GIT_EDITOR test -z "${HOOK_EDITOR}" && HOOK_EDITOR=$(git config --get core.editor) test -z "${HOOK_EDITOR}" && HOOK_EDITOR=$VISUAL diff --git a/scripts/common.sh b/scripts/common.sh index 1890c430b..bb6353ebf 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -102,5 +102,3 @@ make_random_string() { echo "${raw_rand:0:pos}${sub_str}${raw_rand:pos}" fi } - -set_colors diff --git a/scripts/install-git-hooks b/scripts/install-git-hooks index 7fec80501..53b68a25d 100755 --- a/scripts/install-git-hooks +++ b/scripts/install-git-hooks @@ -1,16 +1,40 @@ #!/usr/bin/env bash +# Ensure that the common script exists and is readable, then verify it has no +# syntax errors and defines the required function. +common_script="$(dirname "$0")/common.sh" +[ -r "$common_script" ] || { echo "[!] '$common_script' not found or not readable." >&2; exit 1; } +bash -n "$common_script" >/dev/null 2>&1 || { echo "[!] '$common_script' contains syntax errors." >&2; exit 1; } +source "$common_script" +declare -F set_colors >/dev/null 2>&1 || { echo "[!] '$common_script' does not define the required function." >&2; exit 1; } + +set_colors + +TOTAL_STEPS=5 +CURRENT_STEP=0 + +# 1. Validate the workspace +((CURRENT_STEP++)) +progress "$CURRENT_STEP" "$TOTAL_STEPS" + if ! test -d .git; then - echo "Execute scripts/install-git-hooks in the top-level directory." - exit 1 + throw "Execute scripts/install-git-hooks in the top-level directory." +fi + +workspace=$(git rev-parse --show-toplevel) +if [ ! -d "$workspace" ]; then + throw "The workspace path '$workspace' contains non-ASCII characters." fi +# 2. Check GitHub account +((CURRENT_STEP++)) +progress "$CURRENT_STEP" "$TOTAL_STEPS" + ACCOUNT=$(git config -l | grep -w remote.origin.url | sed -e 's/^.*github.com[\/:]\(.*\)\/lab0-c.*/\1/') CURL=$(which curl) if [ $? -ne 0 ]; then - echo "[!] curl not installed." >&2 - exit 1 + throw "curl not installed." fi CURL_RES=$(${CURL} -s \ @@ -19,44 +43,51 @@ https://api.github.com/repos/${ACCOUNT}/lab0-c/actions/workflows) TOTAL_COUNT=$(echo ${CURL_RES} | sed -e 's/.*"total_count": \([^,"]*\).*/\1/') case ${TOTAL_COUNT} in - *"Not Found"*) - echo "Check your repository. It should be located at https://github.com/${ACCOUNT}/lab0-c" - exit 1 + *"Not Found"*) + throw "Check your repository. It should be located at https://github.com/${ACCOUNT}/lab0-c" esac +# 3. Ensure this repository is frok from sysprog21/lab0-c'. +((CURRENT_STEP++)) +progress "$CURRENT_STEP" "$TOTAL_STEPS" + if [[ "${ACCOUNT}" != "sysprog21" ]]; then - REPO_FORKED=$(${CURL} -s \ - -H "Accept: application/vnd.github.v3+json" \ - https://api.github.com/repos/${ACCOUNT}/lab0-c | grep -m 1 fork) - case ${REPO_FORKED} in - *true*) - ;; - *) - echo "Your repository MUST be forked from sysprog21/lab0-c" - exit 1 - esac + REPO_FORKED=$(${CURL} -s \ + -H "Accept: application/vnd.github.v3+json" \ + https://api.github.com/repos/${ACCOUNT}/lab0-c | grep -m 1 fork) + case ${REPO_FORKED} in + *true*) + ;; + *) + throw "Your repository MUST be forked from 'sysprog21/lab0-c'." + esac fi +# 4. Check GitHub Actions +((CURRENT_STEP++)) +progress "$CURRENT_STEP" "$TOTAL_STEPS" + if [ ${TOTAL_COUNT} -eq "0" ]; then - echo "Fatal: GitHub Actions MUST be activated." - case ${OSTYPE} in - "linux"*) - xdg-open https://github.com/${ACCOUNT}/lab0-c/actions > /dev/null 2>&1 - ;; - "darwin"*) - open https://github.com/${ACCOUNT}/lab0-c/actions - ;; - *) - echo "Please activate at https://github.com/${ACCOUNT}/lab0-c/actions" - ;; - esac - echo "Check this article: https://docs.github.com/en/actions/managing-workflow-runs/disabling-and-enabling-a-workflow" - echo "Then, execute 'make' again." - exit 1 -else - echo "GitHub Actions has been activated" + printf "\n[!] GitHub Actions MUST be activated." + case ${OSTYPE} in + "linux"*) + xdg-open https://github.com/${ACCOUNT}/lab0-c/actions > /dev/null 2>&1 + ;; + "darwin"*) + open https://github.com/${ACCOUNT}/lab0-c/actions + ;; + *) + echo "Please activate at https://github.com/${ACCOUNT}/lab0-c/actions" + ;; + esac + throw "Check this article: https://docs.github.com/en/actions/managing-workflow-runs/disabling-and-enabling-a-workflow\n\ + Then, execute 'make' again." fi +# 5. Install Git hooks +((CURRENT_STEP++)) +progress "$CURRENT_STEP" "$TOTAL_STEPS" + mkdir -p .git/hooks ln -sf ../../scripts/pre-commit.hook .git/hooks/pre-commit || exit 1 diff --git a/scripts/pre-push.hook b/scripts/pre-push.hook index ce8bb4b52..c1b457257 100755 --- a/scripts/pre-push.hook +++ b/scripts/pre-push.hook @@ -1,11 +1,17 @@ #!/usr/bin/env bash +# Ensure that the common script exists and is readable, then verify it has no +# syntax errors and defines the required function. +common_script="$(dirname "$0")/../../scripts/common.sh" +[ -r "$common_script" ] || { echo "[!] '$common_script' not found or not readable." >&2; exit 1; } +bash -n "$common_script" >/dev/null 2>&1 || { echo "[!] '$common_script' contains syntax errors." >&2; exit 1; } +source "$common_script" +declare -F set_colors >/dev/null 2>&1 || { echo "[!] '$common_script' does not define the required function." >&2; exit 1; } + +set_colors + protected_branch='master' current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') -RED='\033[0;31m' -GREEN='\033[1;32m' -YELLOW='\033[1;33m' -NC='\033[0m' # No Color # Validate repository # commit 50c5ac53d31adf6baac4f8d3db6b3ce2215fee40 diff --git a/scripts/prepare-commit-msg.hook b/scripts/prepare-commit-msg.hook index 5a097c558..207795e40 100755 --- a/scripts/prepare-commit-msg.hook +++ b/scripts/prepare-commit-msg.hook @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash COMMIT_MSG_FILE="$1"