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?
The question “Count number of lines in a git repository” asks how to count the lines of code in a local Git repository, but:
- You have to clone the project, which could be massive. Cloning a project like Wine, for example, takes ages.
- You would count lines in files that wouldn’t necessarily be code, like i13n files.
- 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”.
There is an extension for Google Chrome browser - GLOC which works for public and private repos.
Counts the number of lines of code of a project from:
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
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 comment for details.)
UPDATE:
This data is also available in GitHub's API. So I wrote a quick script to fetch the data and do the calculation:
Just paste it in a Chrome DevTools snippet, change the repo and click run.
Disclaimer (thanks to 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.
UPDATE:
Looks like this method to calculate total line numbers isn't entirely reliable. Take a look at orbitbot's comment for details.
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:
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
, runningchmod +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 arehttps://github.com/evalEmpire/perl5i.git
(HTTPS) orgit@github.com:evalEmpire/perl5i.git
(SSH). You can get this URL from any GitHub project page by clicking “Clone or download”.Example output:
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:
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 runlinguist
. I couldn’t get it to work (issue #2223).You can use GitHub API to get the sloc like the following function
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.
You can download from here https://chrome.google.com/webstore/detail/github-sloc/fkjjjamhihnjmihibcmdnianbcbccpnn
Source code is available here https://github.com/martianyi/github-sloc
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...
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 returnedurl
for each file.