[英]git - Can I have public preferred config settings for my repo?
I'd like to do something like force anyone from committing trailing whitespace to my branch by showing them an error rather than allow the commit. 我想做一些类似的事情,通过向他们显示错误而不是允许提交来迫使任何人将尾随空白提交到我的分支。 I know I can add something to .git/config but I'm interested in doing something so that when the repo is cloned the file is still there like .gitignore. 我知道我可以在.git / config中添加一些内容,但是我有兴趣做一些事情,以便克隆存储库时文件仍然像.gitignore一样存在。 Does git allow for something like that with config? git是否允许使用config这样的东西?
The config cannot be cloned. 该配置无法克隆。
You could setup a template directory that the users would need to use when cloning your repo ( git clone
): 您可以设置一个模板目录 ,供用户在克隆存储库时需要使用( git clone
):
git clone --template=/path/to/template/folder /url/of/your/repo
That would allow a custom .git/config
to be copied in the cloned repo (with a apply.whitespace
set to error
). 这将允许将自定义.git/config
复制到克隆的apply.whitespace
(将apply.whitespace
设置为error
)。
But that means the users have to use that option. 但这意味着用户必须使用该选项。
The other option is to setup a pre-receive hook on the bare repo your colleagues would push to, in order to reject the commit if whitespace errors are detected. 另一个选择是在同事将要推送的裸仓库上设置一个预接收钩子,以便在检测到空白错误时拒绝提交。
Centralizing that constraint allows the users to have the settings they want locally, since that particular option is enforced on the push in the central (bare) repo. 集中约束可以使用户在本地具有他们想要的设置,因为在中央(裸露)存储库中的推送上会强制执行该特定选项。
Example of such an hook: 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}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.