开发者

git hooks post-merge

Is there anyway to tell whether or not the merge made was associated with a pull?

I'd like to u开发者_开发问答se git-flow's merges as a hook to send my data to heroku. Ideas?


Personally, I wouldn't like this arrangement: every merge into master or development that you try locally would cause a deploy, and I'd much rather make deploying the code an active decision - there's no room to try merging something without deploying it. (Also, I don't know about Heroku's behaviour exactly, but I would be surprised if it redeployed when pushing a commit to a ref that wouldn't change that ref.) Finally, often the merge that is done by a pull will not be a fast-forward, so there will be a new version of the code to deploy.

However, if you really want to do this, you can do the following in a script:

UPSTREAM=$(git rev-parse --symbolic-full-name @{u})

That will give you the remote-tracking branch that represents upstream of your current branch - in a post-merge hook your current branch is the one that you've just merged into. (That nice command is taken from Kevin Ballard's comment on this answer.) You can find the object name (SHA1sum) of that commit with:

UPSTREAM_OBJECT_NAME=$(git rev-parse --verify $UPSTREAM)

Then you want to find out if that commit was one of the parents of HEAD - you can get the parents with:

HEAD_PARENTS=$(git log -n 1 --pretty=format:"%P")

... so can test for whether upstream was one of the parents with:

git log -n 1 --pretty=format:"%P" | grep -q $UPSTREAM_OBJECT_NAME

Finally, you should check the first line of the reflog for the remote-tracking branch to check that it was last updated via a pull. In other words, does the line:

git reflog show -n 1 $UPSTREAM

... contain pull : fast-forward.

That should give you the pieces you need for post-merge script of the type that you want. To reiterate, though, I really wouldn't want this ;)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜