【问题标题】:git precommit-hook: check whether line content has changedgit pre commit-hook:检查行内容是否改变
【发布时间】:2016-03-31 07:56:47
【问题描述】:

Szenario: 在使用 git 提交文件时,我想在预提交挂钩中检查某行是否已被修改。因此,我想将修改后文件中的一行部分与存储库中相应的行部分进行比较。

背景:我想确保要提交的文件的版本号与存储库中已有的文件不同。应强制用户为文件提供适当的版本号。在提交时自动增加版本号不是一个选项,因为我们使用多部分版本(1.0.0.0),并且用户必须修改版本字符串的正确部分......

问题:是否可以在 pre-commit 挂钩中访问修改后的文件内容以及存储库文件?

【问题讨论】:

    标签: git pre-commit-hook pre-commit


    【解决方案1】:

    答案是肯定的。

    但是 - pre commit hook 是一个客户端钩子,可以删除或删除,这就是为什么最好使用服务器端钩子来完成它

    这是一个示例挂钩,用于检查所需文件是否已被修改。 一旦您知道文件是否已提交,请使用:

    # you have the commit id so you can checkout the given file
    git show <commit id>:<full path to file>
    

    完整示例代码:

    pre-receive hook

    #!/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
    
    # Check to see if we have updated the given file
    if [ $(git diff-tree -r --name-only $against | grep <ANY FILE YOU WANT TO FIND OUT HERE> ) ];
    then
    
        # you have the commit id so you can checkout the given file
        # the commit is: git rev-parse HEAD
        git show <commit id>:<full path to file>
    
        # personal touch :-)
        echo "                                         "
        echo "                   |ZZzzz                "
        echo "                   |                     "
        echo "                   |                     "
        echo "      |ZZzzz      /^\            |ZZzzz  "
        echo "      |          |~~~|           |       "
        echo "      |        |-     -|        / \      "
        echo "     /^\       |[]+    |       |^^^|     "
        echo "  |^^^^^^^|    |    +[]|       |   |     "
        echo "  |    +[]|/\/\/\/\^/\/\/\/\/|^^^^^^^|   "
        echo "  |+[]+   |~~~~~~~~~~~~~~~~~~|    +[]|   "
        echo "  |       |  []   /^\   []   |+[]+   |   "
        echo "  |   +[]+|  []  || ||  []   |   +[]+|   "
        echo "  |[]+    |      || ||       |[]+    |   "
        echo "  |_______|------------------|_______|   "
        echo "                                         "
        echo "                                         "
        echo "      You have just committed code       " 
        echo "      Your code is bad.!!!               "
        echo "      Do not ever commit again           "
        echo "                                         "
    fi;
    
    # set the exit code to 0 or 1 based upon your needs
    # 0 = good to push
    # 1 = exit without pushing.
    exit 0;
    

    【讨论】:

    • 据我了解建议的答案,您只需检查专用文件是否已更改。我的问题是关于检查文件中的某一行是否已更改 - 因为该文件也可能包含许多其他更改...
    • 这就是为什么我提到如何获取文件的内容(show 命令),然后用它做任何你想做的事情(sed、awk、grep 等)。这是一个完整的答案,但我不知道你在寻找什么字符串,这就是你需要做的全部
    • 我不只是给你一个我正在使用的脚本,我给你一个完整的解释以及它是什么以及它是如何工作的。
    • 我明白了——我没有正确理解“git show”的含义。感谢您的回复。我会尽快测试一下
    猜你喜欢
    • 1970-01-01
    • 2015-08-19
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2013-02-19
    • 1970-01-01
    相关资源
    最近更新 更多