Skip to content

Commit d91e2e1

Browse files
committed
update pre-commit and update hooks
1 parent 7f56e86 commit d91e2e1

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

pre-commit

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,28 @@
77

88
if git rev-parse --verify HEAD >/dev/null 2>&1
99
then
10-
against=HEAD
10+
against=HEAD
1111
else
12-
# Initial commit: diff against an empty tree object
13-
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
12+
# Initial commit: diff against an empty tree object
13+
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
1414
fi
1515

1616
# If you want to allow non-ascii filenames set this variable to true.
1717
allownonascii=$(git config hooks.allownonascii)
18+
sizelimit=52428800
1819

1920
# Cross platform projects tend to avoid non-ascii filenames; prevent
2021
# them from being added to the repository. We exploit the fact that the
2122
# printable range starts at the space character and ends with tilde.
2223
if [ "$allownonascii" != "true" ] &&
23-
# Note that the use of brackets around a tr range is ok here, (it's
24-
# even required, for portability to Solaris 10's /usr/bin/tr), since
25-
# the square bracket bytes happen to fall in the designated range.
26-
test "$(git diff --cached --name-only --diff-filter=A -z $against |
27-
LC_ALL=C tr -d '[ -~]\0')"
28-
then
29-
echo "Attempt to add a non-ascii file name. Please choose a different name."
30-
exit 1
24+
# Note that the use of brackets around a tr range is ok here, (it's
25+
# even required, for portability to Solaris 10's /usr/bin/tr), since
26+
# the square bracket bytes happen to fall in the designated range.
27+
test "$(git diff --cached --name-only --diff-filter=A -z $against |
28+
LC_ALL=C tr -d '[ -~]\0')"
29+
then
30+
echo "Attempt to add a non-ascii file name. Please choose a different name."
31+
exit 1
3132
fi
3233

3334
# this checks that no unimportant whitespace changes are committed
@@ -46,4 +47,15 @@ if [ "${ifzeros}" != "" ]; then
4647
exit 1
4748
fi
4849

50+
# check the filesize of each file stays below sizelimit
51+
# list new or modified files newfiles=$(git diff --staged --name-only )
52+
currdir="$(pwd)"
53+
for file in $(git diff --cached --name-only | sort | uniq); do
54+
filesize=$(du -b ${currdir}/${file} | awk '{print $1}')
55+
if [ "${size}" -ge "${sizelimit}" ]; then
56+
echo "hard size limit (${sizelimit}) exceeded: ${file} (${size} bytes)"
57+
exit 1
58+
fi
59+
done
60+
4961
echo "All fine, thank you."

serverhooks/update

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,33 @@
1717
refname=$1 # name of ref that is being updated
1818
oldrev=$2 # old object name that ref pointed to before
1919
newrev=$3 # new object name that ref should point to
20+
nullsha="0000000000000000000000000000000000000000"
21+
emptysha=$(git hash-object -t tree /dev/null) # SHA1: "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
22+
2023
echo "Enforcing policies for revision ${refname}:"
2124

2225
# -------------------------------
2326
# Get the list of all the commits
2427
# -------------------------------
2528

2629
# exit if a branch is about to be deleted
27-
if [ "${newrev}" == '0000000000000000000000000000000000000000' ]; then
30+
if [ "${newrev}" == "${nullsha}" ]; then
2831
exit 0
2932
fi
3033

31-
checksince="--since=2020-04-27"
34+
# Set oldrev properly if the is branch created
35+
if [ "${oldrev}" = "${nullsha}" ]; then
36+
oldrev=$emptysha
37+
fi
38+
39+
checkmessagesince="--since=2020-04-27"
40+
checksizesince="--since=2020-04-13"
41+
sizelimit=52428800
3242

3343
# branch labels are updated *after* the hook ran successfully,
34-
# so get all commits reachable from newref (that are newer than a certain date),
44+
# so get all commits reachable from newrev (that are newer than a certain date),
3545
# that are not reachable from any yet known ref (branch) labels
36-
span=$(git rev-list ${newrev} ${checksince} --not --all)
46+
span=$(git rev-list ${newrev} ${checkmessagesince} --not --all)
3747

3848
# ----------------------------------------
3949
# Iterate over all new commits in the push
@@ -54,6 +64,23 @@ for sha1 in ${span}; do
5464
continue
5565
fi
5666

67+
# check the filesize of each file stays below sizelimit
68+
# list new or modified files
69+
newfiles=$(git show --pretty="format:" --name-only --diff-filter=ACMRT ${sha1} )
70+
71+
for file in ${newfiles}; do
72+
size=$(git cat-file -s "${sha1}:${file}")
73+
if [[ -z $size ]]; then
74+
size=0;
75+
fi
76+
77+
if [ "${size}" -gt "${sizelimit}" ]
78+
then
79+
echo "${sha1} hard size limit (${sizelimit}) exceeded: ${file} (${size} bytes)"
80+
refuse=true
81+
fi
82+
done
83+
5784
# check commit message
5885
commitmessage=$(git log --format=%B -n 1 ${sha1})
5986
commitfirstline=$(echo "${commitmessage}" | head -n 1)

0 commit comments

Comments
 (0)