0

I used pre-commit to check my python code style before I commit to git. It works very well. But now I am working on an old project, most of my work just modify several lines of the file. But pre-commit always ask me fix all the pep8 issues of the file.

is there a way that let the pre-commit only check the codes I just modified not the whole file?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Gary Wang
  • 81
  • 1
  • 1
  • 4

1 Answers1

1

there is not in general. pre-commit operates on the file level and does not parse the underlying tool's output in any way

if an underlying tool supports partial changes then you can use that individual tool's features, but doing so at the framework level is difficult, error prone (there is no standardization in tool output), and most of all slow (re-parsing output and diffs is not cheap)

when adapting to an older code base, you're best to take a first pass which improves the code quality and then enable your linting tools. often you can leverage a code formatter such as autopep8 or black to make adoption easier


disclaimer: I'm the author of pre-commit

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • thank you very much. Because our code base is bit, improve all the files is too risky, I will try to find other ways. – Gary Wang May 15 '20 at 10:14
  • you can periodically opt in by using [top-level `files:`](https://pre-commit.com/#pre-commit-configyaml---top-level) if you don't want to do it all at once (though usually doing a gradual rollout will cause _more_ merge conflicts!) – anthony sottile May 15 '20 at 14:41