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

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

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

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

当前回答

在处理大量文件时,我遇到过git ls-files | xargs wc -l的批处理问题,其中的行数将被分块成多个总行。

从问题中得到一个提示,为什么wc实用程序生成多行“total”?,我发现下面的命令可以绕过这个问题:

wc -l $(git ls-files)

或者如果你只想检查一些文件,例如代码:

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

其他回答

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

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)

Try:

find . -type f -name '*.*' -exec wc -l {} + 

在有问题的目录上

我使用以下方法:

git grep ^ | wc -l

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

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

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

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

如果你想从某个作者那里获得行数,试试下面的代码:

git ls-files "*.java" | xargs -I{} git blame {} | grep ${your_name} | wc -l