开发者

Git post-merge hook, how to get name of merged branch

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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜