-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit.sh
44 lines (40 loc) · 2.05 KB
/
pre-commit.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
#!/usr/bin/env sh
#|-----------------------------------------------------------------------------------------------------------------|
#| Program Name: pre-commit.sh
#|-----------------------------------------------------------------------------------------------------------------|
#| Description: This script is a standard git hook. It is invoked each time when developer do `git commit' command.
#| This script uses POSIX standards and should work with no errors in Unix as well as in
#| other Unix-like OS'.
#|-----------------------------------------------------------------------------------------------------------------|
#| Description: This script checks if commit for mistakes inside commit message and commited files
#| 1) This script checks if file saved in UTF-8
#| 2) It removes BOM and carriage return
#| 3) It runs rubocop
#|
#| Note:
#| a) This script does not take any arguments
#|
#|-----------------------------------------------------------------------------------------------------------------|
#| Author: unurgunite
#| Date: 2022/10/05
#|-----------------------------------------------------------------------------------------------------------------|
#| License: MIT
#|-----------------------------------------------------------------------------------------------------------------|
# Check that all staged files are saved in UTF-8 encoding
for file in $(git diff --cached --name-only); do
sed -e 's/^\xEF\xBB\xBF//' "$file"
sed -e 's/\r$//' "$file"
encoding=$(file --mime-encoding -b "$file")
if [ "$encoding" != "utf-8" ] && [ "$encoding" != "us-ascii" ]; then
echo "Error: File $file is not encoded in UTF-8 or US-ASCII"
exit 1
fi
done
# Get only added or modified Ruby files
changed_files=$(git status --porcelain | awk -F ' ' '$1 ~ /A|AM|^M/ {print $2}' | grep "\.rb$" | tr '\n' ' ')
if [ -n "$changed_files" ]; then
# Run rubocop to detect and automatically fix errors in the code
bundle exec rubocop --autocorrect --require rubocop-performance "$changed_files"
exit $?
fi
exit 0