Count number of lines in a git repository
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.
I'm looking for a command to cat
all those files. Something like
git ls-files | [cat all these files] | wc -l
xargs
will do what you want:
git ls-files | xargs cat | wc -l
But with more information and probably better, you can do:
git ls-files | xargs wc -l
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.
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.
上一篇: 如何查找目录及其子目录中的最大文件?
下一篇: 统计git存储库中的行数