有没有办法在 Git 中签出新分支后触发挂钩?

发布于 2024-07-25 01:50:23 字数 30 浏览 18 评论 0原文

有没有办法在 Git 中签出新分支后触发挂钩?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在梵高的星空下 2024-08-01 01:50:24

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

#!/bin/bash

# this is a file checkout – do nothing
if [ "$3" == "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 [ "$1" == "$2"  ] && [ ${NUM_CHECKOUTS} -eq 1 ]; then
    git push origin ${BRANCH_NAME}
fi

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

#!/bin/bash

# this is a file checkout – do nothing
if [ "$3" == "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 [ "$1" == "$2"  ] && [ ${NUM_CHECKOUTS} -eq 1 ]; then
    git push origin ${BRANCH_NAME}
fi
破晓 2024-08-01 01:50:24

post-checkout hook 接收三个参数:

  1. Ref of previous HEAD
  2. Ref新的 HEAD
  3. 这是文件签出 (0) 还是分支签出 (1)

您可以使用以下事实:从当前 HEAD 创建的分支将具有相同的值对于参数 1 和 2。

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

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

限制:

  • 检查与当前 HEAD 恰好位于同一 HEAD 的现有分支会欺骗它。
  • 不会检测到从当前 HEAD 创建新分支not

The post-checkout hook receives three parameters:

  1. Ref of previous HEAD
  2. Ref of new HEAD
  3. Whether this is a file checkout (0) or branch checkout (1)

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

cat > .git/hooks/post-checkout <<"EOF"
if [ "$3" == "0" ]; then exit; fi
if [ "$1" == "$2" ]; 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 not from the current HEAD will not be detected.
北城挽邺 2024-08-01 01:50:23

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

.git/hooks

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

签出后执行的挂钩是 结帐后。 来自文档:

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

示例:

  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=$1                                                                      
newHEAD=$2                                                                       
checkoutType=$3                                                                  

[[ $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`

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

该脚本(git hook)只会捕获传递的三个参数并以人类友好的格式打印它们。

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

.git/hooks

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

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

...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:

#!/bin/bash                                                                      

set -e                                                                           

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

prevHEAD=$1                                                                      
newHEAD=$2                                                                       
checkoutType=$3                                                                  

[[ $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.

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

在梵高的星空下 2024-08-01 01:50:23

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

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

也许这个

结帐后


git-checkout 运行后
更新了工作树。 钩子是
给定三个参数: 的 ref
前一个 HEAD,新 HEAD 的 ref
(可能改变也可能没有改变),
和一个标志,指示是否
结账是分行结账
(更改分支,flag=1)或文件
签出(从
索引,标志=0)。 这个钩子不能
影响 git-checkout 的结果。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文