【问题标题】:Git skip post-commit hookGit跳过提交后挂钩
【发布时间】:2021-01-07 10:35:47
【问题描述】:

我的仓库中有一个 git post-commit 钩子。我有时想跳过运行这个钩子。

要跳过预提交挂钩,我知道我可以在像这样提交时使用 --no-verify 标志

git commit -m "message" --no-verify

但这并没有跳过提交后挂钩。

是否可以跳过提交后挂钩?如果有怎么办?

【问题讨论】:

    标签: git


    【解决方案1】:

    来自documentation

    -n --no-verify 此选项绕过 pre-commit 和 commit-msg 挂钩。另见 githooks[5]。

    所以这个标志不会跳过提交后挂钩。似乎没有一种简单、干净的方法来跳过这个标志。对于一次性操作;您可以在提交后禁用钩子并再次启用它:

    chmod -x .git/hooks/post-commit # disable hook
    git commit ... # create commit without the post-commit hook
    chmod +x .git/hooks/post-commit # re-enable hook
    

    【讨论】:

      【解决方案2】:

      这是可能的。以下是我将为 Linux 和 bash 做的事情:

      #!/bin/bash
      
      # parse the command line of the parent process
      # (assuming git only invokes the hook directly; are there any other scenarios possible?)
      
      while IFS= read -r -d $'\0' ARG; do
          if test "$ARG" == '--no-verify'; then
              exit 0
          fi
      done < /proc/$PPID/cmdline
      
      # or check for the git config key that suppresses the hook
      # configs may be supplied directly before the subcommand temporarily (or set permanently in .git/config)
      # so that `git -c custom.ignorePostCommitHookc=<WHATEVER HERE> commit ...` will suppress the hook
      
      if git config --get custom.ignorePostCommitHook > /dev/null; then
          exit 0
      fi
      
      # otherwise, still run the hook
      
      echo '+---------+'
      echo '| H O O K |'
      echo '+---------+'
      

      【讨论】:

        猜你喜欢
        • 2011-11-06
        • 2015-09-10
        • 1970-01-01
        • 2014-05-10
        • 2018-09-29
        • 2011-12-16
        • 1970-01-01
        • 2017-10-20
        相关资源
        最近更新 更多