java – 将checkstyle添加为pre-commit git hook

我想添加这个:https://gist.github.com/davetron5000/37350 checkstyle代码格式检查为pre-commit git hook.

我按照下面的说明,但它不起作用.

我试过的步骤:

  1. Checkstyle’s jar file somewhere
  2. Checkstyle XML check file somewhere
  3. To configure git:
    • git config --add checkstyle.jar
    • git config --add checkstyle.checkfile
    • git config --add java.command
  4. Put this in your .git/hooks directory as pre-commit

也许它不起作用,因为我不明白git config是什么–add java.command< java可执行文件的路径>手段.但它说它是可选的如果那不是问题,我可能需要以某种方式使脚本文件成为可执行文件吗?

ps:操作系统是Windows

最佳答案
好吧,好像我终于找到了解决方案.我在指令中添加了一些评论,说明我是如何工作的.

 1. checkstyle's jar file somewhere
 2. a checkstyle XML check file somewhere
 3. To configure git:
   * git config --add checkstyle.jar 

我检查了我的配置(可以在你的git reposirtory的.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

当我说出我的问题时,’这’是我链接的文件.所以这个文件需要在/ hooks目录中.但它必须重新命名为已存在的现有样本之一.由于我的钩子是预提交钩子,我采用了“预提交”文件名.接下来,此文件必须成为可执行文件.要在git存储库的/ hooks目录中的chmod x pre-commit中输入.如果您使用Windows,请使用Git Bash.

编辑:

如果有人想要使用这个脚本,并想知道为什么即使检查失败也不会中止 – 这是如何解决它.
在第58行
         if(& run_and_log_system($command))
          必须被吟唱
         if(!& run_and_log_system($command))

相关文章

摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个...
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:...
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程...