0

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?

Arunkumar
  • 73
  • 1
  • 11
  • 1
    If you want to perform system call you should wrap your string in ` (not '). Like this: \`git diff --cached --name-only --diff-filter=ACM\` – unkmas Sep 22 '17 at 12:15
  • I'm confused what you're trying to achieve here... Could you not just put those specific file types in the `.gitignore`?? – Tom Lord Sep 22 '17 at 12:37
  • @TomLord ignored files can still be committed using `git add -f` – Max Sep 22 '17 at 13:24
  • @Max and git hooks can be ignored by deleting/commenting them out, or using `git commit --no-verify`! This can only be a sanity check, not a foolproof blocker. – Tom Lord Sep 22 '17 at 13:29
  • Part of your problem is that your Ruby syntax is wrong, but there's no requirement that hooks use Ruby. If you are more comfortable in some other language, use another language. Meanwhile, another issue is that pre-receive hooks are complex; even if you correct some obvious errors in the above code fragment, it will not be suitable as a pre-receive hook. A pre-receive hook must read its standard input and parse each line to determine what is being requested by whoever is running `git push`. – torek Sep 22 '17 at 15:15
  • @TomLord You are right, But I think that the `.gitignore` file need to add each and every project manually. I have more than 500 projects. So only I go for the hook. Is any way exist to add the `.gitignore` file automatically in all projects? – Arunkumar Sep 25 '17 at 03:55
  • @Parkavi You can also define a [global gitignore](https://help.github.com/articles/ignoring-files/#create-a-global-gitignore) for your personal machine, if desired. – Tom Lord Sep 25 '17 at 08:28
  • @TomLord I think `global-gitignore` will work only if the user configures in his machine. Otherwise, the user can add the file. I think `pre-receive` hook is best to do this without fail. –  Sep 26 '17 at 12:15
  • @Arunkumar A `pre-receive` hook will only work if the user configures in his machine. You *could* implement it as a git template or symbolic link; but either way the user still needs to configure this on their machine!! – Tom Lord Sep 26 '17 at 13:29
  • @TomLord Yes, Any suggestion to do the same operation by `pre-receive` hook in the server-side machine? – Arunkumar Sep 27 '17 at 04:17
  • @Parkavi I repeat that I think this is a bad idea; it is very possible that you will one day want to commit one of these "blacklisted" file types in one of your repos. However, after a quick google search I found that [global custom hooks are supported since gitlab-shell v4.10](https://docs.gitlab.com/ee/administration/custom_hooks.html#chained-hooks-support). They work via a symbolic link convention. – Tom Lord Sep 27 '17 at 09:20
  • @TomLord I tried to add the hook in `.git/hooks/ - symlink to gitlab-shell/hooks global dir` from your suggestion link. Also I want to restrict all our user to restrict to commit those type of file forever. – Arunkumar Sep 27 '17 at 09:35
  • @Parkavi Since this question has now diverged massively, I'd suggest you either update the question or delete it. Your original post was about fixing a git hook (which contained invalid ruby syntax); and it's now become about implementing a global `gitignore` as a pre-commit hook in gitlab. – Tom Lord Sep 27 '17 at 10:03
  • @TomLord Question updated – Arunkumar Sep 27 '17 at 10:20
  • @Parkavi That's not the right question. You update the `gitignore` manually by *editing the file*. The question is "How do I define a **global** gitignore, or implement it as a **global pre-commit hook**, in gitlab?". – Tom Lord Sep 27 '17 at 10:22

0 Answers0