Skip to content

Commit dcb96cb

Browse files
committed
modify git hook to be a small wrapper
1 parent 5c5058b commit dcb96cb

File tree

2 files changed

+59
-57
lines changed

2 files changed

+59
-57
lines changed

tools/hooks/commit-msg

+1-57
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,2 @@
11
#!/bin/sh
2-
#
3-
# Verifies that the ms.date in the header is within the past 5 days
4-
# or is in the future.
5-
#
6-
# You can say "nofreshen" anywhere in the commit message to skip processing.
7-
8-
# Redirect output to stderr.
9-
exec 1>&2
10-
11-
# If "nofreshen" appears in the message, skip further processing
12-
NOFRESHEN_MSG="nofreshen"
13-
if grep -Fiq "nofreshen" "$1"; then
14-
exit 0
15-
fi
16-
17-
ALLOW_LAST_N_DAYS=5
18-
if [ "$(uname)" == "Darwin" ]; then
19-
# POSIX `date` isn't as featureful as GNU `date`
20-
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"))')
21-
else
22-
FLOOR_DATE=$(date --date="$ALLOW_LAST_N_DAYS days ago" +%s)
23-
fi
24-
25-
if [ -t 1 ]; then
26-
RESTORE="\033[0m"
27-
RED="\033[00;31m"
28-
YELLOW="\033[00;33m"
29-
else
30-
RESTORE=""
31-
RED=""
32-
YELLOW=""
33-
fi
34-
35-
REJECT=0
36-
37-
for i in $(git diff --cached --name-only); do
38-
FILE_DATE=$(git show :$i | grep '^ms.date:' | awk '{print $2}')
39-
if [[ -n "$FILE_DATE" ]]; then
40-
if [ "$(uname)" == "Darwin" ]; then
41-
# POSIX `date` isn't as featureful as GNU `date`
42-
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"))')
43-
else
44-
FILE_DATE_AS_EPOCH=$(date --date="$FILE_DATE" +%s)
45-
fi
46-
let DELTA_DATES=$FILE_DATE_AS_EPOCH-$FLOOR_DATE
47-
if (( $DELTA_DATES < 0 )); then
48-
echo -e "${YELLOW}$i has an ms.date of $FILE_DATE${RESTORE}"
49-
REJECT=1
50-
fi
51-
fi
52-
done
53-
54-
if (( $REJECT == 1 )); then
55-
echo -e "${RED}commit rejected. add '${NOFRESHEN_MSG}' to the message to skip validation${RESTORE}"
56-
fi
57-
58-
exit $REJECT
2+
source $(git rev-parse --show-toplevel)/tools/scripts/commit-msg.sh

tools/scripts/commit-msg.sh

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/sh
2+
#
3+
# Verifies that the ms.date in the header is within the past 5 days
4+
# or is in the future.
5+
#
6+
# You can say "nofreshen" anywhere in the commit message to skip processing.
7+
8+
# Redirect output to stderr.
9+
exec 1>&2
10+
11+
# If "nofreshen" appears in the message, skip further processing
12+
NOFRESHEN_MSG="nofreshen"
13+
if grep -Fiq "nofreshen" "$1"; then
14+
exit 0
15+
fi
16+
17+
ALLOW_LAST_N_DAYS=5
18+
if [ "$(uname)" == "Darwin" ]; then
19+
# POSIX `date` isn't as featureful as GNU `date`
20+
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"))')
21+
else
22+
FLOOR_DATE=$(date --date="$ALLOW_LAST_N_DAYS days ago" +%s)
23+
fi
24+
25+
if [ -t 1 ]; then
26+
RESTORE="\033[0m"
27+
RED="\033[00;31m"
28+
YELLOW="\033[00;33m"
29+
else
30+
RESTORE=""
31+
RED=""
32+
YELLOW=""
33+
fi
34+
35+
REJECT=0
36+
37+
for i in $(git diff --cached --name-only); do
38+
FILE_DATE=$(git show :$i | grep '^ms.date:' | awk '{print $2}')
39+
if [[ -n "$FILE_DATE" ]]; then
40+
if [ "$(uname)" == "Darwin" ]; then
41+
# POSIX `date` isn't as featureful as GNU `date`
42+
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"))')
43+
else
44+
FILE_DATE_AS_EPOCH=$(date --date="$FILE_DATE" +%s)
45+
fi
46+
let DELTA_DATES=$FILE_DATE_AS_EPOCH-$FLOOR_DATE
47+
if (( $DELTA_DATES < 0 )); then
48+
echo -e "${YELLOW}$i has an ms.date of $FILE_DATE${RESTORE}"
49+
REJECT=1
50+
fi
51+
fi
52+
done
53+
54+
if (( $REJECT == 1 )); then
55+
echo -e "${RED}commit rejected. add '${NOFRESHEN_MSG}' to the message to skip validation${RESTORE}"
56+
fi
57+
58+
exit $REJECT

0 commit comments

Comments
 (0)