gpt4 book ai didi

git pre-commit 钩子(Hook)代码格式化与部分提交?

转载 作者:bug小助手 更新时间:2023-10-28 10:49:50 40 4
gpt4 key购买 nike

有没有办法让预提交 Hook 自动格式化代码(对于使用 astyle 的示例)但 not 会破坏部分提交吗?

工作流程:

# edit a file.txt
git add -p file.txt
# add one chunk, but not another

git commit -m 'a message'
[PRE_COMMIT_HOOK] Formatting source code

git status
# the "another" chunk is still not added

我的问题是,如果你在 pre-commit 钩子(Hook)中执行 git add ,那就是脚本格式化源代码后需要,添加“另一个” block ,也。但我不想那样。

有没有办法做到这一点?

最佳答案

我会通过使用低级“管道”命令来完成这项工作,我的第一次尝试将类似于

git ls-files --stage \*.c | while read mode object stage path; do
case $mode in
10*)
formatted=`git show $object | indent | git hash-object -w --stdin`
git update-index --cacheinfo $mode $formatted "$path"
;;
esac
done

为避免冗余处理,请按照@torek 的建议从 git diff-index --name-only --diff-filter=AM 输出开始。

关于git pre-commit 钩子(Hook)代码格式化与部分提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22858850/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com