如何使用git diff --cached --name-only --diff-filter = ACM?

时间:2017-09-27 12:09:41

标签: ruby git hook pre-commit-hook

我尝试编写pre-receive挂钩来检查提交文件。我尝试使用git命令git diff --cached --name-only --diff-filter=ACM来获取文件提交文件列表。但我得到了例外

#!/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

结束

但我在第3行遇到语法错误

  

语法错误,意外的一元 - 期待keyword_do或'{'或'('(SyntaxError)

作为ruby脚本初学者,我无法预测为什么会发生这种情况。我需要一只手来解决这个问题。

根据elpiekay指南更新。

for line in ARGF.readlines;arr=line.split();oldvalue=arr[0];newvalue=arr‌​[1];refname=arr[2];

Dir[git diff $oldvalue $newvalue --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 and try again"
 exit 1
 end
exit 0
end

但现在我也在Dir[git diff $oldvalue $newvalue --name-only --diff-filter=ACM].any? do |f|行面临同样的错误。

1 个答案:

答案 0 :(得分:0)

我不熟悉Ruby,所以我可以在bash中完成。

#!/bin/bash

z40=0000000000000000000000000000000000000000

while read oldvalue newvalue refname
do
    if [ "$newvalue" == "$z40" ];then
        #Trying to delete the ref, do something here
        :
    else
        if [ "$oldvalue" == "$z40" ];then
            #Trying to create a new ref, do something here
            :
        else
            git diff $oldvalue $newvalue --name-only --diff-filter=ACM | if grep -e ".txt" -e ".pdf" -e ".dll";then
                echo "Your commit has txt, pdf or dll file(s). Kindly remove the file(s) and try again"
                exit 1
            fi
        fi
    fi
done