你能从Github存储库获取代码行数吗? [英] Can you get the number of lines of code from a Github repository?

查看:150
本文介绍了你能从Github存储库获取代码行数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Github存储库中,您可以看到language statistics,它显示用语言编写的项目的百分比。但是,它并不显示项目由多少行代码组成。通常情况下,我想快速了解一个项目的规模和复杂程度,并且代码行的数量可以给出良好的第一印象。 500行代码意味着一个相对简单的项目,100,000行代码意味着一个非常大的/复杂的项目。

那么,是否有可能将代码行写入Github存储库中的各种语言,最好不克隆它?




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


  1. 您必须复制该项目,该项目可能很庞大。克隆像Wine这样的项目需要很长时间。

  2. 您可以在不一定是代码的文件(如i13n文件)中计算行数。

  3. 如果只计算 (例如)Ruby文件,则可能会漏掉其他语言中的大量代码,如Javascript。您必须事先知道项目使用哪些语言。您还必须重复项目使用的每种语言的计数。
  4. 总而言之,这可能太耗费时间了

    解决方案

    一个shell脚本, cloc-git



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

    #!/ usr / bin / env bash
    git clone --depth 1$ 1temp-linecount-repo &安培;&安培;
    printf('temp-linecount-repo'将被自动删除)\\\
    \\\
    \\\
    &&
    cloc temp-linecount-repo&&
    rm -rf temp-linecount-repo



    安装



    此脚本需要安装 CLOC (计数行数)。 cloc 可能会与您的软件包管理器一起安装 - 例如, brew install cloc Homebrew



    您可以通过将代码保存到文件 cloc-git ,运行 chmod + x cloc-git ,然后将文件移动到 $ PATH 例如 / usr / local / bin



    用法



    这个脚本有一个参数,它是 git clone 会接受的任何URL。示例是 https://github.com/evalEmpire/perl5i.git (HTTPS)或 git@github.com:evalEmpire / perl5i.git (SSH)。您可以通过点击克隆或下载从任何GitHub项目页面获得该URL。



    输出示例:

      $ cloc-git https://github.com/evalEmpire/perl5i.git 
    克隆到'temp-linecount-repo'...
    remote:Counting对象:200,完成。
    remote:压缩对象:100%(182/182),完成。
    remote:总计200(delta 13),重用158(delta 9),包重用0
    接收对象:100%(200/200),296.52 KiB |完成110.00 KiB / s。
    解决三角洲问题:完成100%(13/13)。
    检查连通性...完成。
    ('temp-linecount-repo'将被自动删除)


    171个文本文件。
    166个独特文件。
    17个文件被忽略。

    http://cloc.sourceforge.net v 1.62 T = 1.13 s(134.1 files / s,9764.6 lines / s)
    ------------ -------------------------------------------------- -----------------
    语言文件空白评论代码
    -------------------- -------------------------------------------------- ---------
    Perl 149 2795 1425 6382
    JSON 1 0 0 270
    YAML 2 0 0 198
    ----------- -------------------------------------------------- -------------------
    SUM:152 2795 1425 6850
    ----------------- -------------------------------------------------- ------------



    替代方案



    手动运行命令



    如果你不想打扰savi并且安装shell脚本,您可以手动运行这些命令。例如:

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



    语言学家



    如果您希望结果完全符合GitHub的语言百分比,则可以尝试安装语言学家 a>而不是 CLOC 。根据其自述文件,您需要 gem install linguist ,然后运行 linguist 。我无法使它工作( issue#2223 )。


    In a Github repository you can see "language statistics", which displays the percentage of 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.

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


    This question (Count number of lines in a git repository) asks how to count the lines of code in a local Git repository, but:

    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.

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

    解决方案

    A shell script, cloc-git

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

    #!/usr/bin/env bash
    git clone --depth 1 "$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. cloc can probably be installed with your package manager – for example, brew install cloc with Homebrew.

    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 $PATH such as /usr/local/bin.

    Usage

    The script takes one argument, which is any URL that git clone will accept. Examples are https://github.com/evalEmpire/perl5i.git (HTTPS) or git@github.com:evalEmpire/perl5i.git (SSH). You can get this URL from any GitHub project page by clicking "Clone or download".

    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:

    $ 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 Linguist instead of CLOC. According to its README, you need to gem install linguist and then run linguist. I couldn’t get it to work (issue #2223).

    这篇关于你能从Github存储库获取代码行数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆