forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify-file-sizes.sh
executable file
·70 lines (62 loc) · 2.39 KB
/
verify-file-sizes.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env bash
# Copyright 2023 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
cd "${KUBE_ROOT}"
# Files larger than 1MB need to be allowed explicitly.
maxsize=$((1 * 1024 * 1024))
# Sorted list of those exceptions.
allowlist=(
staging/src/k8s.io/kubectl/images/kubectl-logo-full.png
)
# Files larger than 1MB get reported and verification fails, unless the file is
# allowed to be larger. Any output or a non-zero exit status indicate a
# failure.
largefiles () {
# --eol adds information that allows detecting binary files:
# i/-text w/-text attr/text=auto eol=lf test/images/sample-device-plugin/sampledeviceplugin
#
# shellcheck disable=SC2034 # Some variables unused and only present to capture the output field.
git ls-files -cm --exclude-standard ':!:vendor/*' --eol | while read -r index tree attr eol file; do
case "$tree" in
w/-text)
# Only binary files have a size limit.
size="$(wc -c < "$file")"
if [ "${size}" -gt "$maxsize" ] &&
! kube::util::array_contains "$file" "${allowlist[@]}"; then
echo "$file is too large ($size bytes)"
fi
;;
w/|w/lf|w/crlf|w/mixed|w/none)
# Other files are okay.
;;
*)
echo "unexpected 'git ls-files --eol' output for $file: $tree"
;;
esac
done
}
if ! result="$(largefiles 2>&1)" || [ "${result}" != "" ]; then
# Help "make verify" and Prow identify the failure message through the "ERROR" prefix.
sed -e 's/^/ERROR: /' <<EOF
$result
Large binary files should not be committed to git. Exceptions need to be listed in
${BASH_SOURCE[0]}.
EOF
exit 1
fi