在 Git 中检出新分支后,有没有办法触发钩子?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1011557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:37:19  来源:igfitidea点击:

Is there a way to trigger a hook after a new branch has been checked out in Git?

gitgithooks

提问by Rob Watson

Is there a way to trigger a hook after a new branch has been checked out in Git?

在 Git 中检出新分支后,有没有办法触发钩子?

采纳答案by SpliFF

If one of these hooks won't do it I'd be amazed:

如果这些钩子之一不这样做,我会感到惊讶:

https://schacon.github.io/git/githooks.html

https://schacon.github.io/git/githooks.html

Maybe this one:

也许这个

post-checkout

This hook is invoked when a git-checkout is run after having updated the worktree. The hook is given three parameters: the ref of the previous HEAD, the ref of the new HEAD (which may or may not have changed), and a flag indicating whether the checkout was a branch checkout (changing branches, flag=1) or a file checkout (retrieving a file from the index, flag=0). This hook cannot affect the outcome of git-checkout.

结帐后

在更新工作树后运行 git-checkout 时会调用此钩子。钩子被赋予三个参数:前一个 HEAD 的 ref,新 HEAD 的 ref(可能已经改变也可能没有改变),以及一个指示结帐是否是分支结帐的标志(改变分支,标志=1)或文件检出(从索引中检索文件,flag=0)。这个钩子不会影响 git-checkout 的结果。

回答by givanse

A git hook is a script placed in a special location of your repository, that location is:

git hook 是放置在存储库特殊位置的脚本,该位置是:

.git/hooks

.git/钩子

The script can be any kind that you can execute in your environment i.e. bash, python, ruby etc.

脚本可以是您可以在您的环境中执行的任何类型,即 bash、python、ruby 等。

The hook that is executed after a checkout is post-checkout. From the docs:

结帐执行的钩子是post-checkout。从文档:

...The hook is given three parameters...

...钩子被赋予三个参数...

Example:

例子:

  1. Create the hook (script):

    touch .git/hooks/post-checkout
    chmod u+x .git/hooks/post-checkout
    
  2. Hook sample content:

  1. 创建钩子(脚本):

    touch .git/hooks/post-checkout
    chmod u+x .git/hooks/post-checkout
    
  2. 钩子示例内容:

#!/bin/bash                                                                      

set -e                                                                           

printf '\npost-checkout hook\n\n'                                                

prevHEAD=                                                                      
newHEAD=                                                                       
checkoutType=                                                                  

[[ $checkoutType == 1 ]] && checkoutType='branch' ||                             
                            checkoutType='file' ;                                

echo 'Checkout type: '$checkoutType                                              
echo '    prev HEAD: '`git name-rev --name-only $prevHEAD`                       
echo '     new HEAD: '`git name-rev --name-only $newHEAD`

Note: The shebang in the first line indicates the type of script.

注意:第一行的shebang表示脚本的类型。

This script (git hook) will only capture the three parameters passed and print them in a human-friendly format.

这个脚本(git hook)只会捕获传递的三个参数并以人性化的格式打印它们。

回答by Justin Tilson

Similar to others but verifies that the branch has been checked out once.

与其他类似,但验证分支是否已签出一次。

#!/bin/bash

# this is a file checkout – do nothing
if [ "" == "0" ]; then exit; fi

BRANCH_NAME=$(git symbolic-ref --short -q HEAD)
NUM_CHECKOUTS=`git reflog --date=local | grep -o ${BRANCH_NAME} | wc -l`

#if the refs of the previous and new heads are the same 
#AND the number of checkouts equals one, a new branch has been created
if [ "" == ""  ] && [ ${NUM_CHECKOUTS} -eq 1 ]; then
    git push origin ${BRANCH_NAME}
fi

回答by Steve Bennett

The post-checkouthookreceives three parameters:

post-checkout接收三个参数:

  1. Ref of previous HEAD
  2. Ref of new HEAD
  3. Whether this is a file checkout (0) or branch checkout (1)
  1. 参考以前的 HEAD
  2. 新 HEAD 的参考
  3. 这是文件检出 ( 0) 还是分支检出 ( 1)

You can use the fact that a branch created from the current HEAD will have the same value for parameters 1 and 2.

您可以使用这样一个事实,即从当前 HEAD 创建的分支对于参数 1 和 2 具有相同的值。

cat > .git/hooks/post-checkout <<"EOF"
if [ "" == "0" ]; then exit; fi
if [ "" == "" ]; then 
  echo "New branch created. (Probably)."
fi
EOF

chmod u+x .git/hooks/post-checkout

Limitations:

限制:

  • Checking out an existing branch which happens to be at the same HEAD as the current HEAD will fool it.
  • Creating a new branch notfrom the current HEAD will not be detected.
  • 检查与当前 HEAD 位于同一 HEAD 的现有分支将愚弄它。
  • 不会检测到不是从当前 HEAD创建的新分支。