I have a pre-commit hook that runs some linting like so:
./gradlew app:ktlint --daemon
status=$?
if [ "$status" = 0 ]
then
echo "${green}Linting found no problems.${reset}"
exit 0
else
echo 1>&2 "${red}Linting found issues.${reset}"
echo "${yellow}Attempting to fix automatically...${reset}"
./gradlew app:ktlintFormat --daemon
if [ $? = 0 ]
then
echo "${green}Fixed all issues automatically. Committing automagically...! :)${reset}"
git add .
git commit -m "Automatic commit of linted files" --no-verify
exit 0
else
echo "${red}Could not fix all issues automatically, please review. :( ${reset}"
exit 1
fi
fi
The issue here is that if the ktlint task fails, but the automatic format manages to fix all the problems, i have no way of re-adding only the files that were included in the initial commit.
Perhaps this is best explained with an example:
- I have 3 files, A,B,C
- I commit A & B, but not C
- ktlint fails the commit due to formatting
- ktlintFormat manages to fix the issues
- Current behaviour: Formatted files A & B but also C are added and committed automatically
- Wanted behaviour: Only the formatted A & B files are added and committed automatically
Thanks in advance!