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

Git hooks: default patterns are used if no Kotlin files have changed #1793

Closed
mfederczuk opened this issue Jan 27, 2023 · 2 comments
Closed
Milestone

Comments

@mfederczuk
Copy link
Contributor

This is a follow up to my PR #1606, concerning an oversight on my part.

In the new Git hooks, if git diff doesn't report any Kotlin files changed (which happens either because no Kotlin files are staged (pre-commit), or because no Kotlin files have been changed between HEAD and the remote branch (pre-push)) then ktlint will fall back to the default patterns, which match every file in the project — tracked in VC or not.

For some projects, this is undesirable, either because the codebase contains a lot of legacy code that isn't properly formatted or because the project contains generated code that doesn't conform to style conventions.

I have a couple ideas on how to solve this issue:

  1. Add yet another new option --no-default-patterns. Then the hooks could be updated to:

    git diff --name-only -z --cached --relative -- '*.kt' '*.kts' | ktlint --relative --patterns-from-stdin='' --no-default-patterns
  2. Disable the default patterns if the option --patterns-from-stdin is used, even if no patterns are given

  3. Remove the default patterns outright

  4. Reintroduce the xargs --no-run-if-empty hack:

    if [ "$(uname -s)" != "Darwin" ] then
        no_run_if_empty=--no-run-if-empty
    fi
    
    git diff --name-only --cached --relative -- '*.kt' '*.kts' | xargs $no_run_if_empty ktlint --relative

    Though this would also reintroduce the issues with that hack:

    • Pathnames with whitespace and/or special characters in it won't be found by ktlint (due to either xargs splitting them or git diff quoting them)
    • --no-run-if-empty is an extension of GNU xargs

For anyone else having this problem, a quick and dirty workaround for now would be to simply always give at least one file on the command line. For example:

git diff --name-only -z --cached --relative -- '*.kt' '*.kts' | ktlint --relative --patterns-from-stdin='' 'settings.gradle.kts'
@paul-dingemans
Copy link
Collaborator

At first glance, option 2 seems most attractive to me. Adding default patterns doesn't make sense in case you want to get them from stdin (regardless whether stdin is empty or not) anyways.

paul-dingemans added a commit that referenced this issue Feb 13, 2023
…iven (#1801)

Fixes issue #1793.

Co-authored-by: paul-dingemans <paul-dingemans@users.noreply.github.com>
@paul-dingemans
Copy link
Collaborator

Solved by #1801

@paul-dingemans paul-dingemans added this to the 0.49.0 milestone Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants