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

Unstaged modification lost when running git hook

Open ecabestan opened this issue 3 years ago • 4 comments

Hi,

After running precommit hook, I have lost my unstaged modification :-(

It occurs when you have unstaged modification and ktlint detect an issue in the staged files. The pre-commit remove modification and doesn't re-apply them as ktlint returns an error

The issue is in the pre-commit hook and seems to be fixed by Use Gradle command exit code as hook exit code.

The issue come from here : Resolve pre-commit hook causing conflicts

Do you think it is possible to create new version with this fix ? I think it can save work of many peoples :-)

I think that a better solution should be found : remove the modification in a pre-commit hook is a big risk ! The stash used before the regression has the advantage to not lost the modification. It is the first check I have done when I lost my modifications.

ecabestan avatar Dec 15 '21 09:12 ecabestan

I believe your changes may just be stashed, so you can theoretically get them back by unstashing your changes. Sorry about this. I'll see if we can get a release out this week.

JLLeitschuh avatar Dec 21 '21 21:12 JLLeitschuh

Sadly, the changes are not stashed. It is the first check I have done In the previous release a stash was done in the hook. It has been replaced by a diff apply -r.

Thanks for the answer.

ecabestan avatar Dec 21 '21 22:12 ecabestan

Now with the git apply -R, your can recover your lost changes with:

git apply --ignore-whitespace .git/unstaged-ktlint-git-hook.diff

Might be useful for anybody who is affected by this issue.

Should the script not also handle the SIGINT signal (ctrl+c)?

JeroenErikJensen-TomTom avatar Jan 12 '22 16:01 JeroenErikJensen-TomTom

Thanks @JeroenErikJensen-TomTom for that tip.

fyi, The latest release (10.2.1) also has a bug in it. It is fixed by Fix pre-commit hook command not found error.

JonathanDeLeon avatar Jan 21 '22 03:01 JonathanDeLeon