给其他人的一些注意事项!
新手会犯的最大错误是在完成子模块更新后,在子模块中使用分离的 HEAD 提交。我将尝试用来自钩子的强烈警告来解决这个问题。
下一个最大的可能是在进行需要的结帐后无法进行子模块更新。同样,钩子可以检查这一点并发出警告。
至于开发过程,这种设置使得在子模块中拥有良好的测试基础架构变得更加重要,这样如果可能的话,您可以在其中工作而不必在父模块中工作,并完全避免这个问题。
我将尝试从我最终使用的钩子中发布示例代码,并在一个月后跟进(希望不会太多)真实的恐怖故事。
编辑:
这是钩子的初稿。请记住,这是一项匆忙的工作,请放轻松!
在父仓库中:
对于合并后和结帐后,如果子模块不同步,我们会警告用户。(post-merge 尤其适用于快进合并,从原点拉取)还要注意他们会想要签出一个分支,尽管子模块的 post-checkout 钩子在他们运行子模块更新时也会这样做。提醒越多越好。
#!/bin/bash
if git submodule status | grep '^+' > /dev/null; then
echo "WARNING: common model submodule now out of sync. You probably want to run" 1>&2
echo " git submodule update, then make sure to check out an appropriate branch" 1>&2
echo " in the submodule." 1>&2
fi
对于提交后,如果有子模块更改,我们会警告用户他们可能忘记将它们包含在提交中。在这种高度耦合的情况下,这是一个很好的猜测。用户不太可能分别修改模拟和通用模型。
#!/bin/bash
if git submodule status | grep '^+' > /dev/null; then
echo "WARNING: common model submodule has changes. If the commit you just made depends" 1>&2
echo " on those changes, you must run git add on the submodule, and then run" 1>&2
echo " git commit --amend to fix your commit." 1>&2
fi
在子模块中,一个 post-checkout 钩子强烈警告分离的 HEAD:
#!/bin/bash
get_ppid() {
ps --no-headers -o ppid $1
}
# Check to see if this checkout is part of a submodule update
# git submodule calls git checkout, which calls this script, so we need to
# check the grandparent process.
if ps --no-headers -o command $(get_ppid $(get_ppid $$)) | grep 'submodule update' &> /dev/null; then
if ! git symbolic-ref HEAD &> /dev/null; then
echo "WARNING: common model submodule entering detached HEAD state. If you don't know" 1>&2
echo " what this means, and you just ran 'git submodule update', you probably" 1>&2
echo " want to check out an appropriate branch in the submodule repository." 1>&2
echo
# escape the asterisk from SO's syntax highlighting (it sees C comments)
branches=($(git for-each-ref --format='%(objectname) %(refname:short)' refs/heads/\* | grep ^$(git rev-parse HEAD) | cut -d\ -f2))
case ${#branches} in
0 )
;;
1 )
echo "Branch '${branches[0]}' is at HEAD"
;;
* )
echo "The following branches are at HEAD: ${branches[@]}"
;;
esac
fi
echo
fi
我还添加了一个预提交挂钩来简单地中止使用分离的 HEAD 进行的提交(除非它是一个变基)。我很害怕得到经典的“我所有的提交都消失了”的恐慌投诉。--no-verify
如果你知道你在做什么,你总是可以绕过它。