-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·117 lines (98 loc) · 3.35 KB
/
pre-commit
File metadata and controls
executable file
·117 lines (98 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright the Vortex contributors
#
# Git Pre-Commit Hook for Vortex
# Validates commits to ensure code formatting and REUSE compliance.
#
# If you want to use this hook, simply copy and paste the file into a new file
# `.git/hooks/pre-commit` and everything should work correctly. If you are in the project root, you
# can use these command:
#
# ```sh
# cp scripts/pre-commit .git/hooks/
# ```
set -Euo pipefail
# Configuration.
MAIN_BRANCH="develop" # The main branch name for vortex.
# Color codes for terminal output.
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
RESET='\033[0m'
# Output helper functions.
print_error() {
echo -e "${RED}$1${RESET}"
}
print_success() {
echo -e "${GREEN}$1${RESET}"
}
print_warning() {
echo -e "${YELLOW}$1${RESET}"
}
print_info() {
echo -e "${BLUE}$1${RESET}"
}
print_header() {
echo -e "\n${BOLD}$1${RESET}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
}
# Navigate to repository root to ensure consistent execution.
ROOT_DIR="$(git rev-parse --show-toplevel)"
cd "$ROOT_DIR"
# ============================================================================
# CHECK: Code Formatting
# ============================================================================
# Verify that all Rust code and TOML files follow the project's formatting standards.
# Consistent formatting improves readability and reduces merge conflicts.
print_header "🎨 Checking code formatting"
if ! command -v cargo &> /dev/null; then
print_error "❌ Cargo not found in PATH"
exit 1
fi
# Check if Rust code needs formatting.
FMT_OUTPUT=$(cargo +nightly fmt --all -- --check 2>&1)
FMT_EXIT_CODE=$?
if [ $FMT_EXIT_CODE -eq 0 ]; then
print_success "✅ Rust code formatting looks good"
else
print_error "❌ Rust code needs formatting"
echo
echo "$FMT_OUTPUT"
echo
print_info "💡 Fix with: cargo +nightly fmt"
echo
print_warning "⚠️ To skip checks: git commit --no-verify"
exit 1
fi
# Check if TOML files need formatting.
if ! command -v taplo &> /dev/null; then
print_warning "⚠️ taplo not found - skipping TOML formatting check."
echo " Install from: https://github.com/tamasfe/taplo"
else
TAPLO_OUTPUT=$(taplo fmt --check 2>&1)
TAPLO_EXIT_CODE=$?
if [ $TAPLO_EXIT_CODE -eq 0 ]; then
print_success "✅ TOML formatting looks good"
else
print_error "❌ TOML files need formatting"
echo
echo "$TAPLO_OUTPUT"
echo
print_info "💡 Fix with: taplo fmt"
echo
print_warning "⚠️ To skip checks: git commit --no-verify"
exit 1
fi
fi
# ============================================================================
# Success
# ============================================================================
echo
print_success "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
print_success "🎉 All pre-commit checks passed! Proceeding with push..."
print_success "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo
exit 0