#!/usr/bin/env bash # Pre-commit hook: validate README.md YAML frontmatter for HuggingFace dataset card. # Ensures required fields are present and non-empty. set -euo pipefail README="README.md" if [ ! -f "$README" ]; then echo "Error: $README not found" exit 1 fi # Extract YAML frontmatter (between opening and closing ---) frontmatter=$(awk '/^---$/{n++; next} n==1{print} n==2{exit}' "$README") if [ -z "$frontmatter" ]; then echo "Error: $README has no YAML frontmatter (must start with ---)" exit 1 fi # Required fields per HuggingFace dataset card spec required_fields=( "license" "task_categories" "size_categories" ) errors=0 for field in "${required_fields[@]}"; do if ! echo "$frontmatter" | grep -q "^${field}:"; then echo "Error: $README missing required field '$field'" errors=$((errors + 1)) else # Check the value isn't empty — either inline or as a YAML list on the next line inline_value=$(echo "$frontmatter" | grep "^${field}:" | sed "s/^${field}://" | sed 's/^[[:space:]]*//') if [ -z "$inline_value" ]; then # Value might be a multi-line list; check the next line starts with - if ! echo "$frontmatter" | grep -A1 "^${field}:" | tail -1 | grep -q '^- '; then echo "Error: $README field '$field' is empty" errors=$((errors + 1)) fi fi fi done if [ "$errors" -gt 0 ]; then echo "" echo "See https://huggingface.co/docs/hub/datasets-cards#yaml-metadata" exit 1 fi