Open kangyana opened 1 year ago
git 允许在各种操作之前添加一些 hook 脚本,如未正常运行则 git 操作不通过。 最出名的还是以下两个:
而 hook 脚本置于目录 ~/.git/hooks 中,以可执行文件的形式存在。
~/.git/hooks
$ ls -lah .git/hooks applypatch-msg.sample pre-merge-commit.sample commit-msg.sample pre-push.sample fsmonitor-watchman.sample pre-rebase.sample post-update.sample pre-receive.sample pre-applypatch.sample prepare-commit-msg.sample pre-commit.sample update.sample
另外 git hooks 可使用 core.hooksPath 自定义脚本位置。
core.hooksPath
# 可通过命令行配置 core.hooksPath $ git config 'core.hooksPath' .husky # 也可通过写入文件配置 core.hooksPath $ cat .git/config [core] ignorecase = true precomposeunicode = true hooksPath = .husky
在前端工程化中,husky 即通过自定义 core.hooksPath 并将 npm scripts 写入其中的方式来实现此功能。
husky
npm scripts
~/.husky 目录下手动创建 hook 脚本。
~/.husky
# 手动创建 pre-commit hook $ vim .husky/pre-commit
在 pre-commit 中进行代码风格校验。
pre-commit
#!/bin/sh npm run lint npm run test
git 允许在各种操作之前添加一些 hook 脚本,如未正常运行则 git 操作不通过。 最出名的还是以下两个:
而 hook 脚本置于目录
~/.git/hooks
中,以可执行文件的形式存在。另外 git hooks 可使用
core.hooksPath
自定义脚本位置。在前端工程化中,
husky
即通过自定义core.hooksPath
并将npm scripts
写入其中的方式来实现此功能。~/.husky
目录下手动创建 hook 脚本。在
pre-commit
中进行代码风格校验。