Question

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. Does git allow for something like that with config?

Était-ce utile?

La solution

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 --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).

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}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top