Skip to content

Instantly share code, notes, and snippets.

@MelbourneDeveloper
Created December 20, 2023 09:17

Revisions

  1. MelbourneDeveloper created this gist Dec 20, 2023.
    50 changes: 50 additions & 0 deletions mostchanged.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    #!/bin/bash

    # This script finds the files with the most changes in a Git repository, tracking renames and excluding binary files and files with zero changes

    # Check for the presence of a Git repository
    if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
    echo "This script must be run inside a Git repository."
    exit 1
    fi

    # Create a temporary file to store changes count
    temp_file=$(mktemp)

    # Loop through all files in the Git history, excluding binaries
    git ls-tree -r --name-only HEAD | while read file; do
    if [ -n "$file" ]; then
    # Use git diff to check if the file is binary
    if ! git diff --numstat -- "$file" | grep -q '^-\s-\s'; then
    # Count changes for each non-binary file, tracking renames
    changes=$(git log --follow --numstat --format="%n" -- "$file" | awk '{ add += $1; del += $2 } END { print add + del }')
    if [ "$changes" -ne 0 ]; then
    echo "$changes $file" >> "$temp_file"
    fi
    fi
    fi
    done

    # Sort the results numerically and reverse
    sort -nr "$temp_file" | awk '!seen[$2]++' | head -n 30 | while read changes file; do
    # Determine the emoji based on the count of changes
    if [ "$changes" -ge 10000 ]; then
    emoji="⛔️"
    elif [ "$changes" -ge 5000 ]; then
    emoji="🧨"
    elif [ "$changes" -ge 2500 ]; then
    emoji="🚨"
    elif [ "$changes" -ge 1000 ]; then
    emoji="‼️"
    elif [ "$changes" -ge 500 ]; then
    emoji="⚠️"
    elif [ "$changes" -ge 250 ]; then
    emoji="🔔"
    else
    emoji=""
    fi
    echo "$emoji $changes $file"
    done

    # Clean up the temporary file
    rm "$temp_file"