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

前端 未结 15 1109
庸人自扰
庸人自扰 2020-12-04 04:26

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 ho

相关标签:
15条回答
  • 2020-12-04 04:48

    There in another online tool that counts lines of code for public and private repos without having to clone/download them - https://klock.herokuapp.com/

    screenshot

    0 讨论(0)
  • 2020-12-04 04:49

    From the @Tgr's comment, there is an online tool : https://codetabs.com/count-loc/count-loc-online.html

    0 讨论(0)
  • 2020-12-04 04:50

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

    However, if the question is "can you quickly check 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.

    E.g.

    How big is the "docker" project?

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

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

    {
        ...
        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)

    0 讨论(0)
  • 2020-12-04 04:52

    Firefox add-on Github SLOC

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

    0 讨论(0)
  • 2020-12-04 04:54
    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.js needs to be installed).
    npm install sloc -g
    
    1. Clone shallow repository (faster download than full clone).
    git clone --depth 1 https://github.com/facebook/react/
    
    1. Run sloc and specifiy the path that should be analyzed.
    sloc ".\react\src" --format cli-table
    

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

    1. Delete repository folder (optional)

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

    Screenshots of the executed steps (cli-table):

    sloc output (no arguments):

    It is also possible to get details for every file with the --details option:

    sloc ".\react\src" --format cli-table --details     
    
    0 讨论(0)
  • 2020-12-04 04:56

    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 lines of code.

    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...

    $ 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 acknowledges but the API does not.

    Finally, you can use code search to 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 url for each file.

    0 讨论(0)
提交回复
热议问题