morpheus-brain/scripts/ci-pre-push.sh

69 lines
No EOL
2.4 KiB
Bash

#!/bin/bash
# ci-pre-push.sh - Validate markdown files before push
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
echo -e "${YELLOW}Running pre-push checks...${NC}"
# Check if any markdown files changed
if [[ $(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(md|markdown)$' | wc -l) -eq 0 ]]; then
echo "No markdown files staged for commit."
exit 0
fi
ERRORS=0
# Validate each staged markdown file
while IFS= read -r file; do
echo "Checking $file..."
if [[ -f "$file" ]]; then
# Check front-matter existence (--- at start)
if ! head -n 1 "$file" | grep -q '^---$'; then
echo -e "${RED}ERROR: $file is missing front-matter (should start with ---)${NC}"
((ERRORS++))
else
# Extract front-matter block (lines between first and second ---)
front_matter=$(sed -n '2,/^---$/p' "$file" | head -n -1)
# Check for required fields
if ! echo "$front_matter" | grep -q '^title:'; then
echo -e "${RED}ERROR: $file missing title in front-matter${NC}"
((ERRORS++))
fi
if ! echo "$front_matter" | grep -q '^date:'; then
echo -e "${RED}ERROR: $file missing date in front-matter${NC}"
((ERRORS++))
fi
# Optional: check date format
date_line=$(echo "$front_matter" | grep '^date:' | sed 's/date: //')
if [[ -n "$date_line" && ! "$date_line" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
echo -e "${YELLOW}WARNING: $file date format not YYYY-MM-DD: $date_line${NC}"
fi
fi
# Spell check if aspell is available
if command -v aspell >/dev/null 2>&1; then
# Strip front-matter and code blocks, then spell check
content=$(sed '/^---$/,/^---$/d' "$file" | sed '/```.*/,/```/d')
if echo "$content" | aspell list | grep -v '^$' | head -n 5 | grep -q .; then
echo -e "${YELLOW}WARNING: $file has potential spelling errors (see above)${NC}"
# Don't fail on spelling
fi
fi
# Check for TODO/FIXME comments (optional warning)
if grep -n -i 'TODO\|FIXME' "$file" | head -n 5 | grep -q .; then
echo -e "${YELLOW}INFO: $file contains TODO/FIXME comments${NC}"
fi
fi
done < <(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(md|markdown)$')
if [[ $ERRORS -gt 0 ]]; then
echo -e "${RED}Pre-push checks failed. Fix errors above before committing.${NC}"
exit 1
else
echo -e "${GREEN}All pre-push checks passed.${NC}"
fi