【发布时间】: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