【问题标题】:How to use the git diff --cached --name-only --diff-filter=ACM?如何使用 git diff --cached --name-only --diff-filter=ACM?
【发布时间】:2017-09-27 12:09:40
【问题描述】:

我尝试编写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| 行中也面临同样的错误。

【问题讨论】:

  • git diff --cached 不能在 pre-receive 挂钩中工作。您需要从标准输入中获取更新后的ref的旧值和新值,然后通过git diff $oldvalue $newvalue --name-only --diff-filter=ACM获取这两个值之间的更改文件。
  • @ElpieKay 谢谢你提供的宝贵信息,你指引我的方向正确,我怎么能得到$oldvalue $newvalue ?有什么建议吗?
  • for line in ARGF.readlines;arr=line.split();oldvalue=arr[0];newvalue=arr[1];refname=arr[2];#here do something;end。将此 sn-p 放入 pre-receive 钩子中。
  • @ElpieKay 再次感谢您的帮助,我根据您的回答更新了问题,但我在这里也遇到了同样的问题!你能告诉我我犯的错误吗?
  • 对不起,我对 Ruby 不熟悉。

标签: ruby git hook pre-commit-hook


【解决方案1】:

我不熟悉 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 2017-02-14
    • 2021-11-16
    • 2019-03-21
    相关资源
    最近更新 更多