Count number of lines in a git repository

后端 未结 16 2463
被撕碎了的回忆
被撕碎了的回忆 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:48

    I did this:

    git ls-files | xargs file | grep "ASCII" | cut -d : -f 1 | xargs wc -l
    

    this works if you count all text files in the repository as the files of interest. If some are considered documentation, etc, an exclusion filter can be added.

    0 讨论(0)
  • 2020-12-02 03:49
    git diff --stat 4b825dc642cb6eb9a060e54bf8d69288fbee4904
    

    This shows the differences from the empty tree to your current working tree. Which happens to count all lines in your current working tree.

    To get the numbers in your current working tree, do this:

    git diff --shortstat `git hash-object -t tree /dev/null`
    

    It will give you a string like 1770 files changed, 166776 insertions(+).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 03:54

    Depending on whether or not you want to include binary files, there are two solutions.

    1. git grep --cached -al '' | xargs -P 4 cat | wc -l
    2. git grep --cached -Il '' | xargs -P 4 cat | wc -l

      "xargs -P 4" means it can read the files using four parallel processes. This can be really helpful if you are scanning very large repositories. Depending on capacity of the machine you may increase number of processes.

      -a, process binary files as text (Include Binary)
      -l '', show only filenames instead of matching lines (Scan only non empty files)
      -I, don't match patterns in binary files (Exclude Binary)
      --cached, search in index instead of in the work tree (Include uncommitted files)

    0 讨论(0)
  • 2020-12-02 03:58

    I've encountered batching problems with git ls-files | xargs wc -l when dealing with large numbers of files, where the line counts will get chunked out into multiple total lines.

    Taking a tip from question Why does the wc utility generate multiple lines with "total"?, I've found the following command to bypass the issue:

    wc -l $(git ls-files)

    Or if you want to only examine some files, e.g. code:

    wc -l $(git ls-files | grep '.*\.cs')

    0 讨论(0)
  • 2020-12-02 04:00

    I use the following:

    git grep ^ | wc -l
    

    This searches all files versioned by git for the regex ^, which represents the beginning of a line, so this command gives the total number of lines!

    0 讨论(0)
提交回复
热议问题