Git合并后钩子,如何获取合并分支的名称 [英] Git post-merge hook, how to get name of merged branch

查看:105
本文介绍了Git合并后钩子,如何获取合并分支的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建仅在从特定分支合并时才运行的合并后挂钩脚本.我如何确定分支更改的名称来自特定的提交?

例如

 如果$ from_specific_branch == 1则git diff --name-status HEAD @ {1} HEAD"some_folder" |同时读取st文件;做#skip已删除如果["$ st" =='D'];然后继续;科幻#..对那些文件进行处理结尾 

解决方案

首先,值得注意的是,您可能会将任何提交合并到当前提交中-实际上,它不必位于任何分支上.

创建合并时,我相信第一个父级( HEAD ^ 1 )始终是合并时所在的分支.因此,您可以查看 HEAD ^ 2 (如果是章鱼合并,还可以查看 HEAD ^ 3 HEAD ^ 4 )并进行测试它们是否在特定分支上.

要测试 HEAD ^ 2 是否在分支 foo 上,可以测试 git merge-base HEAD ^ 2 foo 是否是与 git rev-parse --verify HEAD ^ 2 相同.

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

解决方案

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 at HEAD^2 (and possibly HEAD^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 branch foo, you can test whether git merge-base HEAD^2 foo is the same as git rev-parse --verify HEAD^2.

这篇关于Git合并后钩子,如何获取合并分支的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆