Skip to content

Commit dd390ac

Browse files
authored
Merge pull request #395 from smallstep/carl/date-hook
Pre-commit date hook
2 parents 7afcc0b + a5542c0 commit dd390ac

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

.githooks/pre-commit

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
# This pre-commit hook updates (or adds) the updated_at frontmatter in .mdx files with the current date.
3+
#
4+
# To install it, run the following from the top level of the repo:
5+
#
6+
# mkdir -p .git/hooks
7+
# cp .githooks/pre-commit .git/hooks/pre-commit
8+
# chmod +x .git/hooks/pre-commit
9+
10+
# Get the current date in "Month Day, Year" format (e.g., March 23, 2025)
11+
CURRENT_DATE=$(date -u +"%B %d, %Y")
12+
13+
# Function to update frontmatter
14+
update_frontmatter() {
15+
local file=$1
16+
17+
# Ensure the file contains frontmatter
18+
if [[ $(head -n 1 "$file") == "---" ]]; then
19+
if grep -q "^updated_at:" "$file"; then
20+
# Update the existing "updated_at" field
21+
sed -i "" -E "s/^updated_at:.*/updated_at: $CURRENT_DATE/" "$file"
22+
else
23+
# Insert "updated_at" on the second line of the file
24+
sed -i "" "2i\\
25+
updated_at: $CURRENT_DATE
26+
" "$file"
27+
fi
28+
fi
29+
}
30+
31+
# Find all staged Markdown files
32+
for file in $(git diff --cached --name-only -- '*.mdx'); do
33+
if [[ -f "$file" ]]; then
34+
update_frontmatter "$file"
35+
# Re-add the file to staging
36+
git add "$file"
37+
fi
38+
done
39+
40+

0 commit comments

Comments
 (0)