我如何计算在一个git存储库中所有文件中出现的总行数?

Git ls-files给了我一个由Git跟踪的文件列表。

我正在找一个命令来隐藏所有这些文件。类似的

git ls-files | [cat all these files] | wc -l

当前回答

我使用以下方法:

git grep ^ | wc -l

这将搜索由git控制的所有文件的regex ^,它表示一行的开始,所以这个命令给出了总行数!

其他回答

我在玩cmder (http://gooseberrycreative.com/cmder/),我想计算html,css,java和javascript的行数。虽然上面的一些答案是有效的,或者grep中的模式没有-我在这里(https://unix.stackexchange.com/questions/37313/how-do-i-grep-for-multiple-patterns)发现我必须转义它

这就是我现在用的:

git ls-files | grep " \ (html css |。\ |。js |。java \)美元“| xargs厕所-

github https://github.com/flosse/sloc上的这个工具可以以更描述性的方式提供输出。它将创建你的源代码的统计数据:

物理行 代码行数(源代码) 带有注释的行 单行注释 带有块注释的行 代码行与源代码和注释混淆 空行

git diff --stat 4b825dc642cb6eb9a060e54bf8d69288fbee4904

这显示了从空树到当前工作树的差异。它恰好计算当前工作树中的所有行。

要获得当前工作树中的数字,请执行以下操作:

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

它会给你一个字符串,比如1770 files changed, 166776 insertions(+)。

根据您是否希望包含二进制文件,有两种解决方案。

git grep --cached -al '' | xargs -P 4 cat | wc -l 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)

: | git mktree | git diff --shortstat --stdin

Or:

git ls-tree @ | sed '1i\\' | git mktree --batch | xargs | git diff-tree --shortstat --stdin