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

Make git hooks run ktlint on changed files only #1339

Closed
francescocervone opened this issue Jan 20, 2022 · 2 comments · Fixed by #1336
Closed

Make git hooks run ktlint on changed files only #1339

francescocervone opened this issue Jan 20, 2022 · 2 comments · Fixed by #1336

Comments

@francescocervone
Copy link
Contributor

It would be great if the git hooks provided by ktlint via installGitPreCommitHook/installGitPrePushHook were made for executing on git diff only.

Expected Behavior

ktlint git pre-commit and pre-push hooks run on changed files only.

Current Behavior

ktlint git pre-commit and pre-push hooks run on all files of the current directory.
For instance, when I install the pre-commit hook via ktlint installGitPreCommitHook, it looks like this:

git diff --name-only --cached --relative | grep '\.kt[s"]\?$' | xargs ktlint --relative .

As you can see, the first command lists changed files, then only kt[s] files are selected, and finally they are piped into the ktlint command. Unfortunately, last argument of the ktlint command is the current directory, meaning that in addition to changed files, also the entire current directory is going to be checked. Basically, the current directory "overrides" the piped git diff files.

Additional information

  • Current version of ktlint: 0.43.2
  • Looks like ktlint's git hooks have never changed, but I observed a performance decrease from version 0.40 to 0.41 and higher (see 0.41.0 Performance decrease #1135 (comment)). This performance decrease brought me here.
@adriafernandez
Copy link

Hi there! Has someone achieved this behavior??

@paul-dingemans
Copy link
Collaborator

Have you tried to generate new git hooks with latest version of Kltint CLI:

git diff --name-only -z --cached --relative -- '*.kt' '*.kts' | ktlint --relative --patterns-from-stdin=''

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

Successfully merging a pull request may close this issue.

3 participants