Git合并后挂钩,如何获取合并分支的名称

发布于 2024-12-02 08:57:23 字数 323 浏览 0 评论 0原文

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

例如

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

软糯酥胸 2024-12-09 08:57:23

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

当您创建合并时,我相信第一个父级 (HEAD^1) 始终是您合并时所在的分支。因此,您可以查看 HEAD^2 (在章鱼合并的情况下可能还可以查看 HEAD^3HEAD^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 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.

与君绝 2024-12-09 08:57:23

您可以在挂钩中使用 $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.

旧时浪漫 2024-12-09 08:57:23

那么你可以使用 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文