ktfmt-gradle icon indicating copy to clipboard operation
ktfmt-gradle copied to clipboard

Add task to generate pre commit hook that formats staged changes

Open gpopides opened this issue 1 year ago โ€ข 5 comments

๐Ÿš€ Description

I have added 2 tasks. 1 is intented to be used by the user (ktfmtGeneratePreCommitHook) and 1 internal (ktfmtPreCommitHook).

ktfmtPreCommitHook Acts pretty much the same with the example in the readme. The pre commit hook will use this task to format staged kotlin files. I have added this task in order to avoid asking the user to add the pre commit example that is present in the readme.

To generate the file, the user needs to run gradlew ktfmtGeneratePreCommitHook

The hook will be generated if :

  1. There is a git directory in the root directory of the project
  2. There isnt an existing pre-commit hook in the hooks directory

๐Ÿ“„ Motivation and Context

This PR tries to address this issue about generating a git pre commit hook for running ktformat on commit

๐Ÿงช How Has This Been Tested?

Integration tests + manual tests on a different project.

Tested on Arch linux with Kotlin 2.0.0-Beta4 and Gradle 8.6

๐Ÿ“ฆ Types of changes

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)

โœ… Checklist

  • [x] My code follows the code style of this project.
  • [x] My change requires a change to the documentation.
  • [x] I have updated the documentation accordingly.

gpopides avatar Mar 19 '24 20:03 gpopides

Thanks for sending this over @gpopides The unit tests are failing, can we make them green?

cortinico avatar Mar 19 '24 22:03 cortinico

Thanks for sending this over @gpopides The unit tests are failing, can we make them green?

Sure i will take a look again tomorrow. Locally they are passing but i will make sure it works on CI too!

gpopides avatar Mar 19 '24 22:03 gpopides

CI is still red apparentyl

cortinico avatar Mar 21 '24 00:03 cortinico

Indeed, the last failure was because of a detekt issue which i pushed a change to fix. This makes the macos and ubuntu builds pass but the windows one still fails. Looking at the artifact i see:

Execution failed for task ':ktfmtGenerateGitPreCommitHook'.
  > Cannot access input property 'source' of task ':ktfmtGenerateGitPreCommitHook'. Accessing unreadable inputs or outputs is not supported. Declare the task as untracked by using Task.doNotTrackState(). For more information, please refer to https://docs.gradle.org/8.6/userguide/incremental_build.html#sec:disable-state-tracking in the Gradle documentation.
   > Failed to create MD5 hash for file content.

Unfortunately i don't have a windows machine nor i am that familiar with it. Do you happen to have any insights on why this might happen?

Thanks

gpopides avatar Mar 21 '24 07:03 gpopides

I have added the include option as you suggested and have rebased your newest changes. Is there anything else that i have missed before going forward with this PR?

gpopides avatar Mar 24 '24 19:03 gpopides