Git合并后挂钩,如何获取合并分支的名称
我正在尝试创建合并后挂钩脚本,该脚本仅在从特定分支合并时运行。如何确定特定提交的分支更改的名称?
例如
if $from_specific_branch == 1 then
git diff --name-status HEAD@{1} HEAD "some_folder" |
while read st file; do
#skip deleted
if [ "$st" == 'D' ]; then continue; fi
# .. do something with those files
end
I'm trying to create post-merge hook script which runs only when merging from specific branch. How can I determine name of the branch changes came from for specific commit?
e.g.
if $from_specific_branch == 1 then
git diff --name-status HEAD@{1} HEAD "some_folder" |
while read st file; do
#skip deleted
if [ "$st" == 'D' ]; then continue; fi
# .. do something with those files
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,值得注意的是,您可能会将任何提交合并到当前提交中 - 事实上,它不必位于任何分支上。
当您创建合并时,我相信第一个父级 (
HEAD^1
) 始终是您合并时所在的分支。因此,您可以查看HEAD^2
(在章鱼合并的情况下可能还可以查看HEAD^3
、HEAD^4
)并进行测试它们是否在特定的分支上。要测试
HEAD^2
是否在分支foo
上,可以测试git merge-base HEAD^2 foo
是否与相同代码>git rev-parse --verify HEAD^2
。Firstly, it's worth noting that you might be merging any commit into the current commit - it doesn't have to be on any branch, in fact.
When you create a merge, I believe that the first parent (
HEAD^1
) is always the branch you were on when merging. So, you can look atHEAD^2
(and possiblyHEAD^3
,HEAD^4
in the case of an octopus merge) and test whether they are on particular branches.To test if
HEAD^2
is on the branchfoo
, you can test whethergit merge-base HEAD^2 foo
is the same asgit rev-parse --verify HEAD^2
.您可以在挂钩中使用 $GIT_REFLOG_ACTION 。它包含诸如 merge some_branch_name 或 pull origin some_branch_name 等文本。
You can just use $GIT_REFLOG_ACTION in your hook. It contains text like merge some_branch_name, or pull origin some_branch_name or so on.
那么你可以使用 gitbranch --contains列出所有分支;不确定它是否完全满足您的要求,因为它会列出包含该提交的任何分支,但假设您正在合并到新的提交中,它可能会成功......
Well you can use
git branch --contains <commit>
to list all the branches; not sure that accomplishes exactly what you want as it will list any branches that contain that commit, but presuming you're merging in a new commit, it might do the trick...