Skip to content

iafisher/iprecommit

Repository files navigation

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

iprecommit comes with some built-in checks, such as NoDoNotCommit() 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)