1

我已经在我的存储库上应用了一个预提交挂钩,我想要做的是使用 rubocop 检查语法,但仅限于我尝试提交的更改。

目前,预提交正在使用 rubocop 检查我已修改的所有文件的样式和规则,并且由于某些旧代码根据 rubocop 无效,因此不允许我提交。

有什么办法可以更改它以仅检查已更改的代码并允许提交是否该代码根据 rubocop 正确。

4

1 回答 1

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;
于 2016-03-02T12:59:47.970 回答