My new ebook “Comprehending Comprehensions” is on pre-sale and 40% off!

Today I learned how to run the black Python code formatter as a pre-commit hook on git.

A picture of a hook from a construction crane.
Photo by Grace To on Unsplash.

Pre-commit hooks

Pre-commit hooks are scripts that git runs right before you commit something. The idea is that you will install scripts that help verify that, whatever is about to be committed, is fine.

Pre-commit hooks are just a subset of all the hooks that git can run for you. You can (and probably should) check the documentation on this subject, or you might want to check the .git/hooks directory of a git repository you have lying around. What you will notice is that the hooks directory, that was created automatically by git, contains a series of files ending with .sample that represent samples of the various hooks that you can configure.

The hook scripts, by the way, can be any executable you want. For example, they can be Python scripts with a shebang, making them executable as well.

Formatting Python code with black

If you want to make sure your Python project has its code formatted according to black's recommendatitons, you can define your pre-commit hook to run black on your code before the code is committed! This would ensure that no code is committed that isn't formatted properly.

In order to create this pre-commit hook, just go to .git/hooks and create a file called pre-commit, then put this in:

#!/bin/sh
exec black . --check

That's it, that's more than enough! Of course, you can get all sorts of fancy and configure black in a non-standard way, run the code-formatting check on a specific directory, or actually tell black to format the code, instead of checking if the formatting is appropriate.

Quite cool, right?

That's it for now! Stay tuned and I'll see you around!

I hope you learned something new! If you did, consider following the footsteps of the readers who bought me a slice of pizza 🍕. Your small contribution helps me produce this content for free and without spamming you with annoying ads.

References

Previous Post Next Post

Blog Comments powered by Disqus.