我如何计算在一个git存储库中所有文件中出现的总行数?
Git ls-files给了我一个由Git跟踪的文件列表。
我正在找一个命令来隐藏所有这些文件。类似的
git ls-files | [cat all these files] | wc -l
我如何计算在一个git存储库中所有文件中出现的总行数?
Git ls-files给了我一个由Git跟踪的文件列表。
我正在找一个命令来隐藏所有这些文件。类似的
git ls-files | [cat all these files] | wc -l
当前回答
对我来说,最好的解决方案就埋在@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一起使用时没有任何影响。空行被计数。
其他回答
如果你想找到非空行总数,你可以使用AWK:
git ls-files | xargs cat | awk '/\S/{x++} END{print "非空行总数:",x}'
它使用regex计算包含非空格字符的行数。
Xargs会让你把所有文件放在一起,然后把它们传递给wc,就像你问的那样:
git ls-files | xargs cat | wc -l
但是跳过中间的cat会给你更多的信息,可能会更好:
git ls-files | xargs wc -l
从时钟1.68开始工作:
cloc——风险投资= git
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)