set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
get_cargo_version() {
if [[ ! -f "Cargo.toml" ]]; then
echo "Error: Cargo.toml not found" >&2
exit 1
fi
cargo_version=$(grep -E '^\s*version\s*=\s*"[^"]*"' Cargo.toml | head -1 | sed -E 's/.*"([^"]+)".*/\1/')
if [[ -z "$cargo_version" ]]; then
echo "Error: Could not find version in Cargo.toml" >&2
exit 1
fi
echo "$cargo_version"
}
get_readme_version() {
if [[ ! -f "README.md" ]]; then
echo "Error: README.md not found" >&2
exit 1
fi
readme_version=$(grep -E 'pdfium\s*=\s*"[^"]*"' README.md | head -1 | sed -E 's/.*"([^"]+)".*/\1/')
if [[ -z "$readme_version" ]]; then
echo "Error: Could not find pdfium version in README.md" >&2
exit 1
fi
echo "$readme_version"
}
check_versions() {
cargo_version=$(get_cargo_version)
readme_version=$(get_readme_version)
echo " Cargo.toml version: $cargo_version"
echo " README.md version: $readme_version"
if [[ "$cargo_version" == "$readme_version" ]]; then
echo -e " ${GREEN}✓ Versions match!${NC}"
return 0
else
echo -e " ${RED}✗ Version mismatch detected!${NC}"
echo -e " ${YELLOW}Please update the pdfium version in README.md to match Cargo.toml${NC}"
echo -e " ${YELLOW}Expected: pdfium = \"$cargo_version\"${NC}"
echo -e " ${YELLOW}Found: pdfium = \"$readme_version\"${NC}"
return 1
fi
}
check_formatting() {
if ! cargo fmt --check > /dev/null 2>&1; then
echo -e " ${RED}✗ Code is not formatted!${NC}"
echo -e " ${YELLOW}Please run: cargo fmt${NC}"
return 1
fi
echo -e " ${GREEN}✓ Code is properly formatted${NC}"
return 0
}
check_clippy() {
if ! cargo clippy --all-targets --all-features -- -D warnings > /dev/null 2>&1; then
echo -e " ${RED}✗ Clippy found issues!${NC}"
echo -e " ${YELLOW}Please fix clippy warnings by running: cargo clippy --all-targets --all-features${NC}"
return 1
fi
echo -e " ${GREEN}✓ Clippy is happy${NC}"
return 0
}
check_tests() {
if ! cargo test --all-features > /dev/null 2>&1; then
echo -e " ${RED}✗ Tests failed!${NC}"
echo -e " ${YELLOW}Please fix failing tests by running: cargo test --all-features${NC}"
return 1
fi
echo -e " ${GREEN}✓ All tests passed${NC}"
return 0
}
check_docs() {
if ! RUSTDOCFLAGS="-D warnings" cargo doc --all-features --no-deps > /dev/null 2>&1; then
echo -e " ${RED}✗ Documentation build failed!${NC}"
echo -e " ${YELLOW}Please fix documentation issues by running: cargo doc --all-features --no-deps${NC}"
return 1
fi
echo -e " ${GREEN}✓ Documentation builds successfully${NC}"
return 0
}
echo
echo "Running pre-commit checks"
echo "========================="
echo
if ! git diff-files --quiet; then
echo -e "${RED}Error: You have unstaged changes in tracked files.${NC}"
echo -e "${YELLOW}Unstaged changes detected in:${NC}"
git diff-files --name-only | sed 's/^/ /'
echo ""
echo -e "${YELLOW}Please stage all changes before committing:${NC}"
echo " git add -u"
echo ""
exit 1
fi
failed=0
echo "1. Checking version consistency..."
if ! check_versions; then
failed=1
fi
echo
echo "2. Checking code formatting..."
if ! check_formatting; then
failed=1
fi
echo
echo "3. Running clippy..."
if ! check_clippy; then
failed=1
fi
echo
echo "4. Running tests..."
if ! check_tests; then
failed=1
fi
echo
echo "5. Checking documentation..."
if ! check_docs; then
failed=1
fi
echo
echo "============================="
if [[ $failed -eq 1 ]]; then
echo -e "${RED}Pre-commit checks failed!${NC}"
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$current_branch" == "main" ]; then
echo -e "${YELLOW}Please fix the issues above and try again.${NC}"
echo
exit 1
else
echo -e "${YELLOW}Ignoring issues because we are not on main (${current_branch}).${NC}"
echo
exit 0
fi
else
echo -e "${GREEN}All pre-commit checks passed!${NC}"
echo
exit 0
fi