Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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

like image 838
Hubro Avatar asked Nov 12 '14 07:11

Hubro


People also ask

How do I know how many lines of code?

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.

How do you find the lines of code in a project?

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.


1 Answers

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

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

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

like image 118
Rory O'Kane Avatar answered Oct 09 '22 03:10

Rory O'Kane