Last active
October 24, 2024 09:00
-
-
Save lawndoc/886d28f8c863de615bf47c6139773590 to your computer and use it in GitHub Desktop.
Count the total lines of code for a user or organization in GitHub (excludes forks)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Author: C.J. May @lawndoc | |
# Usage: cloc-gh <username> | |
# Prereqs: cloc gh | |
cloc_repo () { | |
gh repo clone "$1" temp-linecount-repo -- --depth 1 > /dev/null 2>&1 && | |
cloc temp-linecount-repo | grep SUM | awk '{ print $5 }' >> line_count.txt && | |
rm -rf temp-linecount-repo | |
} | |
progress_bar () { | |
# Process data | |
let _progress=(${1}*100/${2}*100)/100 | |
let _done=(${_progress}*4)/10 | |
let _left=40-$_done | |
# Build progressbar string lengths | |
_fill=$(printf "%${_done}s") | |
_empty=$(printf "%${_left}s") | |
printf "\r\033[KProgress : [${_fill// /\#}${_empty// /-}] ${_progress}%% -- " | |
} | |
echo "Enumerating $1 repos..." | |
# ignores forked repos | |
gh repo list $1 --limit 10000 --json nameWithOwner,isFork --jq '.[] | select( .isFork | not ) | .nameWithOwner' > repos.txt | |
let _total=$(awk 'END{print NR}' repos.txt) | |
let _current=1 | |
echo "Counting lines of code..." | |
while read repo; do | |
progress_bar $_current $_total | |
printf "$repo" | |
cloc_repo $repo | |
let "_current+=1" | |
done < repos.txt | |
printf "\nDone.\n" | |
printf "Total lines of code in $1: " | |
awk '{ sum += $1 } END { print sum }' line_count.txt | |
rm repos.txt line_count.txt |
Update the --json fields and --jq query to filter on different sets of repos.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use this script, you'll need to install
cloc
andgh
, and you'll also need to sign intogh
with your account. It should then have access to all private repos and organizations you have access to.