使用预提交仅验证已修改文件中的更改,而不是验证整个文件

时间:2016-03-02 12:37:06

标签: ruby-on-rails git pre-commit-hook pre-commit rubocop

我在我的存储库中应用了一个预提交钩子,我想要做的是检查rubocop的语法,但只检查我试图提交的更改。

目前预先提交是使用rubocop检查样式和规则,我已经修改了所有文件并且不允许我提交,因为一些旧代码根据rubocop无效。

有没有办法可以更改它,只检查已更改的代码,如果代码是正确的,则按照rubocop提交。

1 个答案:

答案 0 :(得分:2)

  

我有什么方法可以更改它以仅检查更改的代码

重要的是这一行:

# get the list of files which have been modified
git diff-tree -r --name-only

hook sample

#!/bin/sh

# Check to see if this is the first commit in the repository or not
if git rev-parse --verify HEAD >/dev/null 2>&1
then
    # We compare our changes against the previous commit
    against=HEAD^
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Redirect output to screen.
exec 1>&2

# Get the list of updated files
files = $(git diff-tree -r --name-only $against);

#
#... Loop over the files and do whatever you want to do
#   

# personal touch :-)
echo "                                         "
echo "                   |ZZzzz                "
echo "                   |                     "
echo "                   |                     "
echo "      |ZZzzz      /^\            |ZZzzz  "
echo "      |          |~~~|           |       "
echo "      |        |-     -|        / \      "
echo "     /^\       |[]+    |       |^^^|     "
echo "  |^^^^^^^|    |    +[]|       |   |     "
echo "  |    +[]|/\/\/\/\^/\/\/\/\/|^^^^^^^|   "
echo "  |+[]+   |~~~~~~~~~~~~~~~~~~|    +[]|   "
echo "  |       |  []   /^\   []   |+[]+   |   "
echo "  |   +[]+|  []  || ||  []   |   +[]+|   "
echo "  |[]+    |      || ||       |[]+    |   "
echo "  |_______|------------------|_______|   "
echo "                                         "
echo "                                         "
echo "      Your code is bad.!!!               "
echo "      Do not ever commit again           "
echo "                                         "
echo "${default}"

# set the exit code to 0 or 1 based upon your needs
# 0 = good to push
# 1 = exit without pushing.
exit 0;