Skip to content

Commit 57b75c0

Browse files
committed
Update last_modified_date on pre-commit
1 parent 2830a8d commit 57b75c0

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
#!/usr/bin/bash
1+
#!/usr/bin/env bash
2+
23
# Replace `last_modified_date` timestamp with current time
3-
# This script is used to update the `last_modified_date` timestamp in the frontmatter of all Markdown files in the current directory and subdirectories.
4+
# This script is used to update the `last_modified_date` timestamp
5+
# in the frontmatter of staged Markdown files.
6+
7+
# Get the list of staged Markdown files
8+
staged_files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.md$')
49

5-
# Loop through all Markdown files in the current directory and subdirectories
6-
find . -type f -name "*.md" | while read file; do
7-
# Check if the file contains a frontmatter block
8-
if grep -q "^---" "$file"; then
10+
# Loop through each staged Markdown file
11+
echo "$staged_files" | while read file; do
12+
# Check if the file exists and contains a frontmatter block
13+
if [[ -f "$file" ]] && grep -q "^---" "$file"; then
14+
echo "Updating last_modified_date in $file"
915
# Get the last modified timestamp of the file
1016
last_modified_timestamp=$(stat -c %Y "$file")
1117

@@ -19,5 +25,8 @@ find . -type f -name "*.md" | while read file; do
1925
# If last_modified_date does not exist, add it after the first frontmatter delimiter
2026
sed -i "/^---/a last_modified_date: $last_modified_datetime_utc" "$file"
2127
fi
28+
29+
# Stage the updated file
30+
git add "$file"
2231
fi
2332
done

0 commit comments

Comments
 (0)