在"git checkout -b"之后触发post-checkout钩子.命令 [英] Triggering post-checkout hook after "git checkout -b" command
问题描述
Same question was asked here before however both answers didn't help me much.
我无法将 post-checkout
钩子与 git checkout -b
命令的 git checkout
区别为 $ 1
(上一个HEAD的sha1)和 $ 2
(新HEAD的sha1)对于这两个调用都是相同的.
I couldn't get my post-checkout
hook to differ git checkout
from git checkout -b
command as $1
(sha1 of previous HEAD) and $2
(sha1 of new HEAD) are same for both calls.
这是我的结帐后脚本:
#!/bin/bash
echo "old HEAD: $1"
echo "new HEAD: $2"
echo "checkout type: $3"
我执行了以下命令:
> ozgur@ozgurv:~/project (master)$ git checkout -b new_branch
old HEAD: e86423aa9f45053cb45b8ec15d463bb9684526a2
new HEAD: e86423aa9f45053cb45b8ec15d463bb9684526a2
checkout type: 1
> ozgur@ozgurv:~/project (new_branch)$ git checkout my_branch
old HEAD: e86423aa9f45053cb45b8ec15d463bb9684526a2
new HEAD: e86423aa9f45053cb45b8ec15d463bb9684526a2
checkout type: 1
我要实现的目的是仅在创建新的本地分支时而不在签出时执行 post-checkout
挂钩中的逻辑.
What I am trying to achieve is to execute the logic in post-checkout
hook only when a new local branch is created, not when it is checked out.
推荐答案
在结帐后的钩子中,您无法真正分辨出差异. 1 在任何情况下,您都可能不会想尝试说出区别.考虑:
You can't really tell the difference, in a post-checkout hook.1 In any case you might not want to try to tell the difference. Consider:
$ git branch newbr # make a new branch
$ git checkout newbr # and now check it out
在此处执行 git checkout -b newbr
可能要做的任何事情可能都是有意义的,如果是,则检查 -b
标志(如果可以的话)让它正常工作)可能适得其反.(还应考虑当 origin/feature
存在而本地分支 feature
不存在时会创建 feature
的 git checkout功能
作为跟踪 origin/feature
的新分支,即使再次没有 -b
标志.)
It probably makes sense to do whatever you would have done for git checkout -b newbr
here, and if so, checking for the -b
flag (if you can get it to work) is probably counterproductive. (Consider as well git checkout feature
when origin/feature
exists and local branch feature
does not, which creates feature
as a new branch that tracks origin/feature
, even though again there is no -b
flag.)
让我们再看一次,我将建议一种不同的方法:
Let's look at this again, and I'll suggest a different approach:
仅当创建新的本地分支时
only when a new local branch is created
如果在您的钩中保留了文件中保存的所有可见的本地分支机构"列表,该怎么办?(也许 .git/ozgur/branchlist
将是一个合适的保留位置.我们希望仓库周围有一些东西,而git本身不太可能使用.)然后,如果结帐是一个分支样式的签出( $ 3
为 1
),将当前分支与列表进行比较:
What if, in your hook, you kept a list of "all seen-so-far local branches" saved in a file? (Perhaps .git/ozgur/branchlist
would be a reasonable place to keep this. We want something that follows the repository around, that git itself is unlikely to use.) Then, if the checkout is a branch-style checkout ($3
is 1
), compare the current branch with the list:
git_dir=$(git rev-parse --git-dir) || exit 1
branchlist=$git_dir/ozgur/branchlist
if [ "$3" = 1 ]; then
# Checked out a branch - what branch are we on now?
# (Skip rest of code if we're on a detached HEAD.)
curbranch=$(git symbolic-ref --short HEAD) || return
# Is $curbranch in our branch list? Create empty
# branch list first if needed. Can just "touch" but
# this avoids changing the mod-time unnecessarily.
[ -f $branchlist ] || : > $branchlist
if ! grep "^$curbranch$" $branchlist > /dev/null; then
echo "switching to as-yet-unvisited-branch $curbranch"
echo $curbranch >> $branchlist # now we've visited it
fi
fi
这仍然有一个缺陷:如果删除分支,它可能会保留在分支列表文件中.我们可以通过定期过滤掉已删除的分支来解决此问题(将 $ branchlist
内容与相应的 git for-each-ref refs/heads
输出进行比较),也许是在其他git钩子中也一样但这可能足以满足您的目的.
This still has a bit of a flaw: if you delete a branch, it may remain in the branchlist file. We could fix that by filtering away deleted branches fairly regularly (compare the $branchlist
contents to appropriate git for-each-ref refs/heads
output), perhaps in additional git hooks as well. But it might suffice for your purposes as-is.
1 实际上,有一种 告诉方法,至少在允许您查看进程树并查看其命令参数的系统上.从钩子中找到您的父 git checkout
进程,并查看它是否具有 -b
标志.但是然后它在两步 git分支上不起作用...;git checkout
序列,也不在从上游分支自动创建的分支上.
1Actually, there is a way to tell, at least on systems that will let you look at a process tree, and look at their command arguments. From the hook, find your parent git checkout
process and see if it has a -b
flag. But then it doesn't work on the two-step git branch ...; git checkout
sequence, nor on branches automatically created from an upstream branch.
这篇关于在"git checkout -b"之后触发post-checkout钩子.命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!