904

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

git ls-files给我一个 git 跟踪的文件列表。

我正在寻找cat所有这些文件的命令。就像是

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

16 回答 16

1382

xargs将让您将cat所有文件放在一起,然后再将它们传递给wc,就像您问的那样:

git ls-files | xargs cat | wc -l

但是跳过中间cat可以为您提供更多信息,并且可能更好:

git ls-files | xargs wc -l
于 2011-01-27T22:11:03.600 回答
395
git diff --stat 4b825dc642cb6eb9a060e54bf8d69288fbee4904

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

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

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

它会给你一个像1770 files changed, 166776 insertions(+).

于 2011-01-27T22:51:38.470 回答
383

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.

于 2015-03-12T16:32:13.743 回答
67

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

从问题中获取提示为什么 wc 实用程序会生成多行带有“total”的行?,我发现以下命令可以绕过该问题:

wc -l $(git ls-files)

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

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

于 2013-07-30T06:03:40.973 回答
60

无论如何,对我来说,最好的解决方案隐藏在@ephemient 的回答的评论中。我只是把它拉到这里,这样它就不会被忽视。这应该归功于@FRoZeN(和@ephemient)。

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

返回 repo 工作目录中的文件和行的总数,没有任何额外的噪音。作为奖励,只计算源代码 - 二进制文件不包括在计数中。

上面的命令适用于 Linux 和 OS X。它的跨平台版本是

git diff --shortstat 4b825dc642cb6eb9a060e54bf8d69288fbee4904

这也适用于 Windows。

作为记录,排除空行的选项,

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

与 .一起使用时没有任何效果--shortstat。计算空行。

于 2015-03-04T15:39:44.040 回答
21

这适用于cloc 1.68:

cloc --vcs=git

于 2017-05-11T19:31:10.343 回答
18

我使用以下内容:

git grep ^ | wc -l

这会在所有由 git 版本化的文件中搜索 regex ^,它表示行的开头,所以这个命令给出了总行数!

于 2017-01-11T06:46:38.277 回答
14

我在玩cmder(http://gooseberrycreative.com/cmder/),我想计算html、css、java和javascript的行数。虽然上面的一些答案有效,or但 grep 中的模式没有 - 我在这里找到了(https://unix.stackexchange.com/questions/37313/how-do-i-grep-for-multiple-patterns)逃避它

所以这就是我现在使用的:

git ls-files | grep "\(.html\|.css\|.js\|.java\)$" | xargs wc -l

于 2015-07-22T01:00:54.313 回答
4

我这样做了:

git ls-files | xargs file | grep "ASCII" | cut -d : -f 1 | xargs wc -l

如果您将存储库中的所有文本文件都视为感兴趣的文件,则此方法有效。如果某些被认为是文档等,则可以添加排除过滤器。

于 2015-11-21T20:54:46.793 回答
4

如果要获取某个作者的行数,试试下面的代码:

git ls-files "*.java" | xargs -I{} git blame {} | grep ${your_name} | wc -l
于 2020-06-11T03:28:53.577 回答
3

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

  1. git grep --cached -al '' | xargs -P 4 cat | wc -l
  2. git grep --cached -Il '' | xargs -P 4 cat | wc -l

    “xargs -P 4”表示它可以使用四个并行进程读取文件。如果您正在扫描非常大的存储库,这将非常有用。根据机器的容量,您可能会增加进程数。

    -a,将二进制文件作为文本处理(包括二进制文件)
    -l '',仅显示文件名而不是匹配行(仅扫描非空文件)
    -I,不匹配二进制文件中的模式(排除二进制文件)--
    cached,在索引中搜索而不是在工作树中搜索(包括未提交的文件)

于 2020-03-04T16:41:23.320 回答
3

尝试:

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

在有问题的目录/目录上

于 2018-06-15T12:37:58.937 回答
3

github https://github.com/flosse/sloc上的这个工具可以以更具描述性的方式给出输出。它将创建您的源代码的统计信息:

  • 物理线路
  • 代码行(来源)
  • 带有注释的行
  • 单行注释
  • 带有块注释的行
  • 与源代码和注释混在一起的行
  • 空行
于 2016-01-04T08:00:17.957 回答
2

Carl Norum 的回答假设没有带有空格的文件,其中一个字符是 and IFS,其他字符是taband newline。解决方案是用 NULL 字节终止该行。

 git ls-files -z | xargs -0 cat | wc -l
于 2020-11-18T11:20:07.373 回答
2

如果要查找非空行的总数,可以使用 AWK:

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

这使用正则表达式来计算包含非空白字符的行。

于 2020-07-04T20:24:44.130 回答
1
: | git mktree | git diff --shortstat --stdin

或者:

git ls-tree @ | sed '1i\\' | git mktree --batch | xargs | git diff-tree --shortstat --stdin
于 2016-01-04T01:25:15.537 回答