Skip to content

Commit fae500d

Browse files
committed
more file checks
Signed-off-by: John Seekins <john.seekins@spoileralert.com>
1 parent fc13d15 commit fae500d

6 files changed

Lines changed: 77 additions & 16 deletions

File tree

.lefthook.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ pre-commit:
5656
- name: check for merge conflicts
5757
run: tools/check-merge-conflicts.sh {staged_files}
5858

59-
- name: check for file format
60-
run: tools/check-file-format.sh {staged_files}
59+
- name: check for bad file details
60+
run: tools/check-file-details.sh {staged_files}
61+
env:
62+
- FILE_SIZE: 107374182400
63+
stage_fixed: true
6164

6265
output:
6366
- success

.pre-commit-config.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,8 @@ repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
55
rev: v6.0.0
66
hooks:
7-
- id: check-json
8-
- id: trailing-whitespace
97
- id: end-of-file-fixer
108
- id: check-added-large-files
119
args:
1210
- "--maxkb=5000"
13-
- id: check-merge-conflict
1411
- id: check-symlinks
15-
16-
- repo: https://github.com/igorshubovych/markdownlint-cli
17-
rev: v0.45.0
18-
hooks:
19-
- id: markdownlint

main.py

100644100755
File mode changed.

tools/check-file-details.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env bash
2+
3+
set -eou pipefail
4+
5+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
6+
pushd "${SCRIPT_DIR}" > /dev/null || exit 1
7+
pushd "$(git rev-parse --show-toplevel)" > /dev/null || exit 1
8+
9+
. tools/.library.sh
10+
11+
if [[ $# -eq 0 ]]; then
12+
FILES=$(git_files | xargs)
13+
else
14+
FILES=$*
15+
fi
16+
SHEBANG_FIXED=()
17+
WHITESPACE_TO_FIX=()
18+
FILE_SIZE=${FILE_SIZE:-5000}
19+
TOO_BIG=()
20+
exit_code=0
21+
22+
for fn in ${FILES}; do
23+
set +e
24+
shebang=$(grep -l "^#!/" "${fn}")
25+
if [[ -n "${shebang}" && ! -x "${fn}" ]]; then
26+
chmod +x "${fn}"
27+
SHEBANG_FIXED+=("${fn}")
28+
exit_code=1
29+
fi
30+
set +e
31+
# grep exits non-0 when it doesn't find anything
32+
whitespace=$(grep -l ' +$' "${fn}")
33+
set -e
34+
if [[ -n "${whitespace}" ]]; then
35+
WHITESPACE_TO_FIX+=("${whitespace}")
36+
exit_code=1
37+
fi
38+
size=$(stat --printf="%s" "${fn}")
39+
if [[ "${size}" -gt "${FILE_SIZE}" ]]; then
40+
TOO_BIG+=("${fn}: ${size}")
41+
exit_code=1
42+
fi
43+
done
44+
45+
if [[ "${#SHEBANG_FIXED[@]}" -gt 0 ]]; then
46+
echo "Fixed executable bit on:"
47+
printf '\t%s\n' "${SHEBANG_FIXED[@]}"
48+
fi
49+
if [[ "${#WHITESPACE_TO_FIX[@]}" -gt 0 ]]; then
50+
echo "Should clean whitespace on:"
51+
printf '\t%s\n' "${WHITESPACE_TO_FIX[@]}"
52+
fi
53+
54+
if [[ "${#TOO_BIG[@]}" -gt 0 ]]; then
55+
echo "Files larger than ${FILE_SIZE}:"
56+
printf '\t%s\n' "${TOO_BIG[@]}"
57+
fi
58+
59+
popd > /dev/null || exit 1
60+
popd > /dev/null || exit 1
61+
exit "${exit_code}"
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@ if [[ $# -eq 0 ]]; then
1313
else
1414
FILES=$*
1515
fi
16-
exit_code=0
1716

17+
TO_FIX=()
18+
exit_code=0
19+
set +e
1820
for fn in ${FILES}; do
19-
if [[ "${fn}" == *".sh" || "${fn}" == *".zsh" || "${fn}" == *".bash" ]]; then
20-
if [[ ! -x "${fn}" ]]; then
21-
echo "${fn} is not executable, but probably should be"
22-
exit_code=1
23-
fi
21+
found=$(grep -l ' +$' "${fn}")
22+
if [[ -n "${found}" ]]; then
23+
TO_FIX+=("${found}")
24+
exit_code=1
2425
fi
2526
done
27+
set -e
2628

29+
if [[ "${#TO_FIX[@]}" -gt 0 ]]; then
30+
printf '%s\n' "${TO_FIX[@]}"
31+
fi
2732
popd > /dev/null || exit 1
2833
popd > /dev/null || exit 1
2934
exit "${exit_code}"

tools/find_missing_vera.py

100644100755
File mode changed.

0 commit comments

Comments
 (0)