我正在尝试添加这个:https://gist.github.com/davetron5000/37350checkstyle代码格式检查作为预提交git钩子。
我按照下面的说明操作,但它不起作用。
我试过的步骤:
- Checkstyle 的 jar 文件在某处
- Checkstyle XML 在某处检查文件
- 配置git:
git config --add checkstyle.jar <location of jar>
git config --add checkstyle.checkfile <location of checkfile>
git config --add java.command <path to java executable> [optional, defaults to assuming it's in your path]
- 将它作为预提交放在你的 .git/hooks 目录中
也许它不起作用,因为我不明白
git config --add java.command <path to java executable>
是什么意思。但它说它是可选的如果那不是问题,也许我需要以某种方式使脚本文件成为可执行文件?
ps: 操作系统是 Windows
好吧,我终于找到了解决方案。我在如何让它工作的说明中添加了一些评论。
1. checkstyle's jar file somewhere
2. a checkstyle XML check file somewhere
3. To configure git:
* git config --add checkstyle.jar <location of jar>
* git config --add checkstyle.checkfile <location of checkfile>
* git config --add java.command <path to java executale> [optional
defaults to assuming it's in your path]
我检查了我的配置(可以在你的 git 存储库的 .git 目录中找到),它看起来像这样:
...
[checkstyle]
checkfile = C:\\Users\\schuster\\Desktop\\checkstyle
jar = C:\\Users\\schuster\\Desktop\\checkstyle
...
所以自从我在 Windows 上工作后,我将其更改为:
...
[checkstyle]
checkfile = C:/Users/schuster/Desktop/checkstyle/google_checks.xml
jar = C:/Users/schuster/Desktop/checkstyle/checkstyle.jar
...
.
4. Put this in your .git/hooks directory as pre-commit
'This' 是我在陈述问题时链接的文件。所以这个文件需要在/hooks目录下。但它必须重命名为已经存在的现有样本之一。由于我的钩子是预提交钩子,所以我使用了“预提交”文件名。接下来这个文件必须变成一个可执行文件。为此,请在 git 存储库的 /hooks 目录中输入
chmod +x pre-commit
。如果您使用 Windows,请使用 Git Bash。
如果有人想使用这个脚本并且想知道为什么即使检查失败它也不会中止 - 这是修复它的方法。
在第 58 行
if (&run_and_log_system ($command))
if (!&run_and_log_system ($command))
配置checkstyle.jar 和checkstyle.checkfile 后。如果它没有在检查失败时中止,这里就是修复方法。
在第 58 行
if (&run_and_log_system ($command))
替换为以下
$command .= " | grep WARN ";
if (!&run_and_log_system ($command))
我已经开源并发布了 Commit Controls,这是一个 ZIP 包,旨在在 Javaland 中更轻松地本地连接项目中的 Checkstyle。
带有文档的项目主页。希望这个项目能简化大多数用例。
我在哪里可以找到 jar 文件?我已经有了 checkstyle.xml 文件。但是我应该把 jar 文件放在哪里呢?