2

I am using php-cs-fixer and decided to try to put the command in pre-commit hook. #!/bin/sh

echo "php-cs-fixer pre commit hook start"

SRC="src/Controller/Api/FlowsController.php"

exec tools/php-cs-fixer/vendor/bin/php-cs-fixer fix $SRC

echo "php-cs-fixer pre commit hook finish"

So the php-cs-fixer command works fine, but the changes are not commited after it. I also don't see the final echo statement when the hook is done. What I see on committing is:

toma.tomov@MBP-TOMA myproject % git commit -m "test"
php-cs-fixer pre commit hook start
Loaded config default from "/Users/toma.tomov/Desktop/projects/myproject/.php-cs-fixer.dist.php".
Using cache file ".php-cs-fixer.cache".
Paths from configuration file have been overridden by paths provided as command arguments.
   1) src/Controller/Api/FlowsController.php

Fixed all files in 2.028 seconds, 26.000 MB memory used
[PHP-CS-FIXER 9550f90a22] test
 1 file changed, 2 insertions(+)

2 Answers 2

6

Pre-commit hooks in general should not try to change what will be committed. Depending on how you invoke Git, they maybe unable to change what will be committed. A pre-commit hook should therefore only verify that what you plan to commit is correct (according to whatever correctness rules you'd like to enforce).

There are some exceptions to the above rules, but you must know a great deal about Git's internal operation before you break these rules. If you'd like to, e.g., reformat or otherwise fix up files before committing, you should do this as a separate step.

Hint: don't run git commit at all. Run your own program, which runs the file-fixing step, then runs git add, then runs git commit. You can retain the checking hooks (which verify that what's to be committed looks right, then allows or denies the commit) and since the file-fixing step has run before the git add step, everything should always pass.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah, think pre-commit hooks as "code-format-validator" not "code-formatter".
0

Imagine you have a pre-commit hook that does this:

  1. Creates or modifies a file
  2. runs git add file

It will work IF AND ONLY IF, there was already something to be commited. It does not have to be the same file tho. If there was nothing to be commited, the git add git add file will work, but will not be commited.

Example that works:

  1. You changed a file, and added it (for example, the gitignore)
$> git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   .gitignore
  1. Run commit. First, the pre-commit hook will run and create file. Then it will commit.
--- Dataset Pre-Commit Hook ---
--- Done ---
[master (root-commit) 2ba2f7c] First commit
 3 files changed, 34 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 file
  1. You can see the message says file was commited. And git status now outputs:
On branch master
nothing to commit, working tree clean

Example that DOES NOT work

  1. You have nothing to add. You can of course run git add but it will do nothing. git status gives:
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
  1. Run commit. The hook will create file. But the commit message says he did nothing.
--- Dataset Pre-Commit Hook ---
--- Done ---
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
  1. Verify the status git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   file
  1. You can see that file was indeed modified, and that it was added, but yet not commited. You need to run git commit again for it to work.

I tried adding that as a post-commit hook but got no luck. If someone knows a fix for this case, I would love to know.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.