You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am currently experimenting with diff-based pre-commit checks.
The general idea is to run some tools on commit, but filter out its' outputs based on diff between HEAD and index.
You can consider flake --diff as good real-life example.
At the same time, I am trying to expose easy reproduction steps for people who (for any reason) were committing without hook installed or with --no-verify enabled. Taking a look at docs, I think that proper approach is to use --from-ref, --to-ref. With GitHub PR flow, from-ref would take value of git merge-base HEAD TARGET_BRANCH and to-ref would just be HEAD. This is exactly what docs recommends, so far so good.
The problem I see is that if some files are staged but not committed, I can have a discrepancy between file system (which is where all linters actually operate) and diff data (which is used to build affected file list and then to postprocess linter outcome).
Of course I can add arbitrary limitation that no delta between --to-ref and index is allowed when --from-ref and --to-ref is used. At the same time, somehow exposing --to-ref=git_index seems to be a desirable feature, consistent with how static analysis tools actually operate on checked-out codebase. Currently this is not possible, as --to-ref has to be a valid revision.
I am most likely missing something important in my understanding of git semantics or in how hooks generalize outside of pre-commit hook. Yet, I still want to discuss possible approaches.
pre-commit intentionally operates on a per-file granularity. I would not recommend trying to build diff-based linters unless you're operating specifically on a per-file basis (which is already what pre-commit does)
describe your issue
I am currently experimenting with diff-based pre-commit checks.
The general idea is to run some tools on commit, but filter out its' outputs based on diff between
HEAD
and index.You can consider
flake --diff
as good real-life example.At the same time, I am trying to expose easy reproduction steps for people who (for any reason) were committing without hook installed or with
--no-verify
enabled. Taking a look at docs, I think that proper approach is to use--from-ref
,--to-ref
. With GitHub PR flow, from-ref would take value ofgit merge-base HEAD TARGET_BRANCH
and to-ref would just beHEAD
. This is exactly what docs recommends, so far so good.The problem I see is that if some files are staged but not committed, I can have a discrepancy between file system (which is where all linters actually operate) and diff data (which is used to build affected file list and then to postprocess linter outcome).
Of course I can add arbitrary limitation that no delta between
--to-ref
and index is allowed when--from-ref
and--to-ref
is used. At the same time, somehow exposing--to-ref=git_index
seems to be a desirable feature, consistent with how static analysis tools actually operate on checked-out codebase. Currently this is not possible, as--to-ref
has to be a valid revision.I am most likely missing something important in my understanding of git semantics or in how hooks generalize outside of pre-commit hook. Yet, I still want to discuss possible approaches.
pre-commit --version
pre-commit 2.15.0
.pre-commit-config.yaml
~/.cache/pre-commit/pre-commit.log (if present)
No response
The text was updated successfully, but these errors were encountered: