-
Notifications
You must be signed in to change notification settings - Fork 166
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
Use Gradle command exit code as hook exit code #551
Conversation
…e and exiting on gradle fail. This will ensure unstaged changes are always re-applied regardless of whether or not the check or format has failed or passed
Sorry, I don't actually understand the implications of this. Could you elaborate a bit. Although I've been using git for years, my understanding of some of the terminology is somewhat lacking at times. A bit of a simplified explanation here would help me understand what this fix is attempting to resolve. |
@JLLeitschuh Sure thing, I'd be happy to. Apologies if this goes a little too simple, but it's just in the interest of clarity. On staging files in gitLet's say for example that we've made some changes in a repo where we've changed 2 files, git add Foo.kt Now More on staging files can be found here On how staged and un-staged changes are this is used in this git hookSo, now assume that our repo has this plugins pre-commit check git hook installed (just check, not format) and we want to commit the changes we made to The pre-commit check first takes all of the un-staged changes, removes them from the repo and saves them to a file diff=.git/unstaged-ktlint-git-hook.diff
git diff --color=never > $diff
if [ -s $diff ]; then
git apply -R $diff
fi If you were to run Assuming there's no lint violations in if [ -s $diff ]; then
git apply --ignore-whitespace $diff
fi
rm $diff After this block, our changes to On the bug this PR fixesSo, imagine the above scenario again, except this time there's a lint violation in The hook script uses set -e so when the gradle command fails, the hook will stop after the gradle command and will exit with a non-zero error code. This indicates to the git process that the hook has failed the pre-commit check and the staged files should not be committed. But because we use set -e to exit immediately on failure, the remaining lines of the hook script after the gradle command will not run. This means that our changes to At this point they do still exist in the ExamplesTo illustrate this further, here's an example where I've added some additional logging to the script and ran it with and without a lint violation before and after this fix (with local paths for my repo redacted).
I hope that helps. Do let me know if there's anything else I need to add to this change. From the checks, it looks like I need to update the changelog? |
First off, bravo on a very detailed and also very clear description of the issue. If you could figure out a way to encode this description into a comment (or at minimum link to your above comment) from a kotlin doc comment on the test you introduced that would be incredibly useful. Being able to, in the future, ask, "why is this test here the way it is" your explanation would be very useful.
Wow, that does indeed seem bad. Thank you for this fix, let's get this in as soon as we can! Please do add something to the changelog and add some sort of comment on the test you added and I'll be happy to merge this change. I'm hoping to do a release sometime this week. |
Thanks for the feedback! 👍🏽 I've made those updates to the tests and changelog, so this should be good to go now. Would be great to get this into a release soon 🤞🏽 |
Much appreciated! Thanks for this fix! |
Hi, Thanks a lot for this fix. I hop it will be intregated in a version soon. |
The hook was using set -e, which meant that if the gradle command failed, then the saved unstaged changes in the diff file were never re-applied.
This change removes the use of set -e and Instead uses the exit code of the gradle task to indicate the success or failure of the hook after the unstaged changes are re-applied. This will ensure unstaged changes are always re-applied regardless of whether or not the check or format has failed or passed