#!/usr/bin/env bash
set -ex
CACHE_DIRECTORY="/tmp/.lintbin"
CLANG_LINTERS="CLANGTIDY,CLANGTIDY_EXECUTORCH_COMPATIBILITY,CLANGFORMAT"
PYREFLY_LINTERS="PYREFLY"
case "${LINTER_GROUP:-}" in
clang)
ADDITIONAL_LINTRUNNER_ARGS="--take ${CLANG_LINTERS} ${CHANGED_FILES}"
;;
pyrefly)
ADDITIONAL_LINTRUNNER_ARGS="--take ${PYREFLY_LINTERS} --all-files"
;;
other)
ADDITIONAL_LINTRUNNER_ARGS="--skip ${CLANG_LINTERS},${PYREFLY_LINTERS} ${CHANGED_FILES}"
;;
"")
;;
*)
echo "Unknown linter group: ${LINTER_GROUP}" >&2
exit 1
;;
esac
if [[ -d "${CACHE_DIRECTORY}" ]]; then
cp -r "${CACHE_DIRECTORY}" . || true
fi
if [[ "${CLANG}" == "1" ]]; then
spin regenerate-clangtidy-files
fi
spin regenerate-version
spin regenerate-type-stubs
find torch -name '*.pyi' -exec git add --force -- "{}" +
RC=0
if ! spin lint -- --force-color --tee-json=lint.json ${ADDITIONAL_LINTRUNNER_ARGS} 2> /dev/null; then
echo ""
echo -e "\e[1m\e[36mYou can reproduce these results locally by using \`lintrunner -m origin/main\`. (If you don't get the same results, run \'lintrunner init\' to update your local linter)\e[0m"
echo -e "\e[1m\e[36mSee https://github.com/pytorch/pytorch/wiki/lintrunner for setup instructions. To apply suggested patches automatically, use the -a flag. Before pushing another commit,\e[0m"
echo -e "\e[1m\e[36mplease verify locally and ensure everything passes.\e[0m"
RC=1
fi
find torch -name '*.pyi' -exec git restore --staged -- "{}" +
jq --raw-output \
'"::\(if .severity == "advice" or .severity == "disabled" then "warning" else .severity end) file=\(.path),line=\(.line),col=\(.char),title=\(.code) \(.name)::" + (.description | gsub("\\n"; "%0A"))' \
lint.json || true
exit $RC