使用husky尝试使用预提交挂钩不起作用

mspsb9vt  于 2022-10-23  发布在  Git
关注(0)|答案(1)|浏览(155)

我一直在配置哈士奇一段时间,但我在这个问题上遇到过,我认为我有正确的配置,但当我尝试提交钩子时,没有调用钩子。
下面是我的代码库
package.json包

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "@types/jest": "^27.5.2",
    "@types/node": "^16.11.68",
    "@types/react": "^18.0.21",
    "@types/react-dom": "^18.0.6",
    "eslint-config-airbnb-typescript": "^17.0.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "typescript": "^4.8.4",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "lint": "eslint .",
    "lint:fix": "eslint --fix .",
    "prepare": "husky install"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.40.1",
    "@typescript-eslint/parser": "^5.40.1",
    "eslint": "^8.25.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-jsx-a11y": "^6.6.1",
    "eslint-plugin-prettier": "^4.2.1",
    "eslint-plugin-react": "^7.31.10",
    "eslint-plugin-react-hooks": "^4.6.0",
    "husky": "^8.0.0",
    "prettier": "^2.7.1"
  }
}

预提交文件


# !/usr/bin/env sh

. "$(dirname -- "$0")/_/husky.sh"

npm test

我没有任何输出,所以对我的git版本感到奇怪

git version 2.37.0 (Apple Git-136)
k5hmc34c

k5hmc34c1#

要测试某个现有的预提交挂钩是否正在运行,请修改它,使其退出并显示错误消息,从而阻止提交。在这种情况下,可以将.git/hooks/pre-commit文件更改为:


# !/usr/bin/env sh

echo debugging the pre-commit hook 1>&2
exit 1

# . "$(dirname -- "$0")/_/husky.sh"

# npm test

如果git commit打印消息并阻止您提交,您就知道预提交挂钩正在运行。如果提交成功,您就知道预提交挂钩没有运行。
如果预提交钩子未运行,请检查常见的疑点:请参阅git pre- and post- commit hooks not running

相关问题