|
1 | | -#!/bin/bash |
2 | | - |
3 | | -cargo +nightly fmt --check && |
4 | | - cargo +nightly check --tests --benches --examples --workspace --all-targets --all-features && |
5 | | - cargo +nightly doc --no-deps --bins --examples --workspace --all-features && |
6 | | - cargo +nightly machete && |
7 | | - cargo +stable build && |
8 | | - CARGO_INCREMENTAL=0 cargo +stable clippy --no-deps --tests --benches --examples --workspace --all-targets --all-features -- -D clippy::correctness -D clippy::suspicious -D clippy::complexity -D clippy::perf -D clippy::style -D clippy::pedantic && |
9 | | - cargo +stable test --doc --workspace && |
10 | | - cargo +stable test --tests --benches --examples --workspace --all-targets --all-features |
| 1 | +#!/usr/bin/env bash |
| 2 | +# Pre-commit verification script |
| 3 | +# Run all mandatory checks before committing changes. |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# ./contrib/dev-tools/git/hooks/pre-commit.sh |
| 7 | +# |
| 8 | +# Expected runtime: ~3 minutes on a modern developer machine. |
| 9 | +# AI agents: set a per-command timeout of at least 5 minutes before invoking this script. |
| 10 | +# |
| 11 | +# All steps must pass (exit 0) before committing. |
| 12 | + |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +# ============================================================================ |
| 16 | +# STEPS |
| 17 | +# ============================================================================ |
| 18 | +# Each step: "description|success_message|command" |
| 19 | + |
| 20 | +declare -a STEPS=( |
| 21 | + "Checking for unused dependencies (cargo machete)|No unused dependencies found|cargo machete" |
| 22 | + "Running all linters|All linters passed|linter all" |
| 23 | + "Running documentation tests|Documentation tests passed|cargo test --doc --workspace" |
| 24 | + "Running all tests|All tests passed|cargo test --tests --benches --examples --workspace --all-targets --all-features" |
| 25 | +) |
| 26 | + |
| 27 | +# ============================================================================ |
| 28 | +# HELPER FUNCTIONS |
| 29 | +# ============================================================================ |
| 30 | + |
| 31 | +format_time() { |
| 32 | + local total_seconds=$1 |
| 33 | + local minutes=$((total_seconds / 60)) |
| 34 | + local seconds=$((total_seconds % 60)) |
| 35 | + if [ "$minutes" -gt 0 ]; then |
| 36 | + echo "${minutes}m ${seconds}s" |
| 37 | + else |
| 38 | + echo "${seconds}s" |
| 39 | + fi |
| 40 | +} |
| 41 | + |
| 42 | +run_step() { |
| 43 | + local step_number=$1 |
| 44 | + local total_steps=$2 |
| 45 | + local description=$3 |
| 46 | + local success_message=$4 |
| 47 | + local command=$5 |
| 48 | + |
| 49 | + echo "[Step ${step_number}/${total_steps}] ${description}..." |
| 50 | + |
| 51 | + local step_start=$SECONDS |
| 52 | + local -a cmd_array |
| 53 | + read -ra cmd_array <<< "${command}" |
| 54 | + "${cmd_array[@]}" |
| 55 | + local step_elapsed=$((SECONDS - step_start)) |
| 56 | + |
| 57 | + echo "PASSED: ${success_message} ($(format_time "${step_elapsed}"))" |
| 58 | + echo |
| 59 | +} |
| 60 | + |
| 61 | +trap 'echo ""; echo "=========================================="; echo "FAILED: Pre-commit checks failed!"; echo "Fix the errors above before committing."; echo "=========================================="; exit 1' ERR |
| 62 | + |
| 63 | +# ============================================================================ |
| 64 | +# MAIN |
| 65 | +# ============================================================================ |
| 66 | + |
| 67 | +TOTAL_START=$SECONDS |
| 68 | +TOTAL_STEPS=${#STEPS[@]} |
| 69 | + |
| 70 | +echo "Running pre-commit checks..." |
| 71 | +echo |
| 72 | + |
| 73 | +for i in "${!STEPS[@]}"; do |
| 74 | + IFS='|' read -r description success_message command <<< "${STEPS[$i]}" |
| 75 | + run_step $((i + 1)) "${TOTAL_STEPS}" "${description}" "${success_message}" "${command}" |
| 76 | +done |
| 77 | + |
| 78 | +TOTAL_ELAPSED=$((SECONDS - TOTAL_START)) |
| 79 | +echo "==========================================" |
| 80 | +echo "SUCCESS: All pre-commit checks passed! ($(format_time "${TOTAL_ELAPSED}"))" |
| 81 | +echo "==========================================" |
| 82 | +echo |
| 83 | +echo "You can now safely stage and commit your changes." |
0 commit comments