【发布时间】:2015-09-12 11:36:36
【问题描述】:
我们目前正在使用 git 挂钩(如下)在允许用户提交之前在我们的源代码上运行样式。这有一个警告,用户必须提交,格式化他们的代码,然后再次提交,这有点麻烦。理想情况下,我们希望钩子格式化代码,然后将格式化的代码包含在原始提交中。我尝试重新添加更改的文件,但它会导致 ref 错误(显然)。我还尝试在 pre-commit 钩子中获取历史记录并尝试退出钩子并重新运行 git commit 命令,但没有成功。
# Run astyle on changed .cs files, ignoring $ignored
res=$(exec git diff --cached --name-only | \
grep -Ev $ignored | \
xargs astyle --options=conf/astylerc | \
tail -n 1)
num_formatted=$(echo $res | cut -b 1) # We are only interested in the number preceeding 'formatted'.
[[ $num_formatted -ne 0 ]] && echo "WARNING: Code has been automatically formatted. Please re-add and re-commit" && exit 1 || echo "No code to format! Continuing commit"
有人有什么想法吗?
【问题讨论】:
-
为什么不在提交前运行预提交挂钩并格式化代码?
-
这是一个预提交钩子。只是我的开发人员需要在文件格式化后再次运行 git add 和 git commit 。所以它看起来像: git add file.cs git commit -m "msg" 警告:代码格式化 git add file.cs git commit -m "msg again" 这很烦人。我更喜欢: git add file.cs git commit -m "msg" Code Formatted, Re-Added, and Commit!谢谢!
标签: git formatting githooks