-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpre-commit
More file actions
66 lines (62 loc) · 2.61 KB
/
Copy pathpre-commit
File metadata and controls
66 lines (62 loc) · 2.61 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
59
60
61
62
63
64
65
66
#!/bin/bash
# Pre-commit Git hook. This currently just calls utilities/optimise_thumbnail.py
# which makes a smaller copy of the header image for all articles.
# To use: run `cp utilities/pre-commit .git/hooks`
CYAN="\e[36m"
RED="\e[31m"
GREEN="\e[92m"
CLEAR="\e[39m"
# Run optimise_thumbnail.py and print out files that need to be thumbnailised to a
# temporary file
# NOTE: This script runs through all articles. So if there are past articles without
# a thumbnail, a thumbnail will be created and the old article will be edited and
# staged too
run_optimise_thumbnail_script() {
# Get the root of the project
local root="$(git rev-parse --show-toplevel)"
# Create a temp file to write the python script output to
# -t flag is needed to ensure OSX usability, as recommended in
# https://tinyurl.com/ycx922hd
local temp=$(mktemp -p ${root} -t)
echo -e "${CYAN}Optimising thumbnails...${CLEAR}"
# Check if pipenv is a global or a user install in the your machine
if hash pipenv 2>/dev/null; then
pipenv run --three python3 "${root}/utilities/optimise_thumbnail.py" $root > $temp
else
python3 -m pipenv run --three "${root}/utilities/optimise_thumbnail.py" \
$root > $temp
fi
if [[ $? -gt 0 ]]; then
echo -e "${RED}There was a problem running the optimiser. Please try again.${CLEAR}"
exit 1
fi
echo -e "${GREEN}Thumbnail creation complete! Staging these new changes...\
${CLEAR}"
# Read each line from the temp file to stage the thumbnailised files to commit
while IFS='' read -r line || [[ -n "$line" ]]; do
echo -e "${CYAN}Adding to commit:${CLEAR} $line"
git add $line
done < $temp
# Delete the temp file
rm $temp
# Pause for a bit so that you can see what's goin' on
polite_notice="${CYAN}The bottom blurb in your commit message window may not \
say that some of the files above are staged. They actually are. Why? \
Don't worry about it${CLEAR}"
echo -e $polite_notice
sleep 4
}
### Script starts here ###
# AWK command to check if:
# 1. A staged file is an article; and
# 2. We aren't removing the file, but adding/modifying it
# Our function parameter will be a line from git diff --cached --name-status
this_is_the_question="$(git diff --cached --name-status \
| awk '$1 != "D" { print $2 }' \
| awk '/^_posts\/articles/ { print $1 }')"
# If the above test passes, run the function above
if [[ ! -z $this_is_the_question ]]; then
echo $this_is_the_question
echo -e "${CYAN}Running thumbnail creation script...${CLEAR}"
run_optimise_thumbnail_script
fi