Last active
September 29, 2024 19:44
-
-
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
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
// Reference: http://stackoverflow.com/questions/4822471/count-number-of-lines-in-a-git-repository | |
$ git ls-files | xargs wc -l |
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"
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
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