Get lines of code in src folder skipping comments and empty lines

803 Views Asked by At

Hi I already have something to get the lines of code but still it out puts the count having empty lines and comments counted.

git ls-files | grep "\.java$" | xargs wc -l

Can you modify this to skip comments and blank lines..?

Thanks in advance

2

There are 2 best solutions below

0
On

Try CLOC, it can lists numbers in great detail. You need to install CLOC first using syntax brew install cloc

cloc $(git ls-files)

Sample output for reference:

20 text files.
      20 unique files.                              
       6 files ignored.

    http://cloc.sourceforge.net v 1.62  T=0.22 s (62.5 files/s, 2771.2 lines/s)
    -------------------------------------------------------------------------------
    Language                     files          blank        comment           code
    -------------------------------------------------------------------------------
    Javascript                       2             13            111            309
    JSON                             3              0              0             58
    HTML                             2              7             12             50
    Handlebars                       2              0              0             37
    CoffeeScript                     4              1              4             12
    SASS                             1              1              1              5
    -------------------------------------------------------------------------------
    SUM:                            14             22            128            471
    -------------------------------------------------------------------------------
0
On

Most test coverage tools also calculate LOC. For instance, Jacoco spits this out to Jenkins. Jacoco spits this out to Jenkins