Count number of lines in a git repository

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

    If you want to get the number of lines from a certain author, try the following code:

    git ls-files "*.java" | xargs -I{} git blame {} | grep ${your_name} | wc -l
    
    0 讨论(0)
  • 2020-12-02 03:42

    This works as of cloc 1.68:

    cloc --vcs=git

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

    This tool on github https://github.com/flosse/sloc can give the output in more descriptive way. It will Create stats of your source code:

    • physical lines
    • lines of code (source)
    • lines with comments
    • single-line comments
    • lines with block comments
    • lines mixed up with source and comments
    • empty lines
    0 讨论(0)
  • 2020-12-02 03:45

    If you want to find the total number of non-empty lines, you could use AWK:

    git ls-files | xargs cat | awk '/\S/{x++} END{print "Total number of non-empty lines:", x}'

    This uses regex to count the lines containing a non-whitespace character.

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

    The best solution, to me anyway, is buried in the comments of @ephemient's answer. I am just pulling it up here so that it doesn't go unnoticed. The credit for this should go to @FRoZeN (and @ephemient).

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

    returns the total of files and lines in the working directory of a repo, without any additional noise. As a bonus, only the source code is counted - binary files are excluded from the tally.

    The command above works on Linux and OS X. The cross-platform version of it is

    git diff --shortstat 4b825dc642cb6eb9a060e54bf8d69288fbee4904
    

    That works on Windows, too.

    For the record, the options for excluding blank lines,

    • -w/--ignore-all-space,
    • -b/--ignore-space-change,
    • --ignore-blank-lines,
    • --ignore-space-at-eol

    don't have any effect when used with --shortstat. Blank lines are counted.

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

    The answer by Carl Norum assumes there are no files with spaces, one of the characters of IFS with the others being tab and newline. The solution would be to terminate the line with a NULL byte.

     git ls-files -z | xargs -0 cat | wc -l
    
    0 讨论(0)
提交回复
热议问题