Last active
August 8, 2023 15:53
-
-
Save signalwerk/7b4411c13073da371b5d3d5d4d73cc9b to your computer and use it in GitHub Desktop.
Count code lines in git repo
This file contains hidden or 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
#!/bin/bash | |
# run | |
# bash <(curl https://gist.githubusercontent.com/signalwerk/7b4411c13073da371b5d3d5d4d73cc9b/raw/count.sh) | |
# Define the output CSV file | |
output_csv="code_lines.csv" | |
# Header for the CSV file | |
echo "Commit,Year,Month,Date,Total Lines,Largest File" > "$output_csv" | |
# Get all the commits and their dates in reverse order | |
commits_dates=$(git log --reverse --pretty=format:"%H,%ad" --date=short) | |
# Initialize variables to keep track of the previous month and year | |
prev_month="" | |
prev_year="" | |
# Loop through each commit and calculate the total lines of code on the first day of each month | |
while IFS=',' read -r commit date; do | |
# Extract the day, month, and year from the commit date | |
day=$(echo "$date" | cut -d'-' -f3) | |
month=$(echo "$date" | cut -d'-' -f2) | |
year=$(echo "$date" | cut -d'-' -f1) | |
# Check if it's the first day of the month (day = 01) and a new month or year | |
if [ "$month" != "$prev_month" ] || [ "$year" != "$prev_year" ]; then | |
total_lines=0 | |
largest_file="" | |
largest_file_size=0 | |
files=$(git ls-tree --name-only -r "$commit") | |
# Loop through each file and count lines of code | |
for file in $files; do | |
if [[ $file =~ \.(php|js|sh|vue|ts|scss|gql|graphql|twig)$ ]] && [[ ! $file =~ /dist/ ]] && [[ ! $file =~ graphql/types ]] && [[ ! $file =~ graphql/schema ]]; then | |
lines=$(git show "$commit:$file" | wc -l) | |
lines=${lines//[[:blank:]]/} | |
lines=$((lines + 0)) | |
total_lines=$((total_lines + lines)) | |
# Keep track of the largest file | |
if [ $((lines + 0)) -gt $((largest_file_size + 0)) ]; then | |
largest_file_size=$lines | |
largest_file=$file | |
fi | |
fi | |
done | |
# Output to CSV | |
echo "$commit,$year,$month,$date,$total_lines,$largest_file" >> "$output_csv" | |
# Update the previous month and year variables | |
prev_month=$month | |
prev_year=$year | |
fi | |
done <<< "$commits_dates" | |
echo "CSV file '$output_csv' generated successfully." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment