Programming

Cloc – Count line of Code

Ever wondered how did large projects count their total line of code? For example, Linux foundation published this video showing off kernel’s impressive codebase. But how did they calculate it? They counted it with Cloc.

Cloc:

Cloc is a command line utility that uses regular expression to count the number of line that is present in a github repository. It does that by a complex algorithm, which essentially boils down to counting total lines from each of the diffable files, then substracting the whitespace, comments and blank lines, then combining them in total. It also pretty formats the file in a human readable format. Let’s see how you can get cloc running using a custom script (for global invocation).

  • Install cloc following the github instruction.
  • Create a file called clockaru in a folder of your preference and paste this code into it :
    #!/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
  • Now let’s give it executable permission with chmod +x clokaru
  • Move the file to $PATH with sudo mv clokaru /usr/local/bin
  • Now we’re ready to use cloc from anywhere (without needing to keep unnecessary downloaded stuff) like this clokaru <github repo link>

Example:

Here’s an example of cloc’s result of a personal project of mine

 

Tagged , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *