Git挂钩,用于在分支更改时更新Git子模块 [英] Git hook for updating Git submodule on branch change

查看:186
本文介绍了Git挂钩,用于在分支更改时更新Git子模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些使用Git子模块的旧代码。该代码具有多个正在使用的分支,但不幸的是,这些分支中的每一个都依赖于相关子模块的不同修订版。 这使开发变得极为复杂。我面临的问题之一是,当我从 master newfeature-foo ,使用 git checkout newfeature-foo ,子模块的状态仍保持为 master 的状态,这通常会导致编译错误,甚至更糟的是,会导致运行时行为方面的差异,从而给用户测试和一般状况造成严重破坏。 / p>

例如,给定 .gitmodules 配置:

  [子模块 robotcontroller] 
路径= robotcontroller
url = https://coolrobots.com/repos/robotcontroller
分支=主
忽略=脏




  • 分支 master 在提交 77d4697 时引用了 robotcontroller @ f57d1b3

  • 分支<位于 b38d29f 的code> newfeature-foo 引用了 robotcontroller @ 60b27d4
  • $ b在 873639f 处的$ b
  • 分支 master 没有子模块 robotcontroller 完全

  • 301dca4 上的分支 newfeature-foo 引用了 robotcontroller @ bdf5991



没有办法,例如使用Git钩子强制在 Git存储库签出时更改 .gitmodules 文件中列出的所有子模块的自动完全重新签出? p>

解决方案

检出后钩子,可删除 robotcontroller 并更新所有子模块。

 #!/ bin / sh 

#更新子模块的结帐后挂钩

prev_HEAD = $ 1
new_HEAD = $ 2
new_branch = $ 3

if [ $ new_branch = 1];然后
如果! grep -Fq robotcontroller .gitmodules;然后
rm -rf robotcontroller
fi

git子模块更新
fi

exit 0

PS。根Git存储库的正确术语是 superproject。 :-)


I'm working with some old code that makes use of Git submodules. The code has several divergent branches which are actively being used, and each of these branches depends on unfortunately a different revision of the relevant submodules; this has made development extremely complicated. One of the problems I face is that, when I switch between branches from e.g. master to newfeature-foo using git checkout newfeature-foo, the state of the submodules remains at that of master, which often causes compile errors and even worse can cause differences in runtime behavior that wreaks havoc with user testing and general sanity.

For example, given the .gitmodules configuration:

[submodule "robotcontroller"]
    path = robotcontroller
    url = https://coolrobots.com/repos/robotcontroller
    branch = master
    ignore = dirty

  • the branch master at commit 77d4697 has a reference to robotcontroller @ f57d1b3
  • the branch newfeature-foo at b38d29f has a reference to robotcontroller @ 60b27d4
  • the branch master at 873639f doesn't have the submodule robotcontroller at all
  • the branch newfeature-foo at 301dca4 has a reference to robotcontroller @ bdf5991

Is there no way e.g. using Git hooks to force the automatic complete re-checking-out of all submodules listed in a .gitmodules file when it changes on checkout of the "root" Git repository?

解决方案

post-checkout hook that removes robotcontroller and update all submodules.

#!/bin/sh

# post-checkout hook that update submodules

prev_HEAD="$1"
new_HEAD="$2"
new_branch="$3"

if [ "$new_branch" = 1 ]; then
   if ! grep -Fq robotcontroller .gitmodules; then
      rm -rf robotcontroller
   fi

   git submodule update
fi

exit 0

PS. The correct term for the root Git repository is "superproject". :-)

这篇关于Git挂钩,用于在分支更改时更新Git子模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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