You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pre-commit 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/sh
  2. # get the list of modified files
  3. files=$(git diff --cached --name-only)
  4. # check if api or web directory is modified
  5. api_modified=false
  6. web_modified=false
  7. for file in $files
  8. do
  9. # Use POSIX compliant pattern matching
  10. case "$file" in
  11. api/*.py)
  12. # set api_modified flag to true
  13. api_modified=true
  14. ;;
  15. web/*)
  16. # set web_modified flag to true
  17. web_modified=true
  18. ;;
  19. esac
  20. done
  21. # run linters based on the modified modules
  22. if $api_modified; then
  23. echo "Running Ruff linter on api module"
  24. # run Ruff linter auto-fixing
  25. uv run --project api --dev ruff check --fix ./api
  26. # run Ruff linter checks
  27. uv run --project api --dev ruff check ./api || status=$?
  28. status=${status:-0}
  29. if [ $status -ne 0 ]; then
  30. echo "Ruff linter on api module error, exit code: $status"
  31. echo "Please run 'dev/reformat' to fix the fixable linting errors."
  32. exit 1
  33. fi
  34. # run ty checks
  35. uv run --directory api --dev ty check || status=$?
  36. status=${status:-0}
  37. if [ $status -ne 0 ]; then
  38. echo "ty type checker on api module error, exit code: $status"
  39. echo "Please run 'dev/ty-check' to check the type errors."
  40. exit 1
  41. fi
  42. fi
  43. if $web_modified; then
  44. echo "Running ESLint on web module"
  45. cd ./web || exit 1
  46. lint-staged
  47. echo "Running unit tests check"
  48. modified_files=$(git diff --cached --name-only -- utils | grep -v '\.spec\.ts$' || true)
  49. if [ -n "$modified_files" ]; then
  50. for file in $modified_files; do
  51. test_file="${file%.*}.spec.ts"
  52. echo "Checking for test file: $test_file"
  53. # check if the test file exists
  54. if [ -f "../$test_file" ]; then
  55. echo "Detected changes in $file, running corresponding unit tests..."
  56. pnpm run test "../$test_file"
  57. if [ $? -ne 0 ]; then
  58. echo "Unit tests failed. Please fix the errors before committing."
  59. exit 1
  60. fi
  61. echo "Unit tests for $file passed."
  62. else
  63. echo "Warning: $file does not have a corresponding test file."
  64. fi
  65. done
  66. echo "All unit tests for modified web/utils files have passed."
  67. fi
  68. cd ../
  69. fi