-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·38 lines (36 loc) · 1.11 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
#!/usr/bin/env bash
set +eou pipefail
size_limit=$((5 * 2**20))
repo_root=$(git rev-parse --show-toplevel)
pushd "$repo_root" 2>/dev/null > /dev/null || true
empty=$(git hash-object -t tree /dev/null)
if git rev-parse --verify HEAD > /dev/null 2>&1
then
against=HEAD
else
against="$empty"
fi
IFS='
'
tracked=$(git lfs ls-files --name-only)
hasLargeFile=false
for file in $(git diff-index --cached --name-only "$against"); do
for tracked_file in $tracked; do
if [ "$file" == "$tracked_file" ]; then
continue 2
fi
done
# shellcheck disable=SC2012
file_size=$( ([ ! -f "$file" ] && echo 0) || ls -la "$file" | awk '{ print $5 }' )
if [ "$file_size" -gt "$size_limit" ]; then
echo File "$file" is "$(( file_size / 10**6 ))"MB, which is larger than our configured limit of "$(( size_limit / 10**6 ))"MB
hasLargeFile=true
fi
done
if $hasLargeFile; then
echo Commit too large, did you add a binary file? For image assets, consider git-lfs.
popd "$repo_root" 2>/dev/null > /dev/null || true
exit 1
fi
popd "$repo_root" 2>/dev/null > /dev/null || true
gitleaks protect --staged --verbose --no-banner --no-color 2>/dev/null