-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathpost-commit
executable file
·27 lines (24 loc) · 921 Bytes
/
post-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
# Author: Tor Hveem
# Loop through each markdown file
for FILE in $(ls *md) ; do
# strip file extension
TITLE="${FILE%%.*}"
REDIS_KEY="post:$TITLE:log"
# get json log output with unix timestamp date
OUTPUT=$(exec git log --pretty=format:'{"commit": "%H", "author": "%an <%ae>", "timestamp": %at, "message": "%s"},' -- $FILE)
# Strip last ,
OUTPUT="${OUTPUT%%,}"
# Switch linesbreaks to , with goal to eventually make a list
OUTPUT=$(echo $OUTPUT | tr "\n" ",")
# Strip last ,
OUTPUT="${OUTPUT%%,}"
# Json list
OUTPUT="[$OUTPUT]"
# Feed the output into redis
echo "SET $REDIS_KEY '$OUTPUT'" | redis-cli > /dev/null
# Find first timestamp of post
TIMESTAMP=$(exec git log --pretty=format:"%ct" --reverse -- $FILE | head -1)
# Add the post to the sorted set of posts
echo "ZADD posts $TIMESTAMP $TITLE" | redis-cli > /dev/null
done