Count number of lines in a git repository

2019-01-09 20:27发布

How would I count the total number of lines present in all the files in a git repository?

git ls-files gives me a list of files tracked by git.

I'm looking for a command to cat all those files. Something like

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

12条回答
Ridiculous、
2楼-- · 2019-01-09 20:52
: | git mktree | git diff --shortstat --stdin

Or:

git ls-tree @ | sed '1i\\' | git mktree --batch | xargs | git diff-tree --shortstat --stdin
查看更多
够拽才男人
3楼-- · 2019-01-09 20:58

xargs will do what you want:

git ls-files | xargs cat | wc -l

But with more information and probably better, you can do:

git ls-files | xargs wc -l
查看更多
祖国的老花朵
4楼-- · 2019-01-09 20:58

I've encountered batching problems with git ls-files | xargs wc -l when dealing with large numbers of files, where the line counts will get chunked out into multiple total lines.

Taking a tip from question Why does the wc utility generate multiple lines with "total"?, I've found the following command to bypass the issue:

wc -l $(git ls-files)

Or if you want to only examine some files, e.g. code:

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

查看更多
别忘想泡老子
5楼-- · 2019-01-09 20:59

This tool on github https://github.com/flosse/sloc can give the output in more descriptive way. It will Create stats of your source code:

  • physical lines
  • lines of code (source)
  • lines with comments
  • single-line comments
  • lines with block comments
  • lines mixed up with source and comments
  • empty lines
查看更多
疯言疯语
6楼-- · 2019-01-09 21:00
git diff --stat 4b825dc642cb6eb9a060e54bf8d69288fbee4904

This shows the differences from the empty tree to your current working tree. Which happens to count all lines in your current working tree.

To get the numbers in your current working tree, do this:

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

It will give you a string like 1770 files changed, 166776 insertions(+).

查看更多
看我几分像从前
7楼-- · 2019-01-09 21:02

Try:

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

on the directory/directories in question

查看更多
登录 后发表回答