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 template
iprecommit install

iprecommit template will create a file called precommit.py, and iprecommit install will install it as a Git pre-commit check.

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:

iprecommit fix

By default, iprecommit run and iprecommit fix operate only on staged changes. To only consider unstaged changes as well, pass the --unstaged flag.

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.sh("black", "--check", pass_files=True, base_pattern="*.py")

iprecommit comes with some built-in checks, such as NoDoNotSubmit() and NewlineAtEndOfFile(). You can also use pre.sh(...) 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.

You can also define your own checks in Python:

class NoTypos(checks.Base):
    typos = {
        "programing": "programming"
    }

    def check(self, changes):
        for path in changes.added_paths + changes.modified_paths:
            text = path.read_text()
            for typo in self.typos:
                if typo in text:
                    return False
            
        return True
    
    def fix(self, changes):
        for path in changes.added_paths + changes.modified_paths:
            text = path.read_text()
            for typo, fixed in self.typos.items():
                text = text.replace(typo, fixed)

            path.write_text(text)

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.3.1.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

iprecommit-0.3.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for iprecommit-0.3.1.tar.gz
Algorithm Hash digest
SHA256 2be8c5933a69537aac6c46e0a063fa66de342c69c3ea0cfbd6bf61bb6dc95809
MD5 60c4183cb1893ddc9dd56b02328c54c4
BLAKE2b-256 e3cba36b3be72c6c1c2f8c945db8a05f62bcb456c367cabd1eb1a1aef557defa

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for iprecommit-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fbdccc183ee1afd2c7e43318bfd63b81aa708a7375e00d0e18101e558ba80165
MD5 5efd780cd824ad1a35ed1e0984631f12
BLAKE2b-256 ac61e08326a0d3ce2a7cf9e0f5954f34753bd5eeec4d15ed509a9102a7c2a789

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