git 您能从 GitHub 存储库中获取代码行数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26881441/
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 02:39:00  来源:igfitidea点击:

Can you get the number of lines of code from a GitHub repository?

gitgithubline-count

提问by Hubro

In a GitHub repository you can see “language statistics”, which displays the percentageof the project that's written in a language. It doesn't, however, display how many lines of code the project consists of. Often, I want to quickly get an impression of the scale and complexity of a project, and the count of lines of code can give a good first impression. 500 lines of code implies a relatively simple project, 100,000 lines of code implies a very large/complicated project.

在 GitHub 存储库中,您可以看到“语言统计数据”,它显示了用某种语言编写的项目的百分比。但是,它不会显示项目包含多少行代码。通常,我想快速了解一个项目的规模和复杂程度,而代码行数可以给人留下良好的第一印象。500 行代码意味着一个相对简单的项目,100,000 行代码意味着一个非常大/复杂的项目。

So, is it possible to get the lines of code written in the various languages from a GitHub repository, preferably without cloning it?

那么,是否有可能从 GitHub 存储库中获取用各种语言编写的代码行,最好不要克隆它?



The question “Count number of lines in a git repository” asks how to count the lines of code in a local Git repository, but:

计算 git 存储库中的行数”问题询问如何计算本地 Git 存储库中的代码行数,但是:

  1. You have to clone the project, which could be massive. Cloning a project like Wine, for example, takes ages.
  2. You would count lines in files that wouldn't necessarily be code, like i13n files.
  3. If you count just(for example) Ruby files, you'd potentially miss massive amount of code in other languages, like JavaScript. You'd have to know beforehand which languages the project uses. You'd also have to repeat the count for every language the project uses.
  1. 您必须克隆该项目,这可能非常庞大。例如,克隆一个像 Wine 这样的项目需要很长时间。
  2. 您会计算不一定是代码的文件中的行数,例如 i13n 文件。
  3. 如果您计算(例如)Ruby 文件,您可能会遗漏大量其他语言(如 JavaScript)的代码。您必须事先知道项目使用哪些语言。您还必须为项目使用的每种语言重复计数。

All in all, this is potentially far too time-intensive for “quickly checking the scale of a project”.

总而言之,这对于“快速检查项目规模”来说可能过于耗时。

回答by Rory O'Kane

A shell script, cloc-git

一个shell脚本, cloc-git

You can use this shell script to count the number of lines in a remote Git repository with one command:

您可以使用此 shell 脚本通过一个命令计算远程 Git 存储库中的行数:

#!/usr/bin/env bash
git clone --depth 1 "" temp-linecount-repo &&
  printf "('temp-linecount-repo' will be deleted automatically)\n\n\n" &&
  cloc temp-linecount-repo &&
  rm -rf temp-linecount-repo

Installation

安装

This script requires CLOC(“Count Lines of Code”) to be installed. cloccan probably be installed with your package manager – for example, brew install clocwith Homebrew. There is also a docker image published under mribeiro/cloc.

该脚本需要安装CLOC(“Count Lines of Code”)。cloc可以用你的包管理器安装——例如,brew install clocHomebrew。还有一个 docker 镜像发布在mribeiro/cloc.

You can install the script by saving its code to a file cloc-git, running chmod +x cloc-git, and then moving the file to a folder in your $PATHsuch as /usr/local/bin.

您可以通过保存它的代码到一个文件中安装脚本cloc-git,运行chmod +x cloc-git,然后移动文件的文件夹中$PATH,例如/usr/local/bin

Usage

用法

The script takes one argument, which is any URL that git clonewill accept. Examples are https://github.com/evalEmpire/perl5i.git(HTTPS) or [email protected]:evalEmpire/perl5i.git(SSH). You can get this URL from any GitHub project page by clicking “Clone or download”.

该脚本采用一个参数,即可git clone接受的任何 URL 。示例是https://github.com/evalEmpire/perl5i.git(HTTPS) 或[email protected]:evalEmpire/perl5i.git(SSH)。您可以通过单击“克隆或下载”从任何 GitHub 项目页面获取此 URL。

Example output:

示例输出:

$ cloc-git https://github.com/evalEmpire/perl5i.git
Cloning into 'temp-linecount-repo'...
remote: Counting objects: 200, done.
remote: Compressing objects: 100% (182/182), done.
remote: Total 200 (delta 13), reused 158 (delta 9), pack-reused 0
Receiving objects: 100% (200/200), 296.52 KiB | 110.00 KiB/s, done.
Resolving deltas: 100% (13/13), done.
Checking connectivity... done.
('temp-linecount-repo' will be deleted automatically)


     171 text files.
     166 unique files.                                          
      17 files ignored.

http://cloc.sourceforge.net v 1.62  T=1.13 s (134.1 files/s, 9764.6 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Perl                           149           2795           1425           6382
JSON                             1              0              0            270
YAML                             2              0              0            198
-------------------------------------------------------------------------------
SUM:                           152           2795           1425           6850
-------------------------------------------------------------------------------

Alternatives

备择方案

Run the commands manually

手动运行命令

If you don't want to bother saving and installing the shell script, you can run the commands manually. An example:

如果您不想费心保存和安装 shell 脚本,您可以手动运行这些命令。一个例子:

$ git clone --depth 1 https://github.com/evalEmpire/perl5i.git
$ cloc perl5i
$ rm -rf perl5i

Linguist

语言学家

If you want the results to match GitHub's language percentages exactly, you can try installing Linguistinstead of CLOC. According to its README, you need to gem install linguistand then run linguist. I couldn't get it to work (issue #2223).

如果您希望结果与 GitHub 的语言百分比完全匹配,您可以尝试安装Linguist而不是CLOC。根据它的README,你需要gem install linguist然后运行linguist。我无法让它工作(问题 #2223)。

回答by Ahmad Awais

You can run something like

你可以运行类似的东西

git ls-files | xargs wc -l

which will give you the total count →

这会给你总数→

lines of code

代码行

Or use this tool → http://line-count.herokuapp.com/

或者使用这个工具→ http://line-count.herokuapp.com/

回答by Artem Solovev

There is an extension for Google Chrome browser- GLOCwhich works for public and private repos.

谷歌 Chrome 浏览器有一个扩展- GLOC,适用于公共和私人存储库。

Counts the number of lines of code of a project from:

计算项目的代码行数:

  • project detail page
  • user's repositories
  • organization page
  • search results page
  • trending page
  • explore page
  • 项目详情页面
  • 用户的存储库
  • 组织页面
  • 搜索结果页面
  • 热门页面
  • 探索页面


enter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description here

在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明

回答by Lewis

If you go to the graphs/contributors page, you can see a list of all the contributors to the repo and how many lines they've added and removed.

如果您转到图表/贡献者页面,您可以看到所有贡献者的列表,以及他们添加和删除的行数。

Unless I'm missing something, subtracting the aggregate number of lines deleted from the aggregate number of lines added among all contributors should yield the total number of lines of code in the repo. (EDIT: it turns out I was missing something after all. Take a look at orbitbot's commentfor details.)

除非我遗漏了什么,从所有贡献者中添加的总行数中减去删除的总行数应该会得出 repo 中的代码总行数。(编辑:事实证明我毕竟错过了一些东西。有关详细信息,请查看轨道机器人的评论。)

UPDATE:

更新:

This data is also available in GitHub's API. So I wrote a quick script to fetch the data and do the calculation:

这些数据也可以在 GitHub 的API中找到。所以我写了一个快速脚本来获取数据并进行计算:

'use strict';

function countGithub(repo) {
fetch('https://api.github.com/repos/'+repo+'/stats/contributors')
    .then(response => response.json())
    .then(contributors => contributors
        .map(contributor => contributor.weeks
            .reduce((lineCount, week) => lineCount + week.a - week.d, 0)))
    .then(lineCounts => lineCounts.reduce((lineTotal, lineCount) => lineTotal + lineCount))
    .then(lines => window.alert(lines));
}

countGithub('jquery/jquery'); // or count anything you like

Just paste it in a Chrome DevTools snippet, change the repo and click run.

只需将其粘贴到 Chrome DevTools 代码段中,更改 repo 并单击运行。

Disclaimer (thanks to lovasoa):

免责声明(感谢lovasoa):

Take the results of this method with a grain of salt, because for some repos (sorich87/bootstrap-tour) it results in negative values, which might indicate there's something wrong with the data returned from GitHub's API.

对这个方法的结果持保留态度,因为对于某些 repos (sorich87/bootstrap-tour),它会导致负值,这可能表明从 GitHub 的 API 返回的数据有问题。

UPDATE:

更新:

Looks like this method to calculate total line numbers isn't entirely reliable. Take a look at orbitbot's commentfor details.

看起来这种计算总行数的方法并不完全可靠。详情请查看轨道机器人的评论

回答by Schwern

You can clone just the latest commit using git clone --depth 1 <url>and then perform your own analysis using Linguist, the same software Github uses. That's the only way I know you're going to get linesof code.

您可以只克隆最新的提交git clone --depth 1 <url>,然后使用与 Github 使用的软件相同的Linguist执行您自己的分析。这是我知道您将获得代码的唯一方法。

Another option is to use the API to list the languages the project uses. It doesn't give them in lines but in bytes. For example...

另一种选择是使用 API 列出项目使用的语言。它不是按行而是按字节给出它们。例如...

$ curl https://api.github.com/repos/evalEmpire/perl5i/languages
{
  "Perl": 274835
}

Though take that with a grain of salt, that project includes YAML and JSON which the web site acknowledgesbut the API does not.

尽管对此持保留态度,但该项目包括 YAML 和 JSON,网站承认但 API 不承认

Finally, you can use code searchto ask which files match a given language. This example asks which files in perl5i are Perl. https://api.github.com/search/code?q=language:perl+repo:evalEmpire/perl5i. It will not give you lines, and you have to ask for the file size separately using the returned urlfor each file.

最后,您可以使用代码搜索来询问哪些文件与给定语言匹配。此示例询问 perl5i 中的哪些文件是 Perl。 https://api.github.com/search/code?q=language:perl+repo:evalEmpire/perl5i. 它不会给你行,你必须使用url每个文件的返回值分别询问文件大小。

回答by Hubro

Not currently possible on Github.com or their API-s

目前无法在 Github.com 或其 API-s 上使用

I have talked to customer support and confirmed that this can not be done on github.com. They have passed the suggestion along to the Github team though, so hopefully it will be possible in the future. If so, I'll be sure to edit this answer.

我已经与客户支持进行了交谈,并确认这无法在 github.com 上完成。不过,他们已将建议传递给 Github 团队,因此希望将来可以实现。如果是这样,我一定会编辑这个答案。

Meanwhile, Rory O'Kane's answeris a brilliant alternative based on clocand a shallow repo clone.

同时,Rory O'Kane 的回答是一个基于cloc浅层回购克隆的绝妙替代方案。

回答by Yi Kai

You can use GitHub API to get the sloc like the following function

您可以使用 GitHub API 获取 sloc 如下函数

function getSloc(repo, tries) {

    //repo is the repo's path
    if (!repo) {
        return Promise.reject(new Error("No repo provided"));
    }

    //GitHub's API may return an empty object the first time it is accessed
    //We can try several times then stop
    if (tries === 0) {
        return Promise.reject(new Error("Too many tries"));
    }

    let url = "https://api.github.com/repos" + repo + "/stats/code_frequency";

    return fetch(url)
        .then(x => x.json())
        .then(x => x.reduce((total, changes) => total + changes[1] + changes[2], 0))
        .catch(err => getSloc(repo, tries - 1));
}

Personally I made an chrome extension which shows the number of SLOC on both github project list and project detail page. You can also set your personal access token to access private repositories and bypass the api rate limit.

我个人做了一个 chrome 扩展,它在 github 项目列表和项目详细信息页面上显示 SLOC 的数量。您还可以设置您的个人访问令牌以访问私有存储库并绕过 api 速率限制。

You can download from here https://chrome.google.com/webstore/detail/github-sloc/fkjjjamhihnjmihibcmdnianbcbccpnn

您可以从这里下载https://chrome.google.com/webstore/detail/github-sloc/fkjjjamhihnjmihibcmdnianbcbccpnn

Source code is available here https://github.com/martianyi/github-sloc

源代码可在此处获得https://github.com/martianyi/github-sloc

回答by lovasoa

Firefox add-on Github SLOC

Firefox 插件Github SLOC

I wrote a small firefox addon that prints the number of lines of code on github project pages: Github SLOC

我写了一个小的firefox插件,它在github项目页面上打印代码行数:Github SLOC

回答by Jimmy Da

If the question is "can you quicklyget NUMBER OF LINES of a github repo", the answer is no as stated by the other answers.

如果问题是“您能否快速获得 GitHub 存储库的 NUMBER OF LINES”,则答案是否定的,如其他答案所述。

However, if the question is "can you quicklycheck the SCALE of a project", I usually gauge a project by looking at its size. Of course the size will include deltas from all active commits, but it is a good metric as the order of magnitude is quite close.

但是,如果问题是“您能否快速检查项目的 SCALE”,我通常会通过查看其大小来衡量一个项目。当然,大小将包括所有活动提交的增量,但这是一个很好的指标,因为数量级非常接近。

E.g.

例如

How big is the "docker" project?

“docker”项目有多大?

In your browser, enter api.github.com/repos/ORG_NAME/PROJECT_NAME i.e. api.github.com/repos/docker/docker

在浏览器中输入 api.github.com/repos/ORG_NAME/PROJECT_NAME 即 api.github.com/repos/docker/docker

In the response hash, you can find the size attribute:

在响应哈希中,您可以找到 size 属性:

{
    ...
    size: 161432,
    ...
}

This should give you an idea of the relative scale of the project. The number seems to be in KB, but when I checked it on my computer it's actually smaller, even though the order of magnitude is consistent. (161432KB = 161MB, du -s -h docker = 65MB)

这应该让您了解项目的相对规模。这个数字似乎以 KB 为单位,但当我在我的电脑上检查它时,它实际上更小,即使数量级是一致的。(161432KB = 161MB,du -s -h docker = 65MB)

回答by Tobi Obeck

npm install sloc -g
git clone --depth 1 https://github.com/vuejs/vue/
sloc ".\vue\src" --format cli-table
rm -rf ".\vue\"

Instructions and Explanation

说明和说明

  1. Install sloc from npm, a command line tool (Node.jsneeds to be installed).
  1. 从命令行工具npm 安装 sloc(需要安装Node.js)。
npm install sloc -g
  1. Clone shallow repository(faster download than full clone).
  1. 克隆浅存储库(下载速度比完整克隆更快)。
git clone --depth 1 https://github.com/facebook/react/
  1. Run slocand specifiy the path that should be analyzed.
  1. 运行 sloc并指定应分析的路径。
sloc ".\react\src" --format cli-table

sloc supports formatting the output as a cli-table, as jsonor csv. Regular expressions can be used to exclude files and folders (Further information on npm).

sloc 支持将输出格式化为 a cli-table、 asjsoncsv。正则表达式可用于排除文件和文件夹(有关 npm 的更多信息)。

  1. Delete repository folder (optional)
  1. 删除存储库文件夹(可选)

Powershell: rm -r -force ".\react\"or on Mac/Unix: rm -rf ".\react\"

Powershell:rm -r -force ".\react\"或在 Mac/Unix 上:rm -rf ".\react\"

Screenshots of the executed steps (cli-table):

执行步骤的截图(cli-table):

sloc output as acli-table

sloc 输出为 acli-table

sloc output (no arguments):

sloc 输出(无参数):

sloc output without arguments

不带参数的 sloc 输出