我有一个预提交的钩子,它像这样运行一些棉绒:
./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
这里的问题是,如果ktlint任务失败,但是自动格式可以解决所有问题,我无法重新添加仅包含在最初提交中的文件。
也许最好用一个例子来说明:
谢谢!
答案 0 :(得分:1)
在提交之前,预提交挂钩在代码库上运行。我建议在自动修复完成后删除git add/commit
行,以便脚本以零(成功)状态退出。
您失去了添加到邮件中的功能,但是预提交将完全按照应有的方式运行。
与您的示例进行比较:
✌️</ p>
答案 1 :(得分:0)
解决方案是跟踪预先提交的文件,然后在自动执行lint格式化后手动添加文件。
echo "${yellow}Running linting...${reset}"
#store the result of the commit in a variable and split into array items with newline
committedFiles=$(git diff --name-only --cached)
files=$(echo $committedFiles | tr ";" "\\n")
[...]
#after ktlintFormat runs and succeeds
echo "${green}Fixed all issues automatically. Committing..! :)${reset}"
#replay items in the commits array and add only those files
for file in $files
do
git add $file
done
git commit -m "Automatic commit of linted files" --no-verify
exit 0
[...]