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

Uploaded Source

Built Distribution

iprecommit-0.3.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: iprecommit-0.3.0.tar.gz
  • Upload date:
  • Size: 5.8 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.0.tar.gz
Algorithm Hash digest
SHA256 c126ef67df503c88df0128d7fc0e8424baed70f7b4035db457214dba5ff88d8e
MD5 8e9c7aa5ebc6daa11e27fb14c83c688f
BLAKE2b-256 ad5dfc2f4123aa26d60a3fda76e16acdd3787843b46f1bee2615049f33aa2f93

See more details on using hashes here.

File details

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

File metadata

  • Download URL: iprecommit-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7af7dbf06cefb438891de61133496db7daeed65b2eed9df9498365fe5d5b1674
MD5 6d1b02bb6f9347521acd9a4af824bf26
BLAKE2b-256 d1987af6c2b490d76dd9dfdf796b395aad67210657eab374f913792ea9038bee

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