Questions tagged [pre-commit]

Pre-commit is an action taken prior to committing your code into a version control system.

A pre-commit is an action that would be applied to source code before it is committed to the version control system. To utilize a pre-commit action you'll need to install a pre-commit hook into the repository. Pre-commit hooks are available in most commonly used version control systems such as SVN or git.

Some actions that could be achieved using pre-commit hooks:

  1. Fixing case issues in file names
  2. Validating file content, such as executing a linter or code convention checker.
  3. Preventing specific users from committing code

pre-commit is also the name of a framework for managing such hooks for git.

406 questions
19
votes
2 answers

pylint and pre-commit hook unable to import

My project structure looks like this: project/ app/ main.py venv/ .pylintrc .pre-commit-config.yaml When I am trying to edit project/app/main.py and it fails with Unable to import 'psycopg2' (import-error) But when I am trying to…
palkan
  • 321
  • 2
  • 5
18
votes
1 answer

VSCode integrated source control and pre-commit

When using https://pre-commit.com with VSCode hooks that depend on packages installed in a Python venv. In pre-commit on can specify to use "system" as environment. This works great from the terminal with desired venv active. However using the…
Martin Gran
  • 241
  • 1
  • 2
  • 5
18
votes
1 answer

Code Review before checking in to TFS 2013

I'm trying to implement a process so that the manager can Review the code of all developers before the developers can check in to TFS 2013. Is there any process to require a review of the code by a human being before it is added to a real…
Amaan Khan
  • 181
  • 2
  • 3
  • 15
18
votes
4 answers

How can I run git pre-commit checks only on staged content?

Suppose git status gives this: # On branch X # Changes to be committed: # (use "git reset HEAD ..." to unstage) # # modified: file1.cc # modified: file1.h # modified: file1_test.cc # modified: SConscript # # Changes not…
user334856
16
votes
2 answers

pre-commit fails to install isort 5.10.1 with error "RuntimeError: The Poetry configuration is invalid"

[INFO] Installing environment for https://github.com/pycqa/isort. [INFO] Once installed this environment will be reused. [INFO] This may take a few minutes... An unexpected error has occurred: CalledProcessError: command:…
LOTEAT
  • 181
  • 1
  • 4
16
votes
5 answers

Testing what is about to be committed in a pre-commit hook

When a pre-commit hook runs, the repository might not be clean. So if you naively run your tests, they will not be against what you're committing, but whatever happens to be in your working tree. The obvious thing to do is to git stash --keep-index…
pwaller
  • 601
  • 6
  • 10
15
votes
2 answers

Stop a git commit by a specific author using pre-commit hook

My plan is to use git to keep track of changes in /etc but when committing I want to have the person making the change specify themselves as author by adding the --author option on the commandline. So I would like to stop accidental commits as…
Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
15
votes
2 answers

running pytest as a pre-commit hook // no such file or directory issue

in my Python project I use pytest as a pre-commit hook. Some tests create and delete temporary files, everything works fine when I run pytest . However, when I run git commit and pre-commit hook triggers pytest, some tests fail…
15
votes
1 answer

With pre-commit, how to use some hooks before commit and others before push

Some hooks can take a while to run, and I would like to run those before I push, but not before each particular commit (for example, pylint can be a bit slow). I've seen the following: Question: Using hooks at different stages mesos-commits mailing…
baxx
  • 3,956
  • 6
  • 37
  • 75
14
votes
2 answers

Run pre-commit only on certain branches

I am looking for a configuration in the .pre-commit-config.yaml to exclude pre-commit from being run on certain branches, or to run it only on some branches. I don't know if this feature not implemented, or if I am missing it in the docs. Thanks!
13
votes
2 answers

Is it possible to run mypy pre-commit without making it fail?

I would like to add the following to pre-commit for a team: - repo: https://github.com/pre-commit/mirrors-mypy rev: 'v0.720' hooks: - id: mypy args: [--ignore-missing-imports] My team is worried that this might be too…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
13
votes
2 answers

Git pre-commit hook: getting list of changed files

I am developing validation and linting utility to be integrated with various commit hooks, including Git one https://github.com/miohtama/vvv Currently validators and linters are run against the whole project codebase on every commit. However, it…
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
12
votes
2 answers

Simplifying line length with pre-commit, flake8, black, isort, pylint, etc

When using multiple tools that either check or format python files, is there a way to set line length once for all? Currently I have: .flake8 file: max-line-length = 120 .isort.cfg file: line-length = 120 .black file: line-length = 120 .pylintrc…
Marjeta
  • 1,111
  • 10
  • 26
11
votes
1 answer

How to ensure that all pre-commit hooks pass in CI/CD

My team uses Pre-commit in our repositories to run various code checks and formatters. Most of my teammates use it but some skip it entirely by committing with git commit --no-verify. Is there anyway to run something in CI/CD to ensure all…
Johnny Metz
  • 5,977
  • 18
  • 82
  • 146
11
votes
1 answer

Pre-commit flake8 with setup.cfg in subfolder

I use flake8 with a bunch of plugins (flake8-docstrings, flake8-isort, flake8-black). I have them all pre-installed into a venv. My repo to be checked with pre-commit: Root folder has two packages Each has its own pyproject.toml (configures black…
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
1
2
3
27 28