没有内置任何东西(尽管毫无疑问应该有)。您可以做的是提供一个预提交挂钩,用于验证所有名称是否正常,如果不正常则阻止提交。
这个钩子只需要在 Linux 机器上运行(虽然让它在 Linux 和 Mac 上工作很容易,但只有 Windows 及其默认的贫乏工具箱才有问题)。您可能想将其添加到侧分支并向 Linux 人员提供有关设置它的说明。
您可能想检查分支names以及,如在git 预提交或更新挂钩,用于停止具有不区分大小写匹配的分支名称的提交 https://stackoverflow.com/q/35658887/1256452。 (有趣的是:这个问题的答案是我自己的;我忘了。)
首先,让我们编写一个“检查大小写冲突”函数。这只是使用大小写折叠进行排序的问题(以便“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
, so:
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
仅检查added文件,允许现有的案例冲突继续,和/或尝试跨多个分支和/或提交检查内容,但这变得很困难。
将上述两个片段组合成一个脚本(和测试),然后将其简单地复制到名为的可执行文件中.git/hooks/pre-commit
.
检查分支名称有点棘手。这确实应该在您创建分支名称时发生,而不是在您提交分支名称时发生,并且不可能在客户端上做得很好 - 它必须在具有适当全局视图的集中式服务器上完成。
这是一种在服务器上使用预接收脚本、shell 脚本而不是 Python 执行此操作的方法(如链接的答案中所示)。我们仍然需要checkstdin
不过,您可能希望在更新挂钩而不是预接收挂钩中执行此操作,因为您不需要拒绝entirePush,只是一个分支名称。
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