Dog*_*ert 715 git bash shell line-count
我如何计算git存储库中所有文件中存在的总行数?
git ls-files 给我一个git跟踪的文件列表.
我正在寻找cat所有这些文件的命令.就像是
git ls-files | [cat all these files] | wc -l
Run Code Online (Sandbox Code Playgroud)
Car*_*rum 1073
xargs 会做你想做的事:
git ls-files | xargs cat | wc -l
Run Code Online (Sandbox Code Playgroud)
但是有了更多的信息,可能更好,你可以这样做:
git ls-files | xargs wc -l
Run Code Online (Sandbox Code Playgroud)
eph*_*ent 331
git diff --stat 4b825dc642cb6eb9a060e54bf8d69288fbee4904
Run Code Online (Sandbox Code Playgroud)
这显示了从空树到当前工作树的差异.这会计算当前工作树中的所有行.
要获取当前工作树中的数字,请执行以下操作:
git diff --shortstat `git hash-object -t tree /dev/null`
Run Code Online (Sandbox Code Playgroud)
它会给你一个字符串1770 files changed, 166776 insertions(+).
Ror*_*ane 293
如果您想要了解项目的范围,那么您可能更喜欢CLOC的输出("计算代码行"),它可以按语言划分重要且无关紧要的代码行.
cloc $(git ls-files)
Run Code Online (Sandbox Code Playgroud)
(这一行相当于git ls-files | xargs cloc.它使用sh's $()命令替换功能.)
样本输出:
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
-------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
您必须先安装CLOC.您可以使用软件包管理器进行安装cloc - 例如,brew install cloc使用Homebrew.
cloc $(git ls-files)通常是一种改进cloc ..例如,上面的示例输出带有git ls-files471行代码报告.对于同一个项目,cloc .报告高达456,279行(并且需要6分钟才能运行),因为它会搜索Git-ignored node_modules文件夹中的依赖项.
Jus*_*dro 51
我git ls-files | xargs wc -l在处理大量文件时遇到了批处理问题,其中行计数将被分成多total行.
从问题中获取提示为什么wc实用程序会生成多行"total"?,我发现以下命令绕过了这个问题:
wc -l $(git ls-files)
或者,如果您只想检查一些文件,例如代码:
wc -l $(git ls-files | grep '.*\.cs')
has*_*nge 40
无论如何,最好的解决方案都埋没在@ ephemient的回答的评论中.我只是把它拉到这里,以免它被忽视.这个应该归功于@FRoZeN(和@ephemient).
git diff --shortstat `git hash-object -t tree /dev/null`
Run Code Online (Sandbox Code Playgroud)
返回repo工作目录中的文件和行总数,没有任何额外的噪音.作为奖励,只计算源代码 - 从计数器中排除二进制文件.
上面的命令适用于Linux和OS X.它的跨平台版本是
git diff --shortstat 4b825dc642cb6eb9a060e54bf8d69288fbee4904
Run Code Online (Sandbox Code Playgroud)
这也适用于Windows.
为了记录,排除空行的选项,
-w/ --ignore-all-space,-b/ --ignore-space-change,--ignore-blank-lines, --ignore-space-at-eol使用时没有任何效果--shortstat.空行被计算在内.
Mic*_*dis 11
我正在玩cmder(http://gooseberrycreative.com/cmder/),我想计算html,css,java和javascript的行数.虽然上面的一些答案有效,但orgrep中的模式没有 - 我在这里找到了(https://unix.stackexchange.com/questions/37313/how-do-i-grep-for-multiple-patterns)逃避它
所以这就是我现在使用的:
git ls-files | grep "\(.html\|.css\|.js\|.java\)$" | xargs wc -l
我使用以下内容:
git grep ^ | wc -l
Run Code Online (Sandbox Code Playgroud)
这将搜索由git版本化的所有文件^,用于表示行的开头的正则表达式,因此该命令给出总行数!
我这样做了:
git ls-files | xargs file | grep "ASCII" | cut -d : -f 1 | xargs wc -l
Run Code Online (Sandbox Code Playgroud)
如果您将存储库中的所有文本文件都算作感兴趣的文件,则此方法有效。如果某些被认为是文档等,则可以添加排除过滤器。
小智 5
尝试:
find . -type f -name '*.*' -exec wc -l {} +
Run Code Online (Sandbox Code Playgroud)
在有问题的目录上
如果您想获取某个作者的行数,请尝试以下代码:
git ls-files "*.java" | xargs -I{} git blame {} | grep ${your_name} | wc -l
Run Code Online (Sandbox Code Playgroud)