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:
All in all, this is potentially far too time-intensive for “quickly checking the scale of a project”.
The most direct way to count lines of code (LOC) is to, well, count lines of code. Our IDE tells us how many lines of text a file has and displays a count in one of the margins. It's a useful metric to have: a quick way to see how long a given method is or object has.
To use cloc simply type cloc followed by the file or directory which you wish to examine. Now lets run cloc on it. As you can see it counted the number of files, blank lines, comments and lines of code. Another cool feature of cloc is that can even be used on compressed files.
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
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. There is also a docker image published under 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 $PATH
such as /usr/local/bin
.
The script takes one argument, which is any URL that git clone
will 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”.
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 -------------------------------------------------------------------------------
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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With