没有内置任何东西(尽管应该有,毫无疑问)。你可以做的是提供一个 pre-commit 钩子来验证所有名称是否正常,如果不正常则阻止提交。
这个钩子只需要在 Linux 机器上运行(虽然让它在 Linux 和 Mac 上运行很容易,但问题在于 Windows 及其默认的贫乏工具箱)。您可能想将它添加到一个分支中,并为 Linux 人员提供有关设置的说明。
您可能还想检查分支names,如git pre-commit or update hook for stopping commit with branch names having Case Insensitive match。 (有趣:这个问题的答案是我自己的;我忘记了。)
首先,让我们编写一个“检查大小写冲突”函数。这只是用大小写折叠排序的问题(以便“helloworld”和“helloWorld”彼此相邻放置),然后使用uniq -di 打印任何重复的(在大小写折叠之后)字符串,但没有非重复:
sort -f | uniq -di
如果这产生任何输出,这些都是“坏名”。让我们在一个临时文件中捕获输出并检查它的大小,以便我们也可以将它们打印到标准输出:
#! /bin/sh
TF=$(mktemp)
trap "rm -f $TF" 0 1 2 3 15
checkstdin() {
sort -f | uniq -di > $TF
test -s $TF || return 0 # if $TF is empty, we are good
echo "non-unique (after case folding) names found!" 1>&2
cat $TF 1>&2
return 1
}
现在我们只需要在将要提交的文件上使用它,也许还可以在分支名称上使用它。前者用git ls-files列出,所以:
git ls-files | checkstdin || {
echo "ERROR - file name collision, stopping commit" 1>&2
exit 1
}
您可以想象一下,使用git diff-index --cached -r --name-only --diff-filter=A HEAD 仅检查添加的文件,允许现有的案例冲突继续,和/或尝试跨多个分支和/或提交检查内容,但是变得困难。
将以上两个片段组合成一个脚本(并测试),然后简单地将其复制到一个名为.git/hooks/pre-commit 的可执行文件中。
检查分支名称有点棘手。这确实应该在您创建分支名称时发生,而不是在您提交时发生,并且在客户端上做得非常好是不可能的 - 它必须在具有适当全局视图的集中式服务器上完成。
这是一种在服务器上以预接收脚本、shell 脚本而不是 Python 执行此操作的方法(如链接答案中所示)。不过,我们仍然需要 checkstdin 函数,并且您可能希望在更新挂钩而不是预接收挂钩中执行此操作,因为您不需要拒绝 整个 推送,只需一个分支名称。
NULLSHA=0000000000000000000000000000000000000000 # 40 0s
# Verify that the given branch name $1 is unique,
# even IF we fold all existing branch names' cases.
# To be used on any proposed branch creation (we won't
# look at existing branches).
check_new_branch_name() {
(echo "$1"; git for-each-ref --format='%(refname:short)' refs/heads) |
checkstdin || {
echo "ERROR: new branch name $1 is not unique after case-folding" 1>&2
exit 1 # or set overall failure status
}
}
while read oldsha newsha refname; do
... any other checks ...
case $oldsha,$refname in
$NULLSHA,refs/heads/*) check_new_branch_name ${refname#refs/heads/};;
esac
... continue with any other checks ...
done