Git hooks - childlabor/blog GitHub Wiki

Git hook能够在发生某特定行为的时机,触发执行自定义的脚本。

Git hook分为客户端hooks(Client-Side Hooks)和服务端hooks(Server-Side Hooks)。可以触发hook的时机,可以在官方文档中查询。

git hooks位置位于每个git项目下的隐藏文件夹.git中的hooks文件夹里,进去后会看到一些hooks的官方示例。注意以.sample结尾的示例脚本是不会执行的,只有重命名后才会生效。

.git里面的文件,无法提交到git项目上去。如果自定义了hooks文件,最好在git目录最外层新建一个同名的hooks文件夹,把写好的脚步放里面。新clone该项目时,在根目录执行一下

// 把脚本文件复制到.git里去
$ cp hooks/* .git/hooks/

pre-commit

pre-commit: 执行git commit命令时触发,常用于检查代码风格。

配置:

// package.json
{
  ...
  "gitHooks": {
    "pre-commit": "lint-staged"
  },
   "lint-staged": {
    "*.{js,vue}": [
      "vue-cli-service lint",
      "git add"
    ]
  }
}

如果项目未安装lint-staged,执行命令可能会出现以下提示:

$ git add .
$ git commit -am "test githook"
 > running pre-commit hook: lint-staged
'lint-staged' ▒▒▒▒▒ڲ▒▒▒▒ⲿ▒▒▒Ҳ▒▒▒ǿ▒▒▒▒еij▒▒▒
▒▒▒▒▒▒▒▒▒ļ▒▒▒

pre-commit hook failed (add --no-verify to bypass)

lint-staged就非常准确的解决了这一问题,从这个包名,就可以看出,Run linters on git staged files,只针对改动的文件进行处理。

$ npm install --save-dev lint-staged

示例:

// xx.vue
...
data() {
    return {
      searchListLoading: false, // loading
      123 // 错误行 未代码检查直接提交
      accountInfo: '',
      ...
     }
}

// 运行
// 一般情况下,会自动修复错误;无法自动处理的错误会抛出
$ git add .
$ git commit -am "test githook"
 > running pre-commit hook: lint-staged
‼ Some of your tasks use `git add` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.

Preparing... [started]
Preparing... [completed]
Running tasks... [started]
Running tasks for *.{js,vue} [started]
vue-cli-service lint [started]
vue-cli-service lint [failed]
→
Running tasks for *.{js,vue} [failed]
→
Running tasks... [failed]
Applying modifications... [started]
Applying modifications... [skipped]
→ Skipped because of errors from tasks.
Reverting to original state... [started]
Reverting to original state... [completed]
Cleaning up... [started]
Cleaning up... [completed]



× vue-cli-service found some errors. Please fix them and try committing again.
error: Parsing error: Unexpected token

22 |       searchListLoading: false, // loading
23 | 123
> 24 |       accountInfo: '',
   |       ^
25 |       showLogo: true,
26 |       searchKeyword: '',
27 |       filterParams: {}, at src\views\SearchList\index.vue:75:6:
73 |       searchListLoading: false, // loading
74 | 123
> 75 |       accountInfo: '',
   |      ^
76 |       showLogo: true,
77 |       searchKeyword: '',
78 |       filterParams: {},


1 error found.

pre-commit hook failed (add --no-verify to bypass)

// 找到相关文件,手动修改后,再次提交
$ git add .
$ git commit -am "test githook"
 > running pre-commit hook: lint-staged
‼ Some of your tasks use `git add` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.

Preparing... [started]
Preparing... [completed]
Running tasks... [started]
Running tasks for *.{js,vue} [started]
vue-cli-service lint [started]
vue-cli-service lint [completed]
git add [started]
git add [completed]
Running tasks for *.{js,vue} [completed]
Running tasks... [completed]
Applying modifications... [started]
Applying modifications... [completed]
Cleaning up... [started]
Cleaning up... [completed]
[feature-hooks 856a4de] test githook
 1 file changed, 1 deletion(-)
 

如果要忽略检查,强制提交:

$ git add .
$ git commit -am "test githook" --no-verify