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
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:
Add yet another new option --no-default-patterns. Then the hooks could be updated to:
Disable the default patterns if the option --patterns-from-stdin is used, even if no patterns are given
Remove the default patterns outright
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:
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.
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 betweenHEAD
and the remote branch (pre-push
)) thenktlint
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:
Add yet another new option
--no-default-patterns
. Then the hooks could be updated to:Disable the default patterns if the option
--patterns-from-stdin
is used, even if no patterns are givenRemove the default patterns outright
Reintroduce the
xargs --no-run-if-empty
hack:Though this would also reintroduce the issues with that hack:
ktlint
(due to eitherxargs
splitting them orgit diff
quoting them)--no-run-if-empty
is an extension of GNUxargs
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:
The text was updated successfully, but these errors were encountered: