Skip to main content

Dead-simple Git pre-commit hooks

Project description

A simple tool to manage pre-commit hooks for Git.

Install it with pip:

pip install iprecommit

Then, initialize a pre-commit check in your git repository:

cd path/to/some/git/repo
iprecommit init

iprecommit init will create a file called hooks/precommit.py, and install it as a Git pre-commit check. You can customize the location with the --hook flag.

Now, whenever you run git commit, the checks in precommit.py will be run automatically. You can also run the pre-commit checks manually:

iprecommit run

Some pre-commit issues can be fixed automatically. To do so, run

iprecommit fix

By default, iprecommit run and iprecommit fix operate on both staged and unstaged changes. To only consider staged changes, pass the --staged flag. (Note that the real pre-commit check only looks at staged changes.)

User guide

Precommit file format

The precommit.py file that precommit generates will look something like this:

from iprecommit import Precommit, checks

pre = Precommit()
pre.check(checks.NoDoNotSubmit())
pre.check(checks.NewlineAtEndOfFile())
pre.command(["black", "--check"], pass_files=True, pattern=["*.py"])

iprecommit comes with some built-in checks, such as NoDoNotSubmit() and NewlineAtEndOfFile(). You can also use pre.command(...) to define your own checks based on shell commands. These checks will pass as long as the shell command returns an exit code of 0.

By default, pre.command(...) will just invoke the command. If you need to pass file names to the command, specify pass_files=True. Only changed files will be passed. You can constrain the files to be passed using pattern, which takes a list of glob patterns interpreted the same way as fnmatch.fnmatch, and exclude.

Writing your own checks

iprecommit comes with some useful checks out of the box, but sometimes you need to write your own checks. Doing so is straightforward.

Checks are Python classes that inherit from BaseCheck. They must provide a single function, check, which takes a parameter of type Changes and returns a list of Message objects.

  • The Changes object has three fields: added_files, modified_files, and deleted_files, each of which is a list of Path objects.
  • The Message object has two fields: message and path. Currently, path is not printed, so if the exact file path is important, you should include it in the human-readable message field so that the user knows what file it is talking about.

check should return one Message for each failure it identifies. If it returns an empty list, the pre-commit check is considered to have passed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iprecommit-0.2.2.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

iprecommit-0.2.2-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file iprecommit-0.2.2.tar.gz.

File metadata

  • Download URL: iprecommit-0.2.2.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.5 Darwin/22.6.0

File hashes

Hashes for iprecommit-0.2.2.tar.gz
Algorithm Hash digest
SHA256 106b97c91caa88c3adf15e4d10df14e1e3ee4af61c8af11bb1c6f9b5724bf05d
MD5 ce186cb7fffba00f2fea0ea4d63c65f3
BLAKE2b-256 354bd22f7893e80298962af5a9f1bd86f29bae05fcc0a0927901ca6354a2a396

See more details on using hashes here.

File details

Details for the file iprecommit-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: iprecommit-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.5 Darwin/22.6.0

File hashes

Hashes for iprecommit-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e981c4e0708d887185195adb332852c4d827ffc2ced9a7139146770571a90df7
MD5 0e0d1b39963a2f0ab6a81cd86a46182f
BLAKE2b-256 61a4f73f9ee32f41265f4be4d8d4bc54c2bb4204ddd896dc61536fd5d1e8c057

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page