|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright (c) Brandon Pacewic |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" |
| 9 | +ROOT="$DIR/../" |
| 10 | +cd $ROOT |
| 11 | + |
| 12 | +RED='\033[0;31m' |
| 13 | +GREEN='\033[0;32m' |
| 14 | +NC='\033[0m' |
| 15 | + |
| 16 | +FAILED=0 |
| 17 | +FAST=0 |
| 18 | + |
| 19 | +while [[ $# -gt 0 ]]; do |
| 20 | + case $1 in |
| 21 | + --fast) |
| 22 | + FAST=1 |
| 23 | + shift |
| 24 | + ;; |
| 25 | + *) |
| 26 | + echo "Unknown option: $1" |
| 27 | + echo "Usage: $0 [--fast]" |
| 28 | + echo " --fast skip build and tests (format + file checks only)" |
| 29 | + exit 1 |
| 30 | + ;; |
| 31 | + esac |
| 32 | +done |
| 33 | + |
| 34 | +function run_check() { |
| 35 | + local name="$1" |
| 36 | + local cmd="$2" |
| 37 | + |
| 38 | + local name_len=${#name} |
| 39 | + local dots_len=$((46 - name_len)) |
| 40 | + local dots=$(printf '%*s' "$dots_len" | tr ' ' '.') |
| 41 | + |
| 42 | + printf "%s%s" "$name" "$dots" |
| 43 | + |
| 44 | + set +e |
| 45 | + log=$(eval "$cmd" 2>&1) |
| 46 | + result=$? |
| 47 | + set -e |
| 48 | + |
| 49 | + if [[ $result -eq 0 ]]; then |
| 50 | + echo -e "[${GREEN}ok${NC}]" |
| 51 | + else |
| 52 | + echo -e "[${RED}FAIL${NC}]" |
| 53 | + echo "$log" |
| 54 | + FAILED=1 |
| 55 | + fi |
| 56 | +} |
| 57 | + |
| 58 | +function check_large_files() { |
| 59 | + local limit=$((250 * 1024)) |
| 60 | + local found=0 |
| 61 | + while IFS= read -r file; do |
| 62 | + local size |
| 63 | + size=$(wc -c < "$file") |
| 64 | + if [[ "$size" -gt "$limit" ]]; then |
| 65 | + echo "Large file ($(( size / 1024 ))KB): $file" |
| 66 | + found=1 |
| 67 | + fi |
| 68 | + done < <(git ls-files) |
| 69 | + return "$found" |
| 70 | +} |
| 71 | + |
| 72 | +run_check "check_default_branch" "[[ \$(git rev-parse --abbrev-ref HEAD) != mega ]]" |
| 73 | +run_check "clang_format" "git ls-files '*.cpp' '*.h' '*.hpp' | xargs clang-format --dry-run --Werror" |
| 74 | +run_check "check_large_files" "check_large_files" |
| 75 | +run_check "check_merge_conflicts" "! git ls-files | xargs grep -lP '^(<{7}|={7}|>{7})' 2>/dev/null | grep -q ." |
| 76 | + |
| 77 | +if [[ "$FAST" -eq 0 ]]; then |
| 78 | + run_check "build" \ |
| 79 | + "cmake -B build -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Debug && cmake --build build -j\$(nproc)" |
| 80 | + run_check "tests" "ctest --test-dir build --output-on-failure" |
| 81 | +fi |
| 82 | + |
| 83 | +echo "" |
| 84 | +if [[ $FAILED -eq 1 ]]; then |
| 85 | + echo -e "${RED}Lint failed${NC}" |
| 86 | + exit 1 |
| 87 | +else |
| 88 | + echo -e "${GREEN}All checks passed${NC}" |
| 89 | +fi |
0 commit comments