In the ruby hook, I need to check the staged file format and restrict the commit, If the commit have .exe
or .dll
file. I got the idea to get the file list in ruby git diff --cached --name-only --diff-filter=ACM
, But I failed to use the line in ruby script because I am newbie for ruby script.
#!/usr/bin/env ruby
Dir[git diff --cached --name-only --diff-filter=ACM].any? do |f|
%w|.txt .pdf .dll|.include?(File.extname(f))
puts "Your commit have exe or dll file. Kindly remove the file an$
exit 0
end
I know If the hook return zero meant it passed the test, Or else it failed the condition. When I run the git push
command means, It shows below exception.
remote: hooks/pre-receive:16:in
require_relative': /opt/gitlab/embedded/service/gitlab-shell/lib/gitlab_ignore_binary.rb:3: syntax error, unexpected unary-, expecting keyword_do or '{' or '(' (SyntaxError) remote: Dir[git diff --cached --name-only --diff-filter=ACM].any? do |f| remote: ^ remote: /opt/gitlab/embedded/service/gitlab-shell/lib/gitlab_ignore_binary.rb:3: syntax error, unexpected unary-, expecting keyword_do or '{' or '(' remote: Dir[git diff --cached --name-only --diff-filter=ACM].any? do |f| remote: ^ remote: /opt/gitlab/embedded/service/gitlab-shell/lib/gitlab_ignore_binary.rb:3: syntax error, unexpected ']', expecting end-of-input remote: Dir[git diff --cached --name-only --diff-filter=ACM].any? do |f| remote: ^ remote: from hooks/pre-receive:16:in
'
Any helps can be apriciated.
Update:
Is there any way to update the gitignore
manually?