fosscat-site/.githooks/update-lastmod.sh

38 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Auto-update lastmod field in staged content files
# This script should never block a commit - errors are warnings only
update_lastmod() {
local file="$1"
# Generate RFC3339 timestamp with timezone (e.g., 2025-11-08T16:19:07-06:00)
local tz_offset=$(date +%z)
local tz_formatted="${tz_offset:0:3}:${tz_offset:3:2}" # Insert colon: -0600 -> -06:00
local now=$(date +%Y-%m-%dT%H:%M:%S)${tz_formatted}
# Only update if file has lastmod field
if grep -q "^lastmod:" "$file"; then
# Cross-platform sed -i (macOS vs Linux)
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "s/^lastmod:.*$/lastmod: $now/" "$file"
else
sed -i "s/^lastmod:.*$/lastmod: $now/" "$file"
fi
git add "$file"
echo " Updated lastmod in $file"
fi
}
# Get staged content files
staged_files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '^content/.*\.md$' || true)
if [ -z "$staged_files" ]; then
exit 0
fi
for file in $staged_files; do
if [ -f "$file" ]; then
update_lastmod "$file" || echo "Warning: Failed to update lastmod in $file"
fi
done