forked from patsevanton/git-server-pre-receive-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-large-files.sh
47 lines (38 loc) · 1.4 KB
/
check-large-files.sh
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
#!/bin/bash -eu
# Fork hook from https://github.com/github/platform-samples/tree/master/pre-receive-hooks
set -o pipefail
nullsha="0000000000000000000000000000000000000000"
maxsize=1
maxbytes=$(( $maxsize * 1024 * 1024 ))
status=0
# Read stdin for ref information
while read oldref newref refname; do
# Skip branch deletions
if [ "$newref" = "$nullsha" ]; then
continue
fi
# Set oldref to HEAD if this is branch creation
if [ "$oldref" = "$nullsha" ]; then
oldref="HEAD"
fi
# Find large objects
for file in $(git rev-list --objects ${oldref}..${newref} | \
git cat-file --batch-check='%(objectname) %(objecttype) %(objectsize) %(rest)' | \
awk -v maxbytes="$maxbytes" '$3 > maxbytes { print $4 }'); do
# Display error header if this is the first offending file
if [ "$status" -eq "0" ]; then
status=1
echo ""
echo "-------------------------------------------------------------------------"
echo "Your push was rejected because it contains files larger than $maxsize MB."
echo "Please discuss with the infra team the best place to store these files."
echo "You might want to consider using https://git-lfs.github.com/ instead."
echo "-------------------------------------------------------------------------"
echo
echo "Offending files:"
fi
echo " - $file"
done
done
if [ "$status" -ne "0" ]; then echo; fi
exit $status