Count number of lines in a git repository

后端 未结 16 2673
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 03:17

How would I count the total number of lines present in all the files in a git repository?

git ls-files gives me a list of files tracked by git.

16条回答
  •  余生分开走
    2020-12-02 03:53

    If you want this count because you want to get an idea of the project’s scope, you may prefer the output of CLOC (“Count Lines of Code”), which gives you a breakdown of significant and insignificant lines of code by language.

    cloc $(git ls-files)
    

    (This line is equivalent to git ls-files | xargs cloc. It uses sh’s $() command substitution feature.)

    Sample output:

          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
    -------------------------------------------------------------------------------
    

    You will have to install CLOC first. You can probably install cloc with your package manager – for example, brew install cloc with Homebrew.

    cloc $(git ls-files) is often an improvement over cloc .. For example, the above sample output with git ls-files reports 471 lines of code. For the same project, cloc . reports a whopping 456,279 lines (and takes six minutes to run), because it searches the dependencies in the Git-ignored node_modules folder.

提交回复
热议问题