-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·73 lines (66 loc) · 2.34 KB
/
pre-commit
File metadata and controls
executable file
·73 lines (66 loc) · 2.34 KB
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
71
72
73
#!/bin/bash
runValue=0
check="message"
# This function's main purpose is to search for a file or program and pr# int an error message if it is not found. The function takes in a file # or program as the parameter.
function exists()
{
program=$1
if [ hash "$program" >/dev/null 2>&1 ]; then
echo >&2 "$program is not found. Please install to continue"
exit 1
fi;
}
# This function receives the output number from the debugger implemented# and depending on whether it is 0 or not, it prints out the errors that# the debugger found in the committed code and sets the runValue to 1
function implementChecks()
{
result=$1
errorMsg=$2
if [ "$result" -ne "0" ] ; then
echo "$file: failed check"
echo "$errorMsg"
runValue=1
fi;
}
# This function goes thrugh any shellscript file that has been committed# and uses the shellcheck debugger to find errors. It then prints out th# e errors found in the committed code and sets the runValue to 1
function verifyShellScripts()
{
exists "shellcheck"
file=$1
check=$(shellcheck "$file")
implementChecks "$?" "$check"
return $runValue
}
# This function runs through any python file that has been committed and# uses the pylint debugger to find errors. Then, the errors are printed # and the runValue is set to 1
function verifyPythonScripts()
{
exists "pylint"
file=$1
check=$(pylint "$file")
implementChecks "$?" "$check"
return $runValue
}
# This function acts the same way as the two functions in the sense that# it uses goimports to debug through a go program and fix whatever synta# x issues present. The difference is goimports will automatically corre# ct the errors found, thus committing code instantly.
function verifyGoScripts()
{
exists "goimports"
file=$1
goimports -w $file
}
# The for loop runs every file that is attempting to be committed on git# through their respective functions,dependent on the type of file it is
for i in $(git diff --cached --name-only --diff-filter=ACM); do
input=${i:(-3)}
if [ "$input" == ".sh" ] ; then
verifyShellScripts "$i"
elif [ "$input" == ".py" ] ; then
verifyPythonScripts "$i"
elif [ "$input" == ".go" ] ; then
verifyGoScripts "$i"
fi;
done
# At the end, if the runValue is equal to 1, then an error message, "Com# mit Failed", not committing the code to the repository
if [ "$runValue" -eq "1" ] ; then
echo "Commit Failed"
exit 1
else
exit 0
fi;