【问题标题】:Use git-hooks for certain branches (With Husky and git-branch-is)对某些分支使用 git-hooks(使用 Husky 和 ​​git-branch-is)
【发布时间】:2019-09-28 01:48:29
【问题描述】:

我将huskygit-branch-is 用于git 挂钩。在我的 package.json 下方

{
"scripts": {
    "test": "jest",
     ...
},
"husky": {
  "hooks": {
    "pre-commit": "git-branch-is master && npm test",
    "pre-push": "git-branch-is master && npm test"
 }
}

从分支提交时使用此选项 feature/802 我重新筛选以下错误

Error: Current branch is "feature/802", not "master". 

问题

  1. 如何禁用以 "feature"
  2. 开头的分支的 git 挂钩
  3. 仅对 masterdevelop 分支应用挂钩。
  4. 不使用 bash 脚本可以实现吗?

【问题讨论】:

    标签: git npm git-branch package.json husky


    【解决方案1】:

    这对我有用。 git-branch-is 不是必需的。

    {
      "hooks": {
        "commit-msg": "if [[ $(git rev-parse --abbrev-ref HEAD) = develop ]]; then commitlint -E HUSKY_GIT_PARAMS; fi"
      }
    }
    

    【讨论】:

      【解决方案2】:

      使用if [ git-branch-is master]; then npm test; fi。然后,返回失败(非零)退出值的测试git-branch-is master 将不会被视为钩子的终端失败。

      如果挂钩需要在符合 POSIX 的外壳之外运行,这可能会引发问题。

      【讨论】:

        【解决方案3】:

        我检查分支是否不是 master 或分支是 master 并且测试是否成功。

        "pre-push": "git-branch-is -r \"^((?!master).)*$\" || (git-branch-is master && npm test)"
        

        【讨论】:

        • 这似乎是一个很好的答案,但如果不解释解决方案的哪一部分解决了哪个问题,就很难评估。请添加一些文字来解释您的解决方案是如何工作的。
        猜你喜欢
        • 2020-08-12
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        • 2016-06-14
        • 2018-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多