【问题标题】:Verify only changes in modified files with pre-commit instead verifying whole file仅使用预提交验证已修改文件中的更改,而不是验证整个文件
【发布时间】:2016-03-02 12:37:06
【问题描述】:

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

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

我有什么办法可以更改它以仅检查已更改的代码,如果该代码按照 rubocop 正确,则允许提交。

【问题讨论】:

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


    【解决方案1】:

    有什么办法可以改变它以仅检查已更改的代码

    重要的是这一行:

    # 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;
    

    【讨论】:

    • 我认为这个问题已经假设了。但是如何只检查用 rubocop 更改的行?
    猜你喜欢
    • 2021-01-24
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    相关资源
    最近更新 更多