Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git precommit-hook: check whether line content has changed

Szenario: On file commit with git I want to check within pre-commit hook whether a certain line has been modified. Therefore I want to compare parts of a line from the modified file with the corresponding linepart from the repository.

Background: I want to ensure that the file to commit has another version number than the file already in the repository. The user should be enforced to give the file an appropriate version number. Automatically increasing the version number on commit is not an option here, as we use multipart version (1.0.0.0), and the user has to modify the correct part of the version string ...

Question: Is it possible to access the file contents of the modified as well as the repository file within pre-commit hook?

like image 242
hoppfrosch Avatar asked Mar 13 '23 18:03

hoppfrosch


1 Answers

The answer is yes.

But - pre commit hook is a client side hook which can be removed o deleted, this is why its better to do it with server side hook

Here is a sample hook to check if the desired file was modified. Once you know if the file was committed use this:

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

Full sample code:

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;
like image 73
CodeWizard Avatar answered Mar 15 '23 07:03

CodeWizard