bash 计算 git 存储库中的行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4822471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:08:03  来源:igfitidea点击:

Count number of lines in a git repository

bashgitshellline-count

提问by Dogbert

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

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

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

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

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

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

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

回答by Carl Norum

xargswill do what you want:

xargs会做你想做的:

git ls-files | xargs cat | wc -l

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

但是有了更多信息并且可能更好,您可以执行以下操作:

git ls-files | xargs wc -l

回答by ephemient

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(+).

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

回答by Rory O'Kane

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(“计算代码行数”)的输出,它可以按语言对重要和不重要的代码行进行细分。

cloc $(git ls-files)

(This line is equivalent to git ls-files | xargs cloc. It uses sh's $()command substitutionfeature.)

(此行等效于git ls-files | xargs cloc。它使用sh$()命令替换功能。)

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 clocwith your package manager– for example, brew install clocwith Homebrew.

您必须先安装 CLOC。您可能可以使用包管理器进行安装cloc- 例如,brew install cloc使用Homebrew

cloc $(git ls-files)is often an improvement over cloc .. For example, the above sample output with git ls-filesreports 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_modulesfolder.

cloc $(git ls-files)往往是一种改进cloc .。例如,上面的示例输出git ls-files报告了 471 行代码。对于同一个项目,cloc .报告高达 456,279 行(运行需要 6 分钟),因为它在 Git-ignorednode_modules文件夹中搜索依赖项。

回答by Justin Aquadro

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

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

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 实用程序生成多行“总计”?,我发现以下命令可以绕过这个问题:

wc -l $(git ls-files)

wc -l $(git ls-files)

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

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

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

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

回答by hashchange

The best solution, to me anyway, is buried in the comments of @ephemient's answer. I am just pulling it up here so that it doesn't go unnoticed. The credit for this should go to @FRoZeN (and @ephemient).

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

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

returns the total of files and lines in the working directory of a repo, without any additional noise. As a bonus, only the source code is counted - binary files are excluded from the tally.

返回存储库工作目录中文件和行的总数,没有任何额外的噪音。作为奖励,只计算源代码 - 二进制文件从计数中排除。

The command above works on Linux and OS X. The cross-platform version of it is

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

git diff --shortstat 4b825dc642cb6eb9a060e54bf8d69288fbee4904

That works on Windows, too.

这也适用于 Windows。

For the record, the options for excluding blank lines,

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

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

don't have any effect when used with --shortstat. Blank lines are counted.

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

回答by kes

This works as of cloc1.68:

这适用于cloc1.68:

cloc --vcs=git

cloc --vcs=git

回答by Michail Michailidis

I was playing around with cmder (http://gooseberrycreative.com/cmder/) and I wanted to count the lines of html,css,java and javascript. While some of the answers above worked, orpattern in grep didn't - I found here (https://unix.stackexchange.com/questions/37313/how-do-i-grep-for-multiple-patterns) that I had to escape it

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

So this is what I use now:

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

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

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

回答by Christopher Shroba

I use the following:

我使用以下内容:

git grep ^ | wc -l

This searches all files versioned by git for the regex ^, which represents the beginning of a line, so this command gives the total number of lines!

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

回答by love

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

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

  • 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
  • 物理线路
  • 代码行(来源)
  • 带注释的行
  • 单行注释
  • 带有块注释的行
  • 行与来源和评论混在一起
  • 空行

回答by Sasha Pachev

I did this:

我这样做了:

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

this works if you count all text files in the repository as the files of interest. If some are considered documentation, etc, an exclusion filter can be added.

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