Skip to content

Instantly share code, notes, and snippets.

@mandiwise
Last active September 29, 2024 19:44
Show Gist options
  • Save mandiwise/dc53cb9da00856d7cdbb to your computer and use it in GitHub Desktop.
Save mandiwise/dc53cb9da00856d7cdbb to your computer and use it in GitHub Desktop.
A command to calculate lines of code in all tracked files in a Git repo
// Reference: http://stackoverflow.com/questions/4822471/count-number-of-lines-in-a-git-repository
$ git ls-files | xargs wc -l
@alexivkin
Copy link

alexivkin commented Apr 9, 2023

I made an app to do it completely remotely, on a forked repo, without having to clone it - https://klock.herokuapp.com/

This does not work anymore. Does it have a new home?

I guess this was a free app. Heroku recently removed hosting apps for free on their platform

author here. yes it was and yes they killed it. I can resurrect it on another free platform if there is interest in it.
or you can just run it locally - https://github.com/alexivkin/klock

@Dicklesworthstone
Copy link

I thought I'd add my own command here in case it helps anyone:

git ls-files | grep -vE '\.(webp|ttf|json|png|js)$' | xargs wc -l | sort -nr

This counts lines of code ignoring various image file types, fonts, json/js (I'm counting code in a python project and the .js would distort things). It sorts it by size for you too.

@Animeshz
Copy link

Or instead of filtering via inversion filter, you can

git ls-files | xargs -I{} sh -c "file '{}' | grep -q text && echo '{}'" | sort -nr

@louika
Copy link

louika commented Nov 12, 2023

omg thank you ❤️

@andyg2
Copy link

andyg2 commented Feb 22, 2024

I find this pretty useful (for public repos) https://codetabs.com/count-loc/count-loc-online.html

Plus there's an open API: https://api.codetabs.com/v1/loc?github=bluesky-social/social-app

[
 {
  "language": "TypeScript",
  "files": 620,
  "lines": 83349,
  "blanks": 5334,
  "comments": 2259,
  "linesOfCode": 75756
 },
 {
  "language": "JavaScript",
  "files": 23,
  "lines": 705,
  "blanks": 40,
  "comments": 46,
  "linesOfCode": 619
 },
 {
  "language": "SVG",
  "files": 23,
  "lines": 55,
  "blanks": 0,
  "comments": 0,
  "linesOfCode": 55
 },
 {
  "language": "Markdown",
  "files": 13,
  "lines": 689,
  "blanks": 201,
  "comments": 0,
  "linesOfCode": 488
 },
 {
  "language": "Go",
  "files": 9,
  "lines": 903,
  "blanks": 114,
  "comments": 64,
  "linesOfCode": 725
 },
 {
  "language": "JSON",
  "files": 9,
  "lines": 512,
  "blanks": 0,
  "comments": 0,
  "linesOfCode": 512
 },
 {
  "language": "Patch",
  "files": 9,
  "lines": 3968,
  "blanks": 17,
  "comments": 0,
  "linesOfCode": 3951
 },
 {
  "language": "YAML",
  "files": 8,
  "lines": 466,
  "blanks": 43,
  "comments": 30,
  "linesOfCode": 393
 },
 {
  "language": "HTML",
  "files": 7,
  "lines": 707,
  "blanks": 42,
  "comments": 16,
  "linesOfCode": 649
 },
 {
  "language": "Shell",
  "files": 5,
  "lines": 126,
  "blanks": 24,
  "comments": 25,
  "linesOfCode": 77
 },
 {
  "language": "Swift",
  "files": 5,
  "lines": 383,
  "blanks": 55,
  "comments": 41,
  "linesOfCode": 287
 },
 {
  "language": "gitignore",
  "files": 3,
  "lines": 124,
  "blanks": 23,
  "comments": 31,
  "linesOfCode": 70
 },
 {
  "language": "Makefile",
  "files": 2,
  "lines": 81,
  "blanks": 19,
  "comments": 4,
  "linesOfCode": 58
 },
 {
  "language": "Plain Text",
  "files": 2,
  "lines": 13,
  "blanks": 1,
  "comments": 0,
  "linesOfCode": 12
 },
 {
  "language": "C Header",
  "files": 1,
  "lines": 3,
  "blanks": 0,
  "comments": 0,
  "linesOfCode": 3
 },
 {
  "language": "Dockerfile",
  "files": 1,
  "lines": 74,
  "blanks": 17,
  "comments": 9,
  "linesOfCode": 48
 },
 {
  "language": "Gemfile",
  "files": 1,
  "lines": 6,
  "blanks": 2,
  "comments": 1,
  "linesOfCode": 3
 },
 {
  "language": "JSX",
  "files": 1,
  "lines": 9,
  "blanks": 1,
  "comments": 0,
  "linesOfCode": 8
 },
 {
  "language": "License",
  "files": 1,
  "lines": 7,
  "blanks": 3,
  "comments": 0,
  "linesOfCode": 4
 },
 {
  "language": "Objective C",
  "files": 1,
  "lines": 26,
  "blanks": 5,
  "comments": 0,
  "linesOfCode": 21
 },
 {
  "language": "TypeScript Typings",
  "files": 1,
  "lines": 8,
  "blanks": 0,
  "comments": 0,
  "linesOfCode": 8
 },
 {
  "language": "Total",
  "files": 745,
  "lines": 92214,
  "blanks": 5941,
  "comments": 2526,
  "linesOfCode": 83747
 }
]

@lucasortigoso
Copy link

lucasortigoso commented Mar 4, 2024

An improvement based on what has already been said, by the way thanks for the tip.

Line counter for child folders with aggregator removing image files (webp|ttf|png|jpg|jpeg) :

How to use:

bash counter.sh foldername

counter.sh:

#!/bin/bash

# Root Folder
root_dir=$1

#  Count initialization
total_lines=0

# Loop
for folder in "$root_dir"/*; do
    # Check is folder
    if [ -d "$folder" ] && [ ! "$(basename "$folder")" = ".*" ] && [ -d "$folder/.git" ]; then
        # Run count command
        lines_in_folder=$(cd "$folder"; git ls-files | grep -vE '\.(webp|ttf|png|jpg|jpeg)$' | sed 's/.*/"&"/' |  xargs wc -l | grep -o "[0-9]* total" | awk '{SUM += $1} END {print SUM}')
        current_branch=$(cd "$folder"; git rev-parse --abbrev-ref HEAD)
        printf "%-80s -> %s lines\n" "$(basename "$folder") ($current_branch)" "$lines_in_folder"

        
        # Increment total lines
        total_lines=$((total_lines + lines_in_folder))
        
    fi
done

# Show total lines
echo "Total: $total_lines lines"

@ZariahDonovan
This comment was marked as a violation of GitHub Acceptable Use Policies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment