How does this tied hook for fixation end?

What happens in this pre-commit hook ? I thought that changing the files would cause them to reload.

#!/bin/sh
#
# A git hook script to find and fix trailing whitespace
# in your commits. Bypass it with the --no-verify option
# to git-commit
#

if git-rev-parse --verify HEAD >/dev/null 2>&1 ; then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Find files with trailing whitespace
for FILE in `exec git diff-index --check --cached $against -- | sed '/^[+-]/d' | sed -r 's/:[0-9]+:.*//' | uniq` ; do
    # Fix them!
    sed -i 's/[[:space:]]*$//' "$FILE"
done

# Now we can commit
exit

I think the idea is to remove trailing spaces in all files that it commits.

+3
source share
3 answers

Except that this does not work. I tried the following at the end of my bindings before committing:

exec git diff-index --check --cached $against --

but changes made to these hooks are still not made (at least in git 1.7.3.4).

If you really want changes to come in, you must explicitly

git add "$file"

for each file that you changed during the pre-commit phase.

+3

, :

  • , ( )
  • , pre-commit

git diff-index

, , .

exec git diff-index --check --cached $against --

--cached:

, .

commit.c:

static int prepare_to_commit(const char *index_file, const char *prefix,
                 struct wt_status *s)
{
...

    if (!no_verify && run_hook(index_file, "pre-commit", NULL))
        return 0;
...


/*
 * Re-read the index as pre-commit hook could have updated it,
 * and write it out as a tree.  We must do this before we invoke
 * the editor and after we invoke run_status above.
 */
discard_cache();
read_cache_from(index_file);
+2

, script.

. , . : https://github.com/addonszz/Galileo/tree/master/githooks

Then you simply replace the algorithm for replacing the version file with the updateVersion.sh file with your "Trilling Spaces" algorithm. Perhaps you need to change a few things, for example, remove the branch restriction, because there the script only works if you are on a development branch.

In addition, it will only modify the file if it is supplied. If the file is not supplied, it will do nothing. More precisely, he prints what he does at every step.

0
source

Source: https://habr.com/ru/post/1741975/


All Articles