Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 56 additions & 6 deletions helpers/python/functions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ function python_bypass_enabled {
}

function check_python {
local pylint=
pylint="$(command -v pylint 2>&1)"
if test -x "${pylint}"; then
python_bypass_enabled || check_python_files "${pylint}"
local linter=

case "${GIT_PYTHON_LINTER:-ruff}" in
pylint|ruff) linter="$(command -v "${GIT_PYTHON_LINTER}" 2>&1)" ;;
*) echo "Unsupported linter: ${GIT_PYTHON_LINTER}"; exit 2
esac

if test -x "$(which "${linter}")"; then
python_bypass_enabled || check_python_files_"$(basename "${linter}")" "${linter}"
else
printf "** WARNING ** Unable to execute pylint: %s\n" "${pylint}"
printf "** WARNING ** Unable to execute linter: %s\n" "${linter}"
fi

}

function check_python_files {
function check_python_files_pylint {
local -i lint_issues=0
local -a errors=()

Expand Down Expand Up @@ -67,3 +73,47 @@ function check_python_files {
printf " OK\n\n"
fi
}

function check_python_files_ruff {
local -i lint_issues=0
local -a errors=()

# shellcheck disable=SC2155
local py_version="$(env python --version)"
local ruff="${1}"
local ruffrc="${HOOKS_DIR}/etc/python/.ruff.toml"
local rufflog="lint.log"
local ruffargs=""

if [[ "${GIT_PYTHON_LINT_CONFIG:-undefined}" != "undefined" ]] && [[ -f "${GIT_PYTHON_LINT_CONFIG}" ]]; then
ruffrc="${GIT_PYTHON_LINT_CONFIG}"
fi

printf "\nPython version: %s" "${py_version}"
printf "\nValidating Python with %s check --config=%s %s:\n\n" \
"${ruff}" "${ruffrc}" "${ruffargs}"

for file in $(get_python_files); do
if [[ "${file:(-2)}" != "py" ]]; then
printf " - ignoring file: %s\n" "${file}"
continue
fi

("${ruff}" check --config="${ruffrc}" ${ruffargs} "${file}" >"${rufflog}" 2>&1)

if test $? -ne 0 ; then
errors[${lint_issues}]="$(printf " - fix lint issues: %s\n\n%s\n" "${file}" "$(grep -E '^[RCWEF]:' "${rufflog}" | sed 's/^/ /g')")"
lint_issues=$((lint_issues + 1))
fi
done

if test ${lint_issues} -gt 0; then
for error in "${errors[@]}"; do
printf "%s\n" "${error}"
done
printf "\nPlease fix lint issues before committing\n\n"
exit 2
else
printf " OK\n\n"
fi
}