Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e76bc17

Browse files
authored
Merge pull request #627 from pre-commit/simplify_hook_template
Use some bash best practices and simplify hook template
2 parents eefeaaf + 916ca72 commit e76bc17

1 file changed

Lines changed: 18 additions & 36 deletions

File tree

pre_commit/resources/hook-tmpl

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,38 @@
22
# This is a randomish md5 to identify this script
33
# 138fd403232d2ddd5efb44317e38bf03
44

5-
pushd `dirname $0` > /dev/null
6-
HERE=`pwd`
7-
popd > /dev/null
5+
pushd "$(dirname "$0")" >& /dev/null
6+
HERE="$(pwd)"
7+
popd >& /dev/null
88

99
retv=0
1010
args=""
1111

1212
ENV_PYTHON={sys_executable}
1313
SKIP_ON_MISSING_CONF={skip_on_missing_conf}
1414

15-
which pre-commit >& /dev/null
16-
WHICH_RETV=$?
17-
"$ENV_PYTHON" -c 'import pre_commit.main' >& /dev/null
18-
ENV_PYTHON_RETV=$?
19-
python -c 'import pre_commit.main' >& /dev/null
20-
PYTHON_RETV=$?
21-
22-
23-
if ((
24-
(WHICH_RETV != 0) &&
25-
(ENV_PYTHON_RETV != 0) &&
26-
(PYTHON_RETV != 0)
27-
)); then
15+
if which pre-commit >& /dev/null; then
16+
exe="pre-commit"
17+
run_args=""
18+
elif "$ENV_PYTHON" -c 'import pre_commit.main' >& /dev/null; then
19+
exe="$ENV_PYTHON"
20+
run_args="-m pre_commit.main"
21+
elif python -c 'import pre_commit.main' >& /dev/null; then
22+
exe="python"
23+
run_args="-m pre_commit.main"
24+
else
2825
echo '`pre-commit` not found. Did you forget to activate your virtualenv?'
2926
exit 1
3027
fi
3128

32-
3329
# Run the legacy pre-commit if it exists
34-
if [ -x "$HERE"/{hook_type}.legacy ]; then
35-
"$HERE"/{hook_type}.legacy
36-
if [ $? -ne 0 ]; then
37-
retv=1
38-
fi
30+
if [ -x "$HERE"/{hook_type}.legacy ] && ! "$HERE"/{hook_type}.legacy; then
31+
retv=1
3932
fi
4033

4134
CONF_FILE="$(git rev-parse --show-toplevel)/{config_file}"
42-
if [ ! -f $CONF_FILE ]; then
43-
if [ $SKIP_ON_MISSING_CONF = true ] || [ ! -z $PRE_COMMIT_ALLOW_NO_CONFIG ]; then
35+
if [ ! -f "$CONF_FILE" ]; then
36+
if [ "$SKIP_ON_MISSING_CONF" = true -o ! -z "$PRE_COMMIT_ALLOW_NO_CONFIG" ]; then
4437
echo '`{config_file}` config file not found. Skipping `pre-commit`.'
4538
exit $retv
4639
else
@@ -55,18 +48,7 @@ fi
5548
{hook_specific}
5649

5750
# Run pre-commit
58-
if ((WHICH_RETV == 0)); then
59-
pre-commit run $args --config {config_file}
60-
PRE_COMMIT_RETV=$?
61-
elif ((ENV_PYTHON_RETV == 0)); then
62-
"$ENV_PYTHON" -m pre_commit.main run $args
63-
PRE_COMMIT_RETV=$?
64-
else
65-
python -m pre_commit.main run $args
66-
PRE_COMMIT_RETV=$?
67-
fi
68-
69-
if ((PRE_COMMIT_RETV != 0)); then
51+
if ! "$exe" $run_args run $args --config {config_file}; then
7052
retv=1
7153
fi
7254

0 commit comments

Comments
 (0)