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

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

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

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

当前回答

我在玩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厕所-

其他回答

我在玩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厕所-

从时钟1.68开始工作:

cloc——风险投资= git

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

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)

如果你想找到非空行总数,你可以使用AWK:

git ls-files | xargs cat | awk '/\S/{x++} END{print "非空行总数:",x}'

它使用regex计算包含非空格字符的行数。

如果您想要这个计数是因为您想要了解项目的范围,那么您可能更喜欢CLOC(“计算代码行数”)的输出,它按语言给出了重要代码行和不重要代码行的分类。

cloc $(git ls-files)

(这一行相当于git ls-files | xargs cloc。它使用sh的$()命令替换特性。)

样例输出:

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

您必须首先安装CLOC。你可以用你的包管理器安装cloc,例如,用Homebrew安装cloc。

Cloc $(git ls-files)通常是对Cloc ..例如,上面使用git ls-files的示例输出报告了471行代码。对于同一个项目,使用cloc。报告高达456,279行(需要6分钟运行),因为它在git忽略的node_modules文件夹中搜索依赖项。