1

我想做一些事情,例如通过向他们显示错误而不是允许提交来强制任何人将尾随空格提交到我的分支。我知道我可以向 .git/config 添加一些东西,但我有兴趣做一些事情,以便在克隆 repo 时文件仍然存在,如 .gitignore。git是否允许配置类似的东西?

4

1 回答 1

0

无法克隆配置。

您可以设置一个模板目录git clone,用户在克隆您的 repo ( )时需要使用该目录:

git clone --template=/path/to/template/folder /url/of/your/repo

这将允许.git/config在克隆的 repo 中复制自定义(apply.whitespace设置为error)。

但这意味着用户必须使用该选项。


另一种选择是在您的同事将推送到的裸仓库上设置一个预接收挂钩,以便在检测到空白错误时拒绝提交。
集中该约束允许用户在本地拥有他们想要的设置,因为该特定选项是在中央(裸)存储库中的推送时强制执行的。

这种钩子的例子:this gist, ruby​​ script (extract)

#!/usr/bin/env ruby

old_sha, new_sha, ref = STDIN.read.split(' ')

diff = %x{ git diff --raw --name-only #{old_sha} #{new_sha} 2> /dev/null }

diff.each_line do |line|
file = line.chomp
next unless file =~ /\.rb\Z/i

tree = %x{ git ls-tree --full-name #{new_sha} #{file} 2> /dev/null }.split(" ")

contents = %x{ git cat-file blob #{tree[2]} 2> /dev/null }

if found = contents.each_line.map(&:chomp).find{ |e| e =~ /\s+\Z/ }
puts <<EOF
> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> !!!!!!!!! PUSH REJECTED !!!!!!!!!
> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>
> REMOVE TRAILING WHITESPACE.
>  * #{tree[3]}
>
>  #{found.inspect}
于 2013-10-02T09:11:28.240 回答