0

I am trying to restrict the binary file(dll, pdf, exe) committing in our GitLab instance. My moto is need to abort the commit if the commit have binary files. I have the code for aborting the commit in Python,

#!/usr/bin/env python3

import os

def scanDir(dirName):
    for root, dirs, files in os.walk(dirName):
        for fileName in files:
            if fileName.split('.')[-1] in ["txt","dll","pdf"]:
                return 1
    return 0

My problem in Gitlab have hooks file in ruby, My code is in python, So It doesn`t works in ruby code. (How to load the python file in ruby hook?)

Also I do not know what is path should I need to give in def scanDir(dirName): I tried my level best , But I can`t able do this in ruby because I newbie to ruby.

Can anyone help to add the hook to restrict the binary file?

1 Answers1

0

GitLab hooks might be in any language.

It’s unclear why you can’t just use your python code as is.


FWIW, the translated code would be:

def scan_dir(dir_name)
  Dir["#{dir_name}/**/*"].any? do |f|
    %w|.txt .pdf .dll|.include?(File.extname(f))
  end
end
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • Thanks for your guiding, What dir_name could I pass to check the files? –  Sep 07 '17 at 12:37
  • I am unsure about `GitLab`, but I think it would be a project root folder. – Aleksei Matiushkin Sep 07 '17 at 12:39
  • Ok, In your answer where the code returns 0 or 1 because there is no exit command, That`s why I asked –  Sep 07 '17 at 12:42
  • 1
    `1` and `0` in python are for truthy and falsey. In ruby, the result of the last line of a method is returned, hence the result of call to `any?` is returned. – Aleksei Matiushkin Sep 07 '17 at 12:44
  • Could you let me know the way to pass the argument to `scan_def` method? if the path is `/home/git/repositories/` –  Sep 07 '17 at 12:51
  • 1
    I have no clue what `scan_def` is. The argument is passed to the method as in literally all other languages: `method(argument)`. If an argument is the same in most calls, it might be made a default via `def scan_dir(dir_name = "/home/git/repositories/")` and the method might be called without an argument to enable default. – Aleksei Matiushkin Sep 07 '17 at 12:52
  • Above code is not working for me, I totally wrong calculated the workflow of the git and worked stupidly. I found a clue in this question https://stackoverflow.com/questions/26955973 to get the staged file in git by **ruby**. But don`t know how to use the answer in ruby code for my this question https://stackoverflow.com/questions/46095928. Could you help me? –  Sep 22 '17 at 10:02