Skip to content

Instantly share code, notes, and snippets.

@archisgore
Last active May 30, 2024 18:43
Show Gist options
  • Save archisgore/5db20ea380491973c5eda0fc6b512566 to your computer and use it in GitHub Desktop.
Save archisgore/5db20ea380491973c5eda0fc6b512566 to your computer and use it in GitHub Desktop.
Count Lines of code in entire github org
#!/bin/bash
# This command clones all repos in a GitHub org and counts lines of code in each
# It can be re-run to collect new repos and pull the latest changes
set -euo pipefail
USAGE="Usage: $0 <user|org> [depth]"
[[ $# -eq 0 ]] && echo >&2 "missing arguments: ${USAGE}" && exit 1
type gh >/dev/null 2>&1 || { echo >&2 "I require GH CLI: https://cli.github.com but it's not installed. Aborting."; exit 1; }
type cloc >/dev/null 2>&1 || { echo >&2 "I require cloc: https://github.com/AlDanial/cloc but it's not installed. Aborting."; exit 1; }
function clone_org {
org=$1
limit=9999
if [[ "$2" != "" ]]; then
depth="--depth=$2"
fi
echo "Cloning all repos in org $org..."
repos="$(gh repo list "$org" -L $limit)"
repo_total="$(echo "$repos" | wc -l)"
repos_complete=0
echo
echo "$repos" | while read -r repo; do
repo_name="$(echo "$repo" | cut -f1)"
echo -ne "\r\e[0K[ $repos_complete / $repo_total ] Cloning $repo_name"
gh repo clone "$repo_name" "$repo_name" -- -q $depth 2>/dev/null || (
cd "$repo_name"
git pull -q
)
repos_complete=$((repos_complete + 1))
done
echo "Finished cloning all repos in $org."
}
# Clone all repos at a depth of 1
clone_org $1 1
# Count lines of code
cloc $org
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment