Showing Check style issues/warning only with git diff and not entire file

236 views
Skip to first unread message

sriram rajan

unread,
Apr 22, 2015, 1:13:42 AM4/22/15
to checksty...@googlegroups.com
Hi All 


I am new to Checkstyle. I am writing a git hook to style check the "commit code". How to display checkstyle errors/warning only to "git diff" code and not the entire file. 


Thanks
Sriram 

Roman Ivanov

unread,
Apr 22, 2015, 12:20:28 PM4/22/15
to sriram rajan, checksty...@googlegroups.com
Hi ,

1) Checkstlye works only with whole java file, that file have to be compilable.

2) 
You can take from diff a paths to affected files and line numbers 

$ git diff
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/CheckStyleTask.java 
             b/src/main/java/com/puppycrawl/tools/checkstyle/CheckStyleTask.java 
index a08123a..33c291e 100644
--- a/src/main/java/com/puppycrawl/tools/checkstyle/CheckStyleTask.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/CheckStyleTask.java
@@ -582,6 +582,11 @@ public class CheckStyleTask extends Task
             return new DefaultLogger(new FileOutputStream(toFile), true);
         }
 
+        @Override
+        protected void finalize() throws Throwable {
+            super.finalize();
+        }
+
         /**
          * @return an XMLLogger instance
          * @param task the task to possibly log to

3) you need to run checkstyle against that files, no need to run against whole project

4) you need to generate a suppresion file that will ignore all violations on lines that are other then referenced in git dif.

Example:
<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
    "-//Puppy Crawl//DTD Suppressions 1.1//EN"

<suppressions>
    <suppress checks=".*"
              files="src/main/java/com/puppycrawl/tools/checkstyle/CheckStyleTask.java"
              lines="1-582, 586-10000000"/>
</suppressions>


FYI:  https://github.com/checkstyle/contribution/tree/master/hooks I am not sure that it works , but it could help you to some extend.

thanks,
Roman Ivanov

sriram rajan

unread,
Apr 22, 2015, 1:29:46 PM4/22/15
to checksty...@googlegroups.com
Thanks Roman for the quick help.

Thanks
Sriram
Reply all
Reply to author
Forward
0 new messages