【问题标题】:Git pre-commit bash script on WindowsWindows 上的 Git 预提交 bash 脚本
【发布时间】:2018-02-15 13:52:19
【问题描述】:

我为 git 创建了一个简单的预提交脚本:

#!/bin/sh

if git rev-parse —verify HEAD >/dev/null 2>&1; then
    against=HEAD
else
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

for FILE in `git diff --cached --name-only` ; do
    # Check if the file contains 'DbMigration'
    echo $FILE $against
    if [ -n "grep -E ':\s*DbMigration\s' $FILE" ];
    then
        echo ''
        echo ''
        echo '[**CODEPOLICE**]'
        echo '[**CODEPOLICE**]' $FILE
        echo '[**CODEPOLICE**]'
        echo '[**CODEPOLICE**] This file contains a direct subclass of DbContext! Refactor your migrations to use <...> instead!'
        echo '[**CODEPOLICE**]'
        echo ''
        echo ''
        exit 1
    fi
done

exit

检查if [ -n "grep -E ':\s*DbMigration\s' $FILE" ] 惨遭失败,因为它会产生误报。

涉及的版本有:

Windows 10 Enterprise

$ git --version
git version 2.15.1.windows.2

$ bash --version
GNU bash, version 4.4.12(1)-release (x86_64-pc-msys)

什么给了?

更新

一些例子:

 public partial class Initial : DbMigration --> we want positive & we get positive --> ok

 public partial class Initial : FoobarDbMigration --> we want negative & we get positive --> not ok

 public partial class Initial : Foobar --> we want negative & we get positive --> not ok

 public partial class Initial : DbMigrationFoobar --> we want negative & we get positive --> also not ok

【问题讨论】:

  • 你能添加一个“肯定”的例子和一个“误报”吗?
  • 非常感谢您的收看。稍后我会根据您的要求更新问题。

标签: bash git githooks pre-commit-hook


【解决方案1】:

你必须启动一个子shell来执行命令。

要测试一个空字符串,你必须这样做:

[ -n "$(grep -E ':\s*DbMigration\s' $FILE)" ]

这对于您提供的所有给定测试用例都是正确的。

【讨论】:

    【解决方案2】:

    测试

    [ -n "grep -E ':\s*DbMigration\s' $FILE" ]
    

    不运行该命令,它测试"" 中的字符串是否不为空。而且它不是空的,所以测试总是成功的!

    要运行命令并测试其输出,请使用反引号而不是双引号:

    [ -n "`grep -E ':\s*DbMigration\s' $FILE`" ]
    

    或使用$():

    [ -n "$(grep -E ':\s*DbMigration\s' $FILE)" ]
    

    【讨论】:

    • 虽然这是代码的问题,但它并没有给出正确的结果。 grep -E ':\s*DbMigration\s' $FILE | wc 将提供1 0 1 并且-n 的测试仍然会成功。
    • @StefanM 我不知道你为什么要谈论wc。你可能需要在 re:grep -Eq ':\s*DbMigration\s* 结尾处 * (看看你的例子)。
    【解决方案3】:

    似乎符合要求的 bash 脚本竟然是这个:

    #!/bin/sh
    
    RED='\033[0;31m'
    NC='\033[0m' # No Color
    
    echo ""
    echo -n "[**CODEPOLICE**] Checking for usages of 'DbMigration' over 'VNextDbMigration' ... "
    
    stagedFiles=`git diff --cached --name-only`
    while read -r FILE ; do
        # Check if the file contains ': DbMigration'
    
        if [ -f "$FILE" ];
        then
            matchingLines=$( grep -P ":(\s|\r|\n)*DbMigration(\s|$)" "$FILE" )
            if [ "$matchingLines" != "" ];
            then
                echo -e ""
                echo -e "${RED}[**CODEPOLICE**]${NC}"
                echo -e "${RED}[**CODEPOLICE**]${NC}" $FILE
                echo -e "${RED}[**CODEPOLICE**]${NC}"
                echo -e "${RED}[**CODEPOLICE**]${NC} ^- This file contains a direct subclass of 'DbContext'! Refactor your migration to have it inherit from 'VNextDbMigration' instead!"
                echo -e "${RED}[**CODEPOLICE**]${NC}"
                echo -e ""
                echo -e ""
                exit 1
            fi
        fi
    done <<< $stagedFiles
    
    echo "ALL OK"
    echo ""
    
    exit
    

    【讨论】:

      猜你喜欢
      • 2012-01-18
      • 1970-01-01
      • 2013-05-26
      • 1970-01-01
      • 2014-01-03
      • 2020-09-06
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多