Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--to-ref: how to indicate worktree/index as reference? #2058

Closed
rogalski opened this issue Sep 21, 2021 · 1 comment
Closed

--to-ref: how to indicate worktree/index as reference? #2058

rogalski opened this issue Sep 21, 2021 · 1 comment
Labels

Comments

@rogalski
Copy link

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 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 --version

pre-commit 2.15.0

.pre-commit-config.yaml

repos:
  - repo: local
    hooks:
      - id: env
        name: env
        entry: env
        language: system
        pass_filenames: false
        verbose: true

~/.cache/pre-commit/pre-commit.log (if present)

No response

@asottile
Copy link
Member

You can consider flake --diff as good real-life example.

flake8 --diff is broken by design -- I would not recommend emulating it

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants