-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·64 lines (57 loc) · 1.79 KB
/
pre-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
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
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
IFS=$'\n\t'
# functions
function elementIn () {
local elem="$1" # Save first argument in a variable
shift # Shift all arguments to the left (original $1 gets lost)
local arr=("$@") # Rebuild the array with rest of arguments
if printf '%s\n' "${arr[@]}" | grep -q --line-regexp "${elem}"; then
return 0
fi
return 1
}
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi
# If there are whitespace errors, print the offending file names and fail.
(
exec git diff-index --check --cached $against --
) 1>&2
IPYNB_FILES=()
while IFS='' read -r line; do
IPYNB_FILES+=( "$line" );
# finding all staged *.ipynb files added, copied, modified or renamed since last commit
done < <(git diff-index --name-only --cached --diff-filter=ACMR "${against}" -- | grep -i \.ipynb$ )
for FILE in "${IPYNB_FILES[@]}"; do
echo "Processing file: '$FILE'"
# you may need to provide the full path to 'jupyter' executable
# if it is not in the path
jupyter nbconvert --ClearOutputPreprocessor.enabled=True --ClearMetadataPreprocessor.enabled=True --inplace "$FILE"
# echo "Current EXIT_CODE value: $?"
done
MODIFIED_FILES=()
while IFS='' read -r line; do
MODIFIED_FILES+=( "$line" );
# list all modified files
done < <(git ls-files --modified --exclude-standard)
AMOUNT=0
for mfile in "${MODIFIED_FILES[@]}"; do
if elementIn "$mfile" "${IPYNB_FILES[@]}"; then
echo "'$mfile' has been modified by pre-commit hook!"
AMOUNT=$((AMOUNT+1))
fi
done
if [[ $AMOUNT -eq 0 ]]; then
echo "No ipynb files were modified!"
exit 0
else
echo "Pre-commit hook modified $AMOUNT ipynb files! You must be recommit \`git commit --amend\` maybe?"
exit 1
fi