Repository navigation
Expand file tree
/
Copy pathcommit-msg.sh
More file actions
executable file
·58 lines (50 loc) · 1.67 KB
/
Copy pathcommit-msg.sh
File metadata and controls
executable file
·58 lines (50 loc) · 1.67 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/sh
#
# Verifies that the ms.date in the header is within the past 5 days
# or is in the future.
#
# You can say "nofreshen" anywhere in the commit message to skip processing.
# Redirect output to stderr.
exec 1>&2
# If "nofreshen" appears in the message, skip further processing
NOFRESHEN_MSG="nofreshen"
if grep -Fiq "nofreshen" "$1"; then
exit 0
fi
ALLOW_LAST_N_DAYS=5
if [ "$(uname)" == "Darwin" ]; then
# POSIX `date` isn't as featureful as GNU `date`
FLOOR_DATE=$(ALLOW_LAST_N_DAYS=$ALLOW_LAST_N_DAYS python -c 'from datetime import datetime, timedelta; import os; print((datetime.now() - timedelta(int(os.environ["ALLOW_LAST_N_DAYS"]))).strftime("%s"))')
else
FLOOR_DATE=$(date --date="$ALLOW_LAST_N_DAYS days ago" +%s)
fi
if [ -t 1 ]; then
RESTORE="\033[0m"
RED="\033[00;31m"
YELLOW="\033[00;33m"
else
RESTORE=""
RED=""
YELLOW=""
fi
REJECT=0
for i in $(git diff --cached --name-only); do
FILE_DATE=$(git show :$i | grep '^ms.date:' | awk '{print $2}')
if [[ -n "$FILE_DATE" ]]; then
if [ "$(uname)" == "Darwin" ]; then
# POSIX `date` isn't as featureful as GNU `date`
FILE_DATE_AS_EPOCH=$(FILE_DATE=$FILE_DATE python -c 'from datetime import datetime; import os; print(datetime.strptime(os.environ["FILE_DATE"], "%m/%d/%Y").strftime("%s"))')
else
FILE_DATE_AS_EPOCH=$(date --date="$FILE_DATE" +%s)
fi
let DELTA_DATES=$FILE_DATE_AS_EPOCH-$FLOOR_DATE
if (( $DELTA_DATES < 0 )); then
echo -e "${YELLOW}$i has an ms.date of $FILE_DATE${RESTORE}"
REJECT=1
fi
fi
done
if (( $REJECT == 1 )); then
echo -e "${RED}commit rejected. add '${NOFRESHEN_MSG}' to the message to skip validation${RESTORE}"
fi
exit $REJECT