Question


Disclaimer: yes, I've read Skip Git commit hooks and I know about the -n flag, but this is not what I'm looking for.


I have that pre-commit hook that runs in order to do some code prettifying (replace tabs with spaces, remove double empty lines, ...).

However, there are some use cases where I don't want the hook to run: for example, when I'm checking in some Makefiles, I cannot simply replace tabs with spaces, so I'd have to skip these files.

If I were the only developer, I would be fine with the -n flag in order to prevent the hook from being run, but there are other guys that use UIs that don't know the -n flag (cough tortoisegit) and they won't be willing to specify some flags to commit.

Apart of implementing the skipping directly in the hook and the obvious -n way: are there any other ways how I could prevent the pre-commit hook to be run?

Was it helpful?

Solution

Following a similar idea as this pre-commit hook, you could use:

git diff --cached --name-only

That would list the files that are about to be committed, and you can then filter on them in order to apply your controls or to skip those files.

In other words, the hook is still running on every commit, for every files.
But its script is smart enough to apply its test to the right files within the list of files to be committed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top