forked from DFE-Digital/early-careers-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·58 lines (48 loc) · 1.92 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
# Find an object to check against
if git rev-parse --verify HEAD >/dev/null 2>&1
then
# If we have a commit, use it
against=HEAD
else
# If we don't, diff against empty object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Redirect output to stderr.
exec 1>&2
# Check changed files for an AWS keys
KEY=$(
git diff --cached --name-only -z $against |
tr '\0' '\n' |
grep --invert-match --fixed-strings 'yarn.lock' |
grep --invert-match --fixed-strings 'Gemfile' |
grep --invert-match --fixed-strings 'Gemfile.lock' |
xargs ls -d 2> /dev/null |
xargs cat |
grep -v '\.\/node_modules\/' |
perl -nle'print $& if m{(?<![A-Z0-9])[A-Z0-9]{20}(?![A-Z0-9])|(?<![A-Za-z0-9/+=])[A-Za-z0-9/+=]{40}(?![A-Za-z0-9/+=])}'
)
if [[ "$KEY" != "" ]] && [[ "$KEY" != *"/components/schemas/"* ]]; then
echo "Found patterns for AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY"
echo "Please check your code and remove API keys."
echo "$KEY"
exit 1
fi
# stash unstaged changes
git diff > unstaged.diff
git apply -R unstaged.diff
# Lint, fix, store exit code, and stage modifications
CHANGED_RUBY_FILES=$(git diff --name-only --cached | xargs ls -1 2>/dev/null | grep '\.rb$')
echo "$CHANGED_RUBY_FILES"
if [[ -n "$CHANGED_RUBY_FILES" ]]; then echo "$CHANGED_RUBY_FILES" | xargs rubocop --force-exclusion --fail-fast --autocorrect ; fi
RUBOCOP_EXIT_CODE="$?"
CHANGED_JS_FILES=$(git diff --name-only --cached | xargs ls -1 2>/dev/null | grep 'app\|spec' | grep '\.js$')
if [[ -n "$CHANGED_JS_FILES" ]]; then echo "$CHANGED_JS_FILES" | xargs yarn run prettier --write ; fi
if [[ -n "$CHANGED_JS_FILES" ]]; then echo "$CHANGED_JS_FILES" | xargs yarn run eslint --max-warnings 0 --fix ; fi
ESLINT_EXIT_CODE="$?"
((EXIT_CODE = RUBOCOP_EXIT_CODE ** 2 + ESLINT_EXIT_CODE ** 2))
git add --update
# Pop the unstaged changes
git apply unstaged.diff
rm unstaged.diff
exit "$EXIT_CODE"