Skip to content

Instantly share code, notes, and snippets.

@ivy
Created December 26, 2024 19:46
Show Gist options
  • Save ivy/35e3a6574f0d06cddbcace53896462b1 to your computer and use it in GitHub Desktop.
Save ivy/35e3a6574f0d06cddbcace53896462b1 to your computer and use it in GitHub Desktop.
Bash shell script to find the Git commit with the most changed files.
#!/bin/bash
#
# find-git-commit-with-most-changed-files -- I needed to test pagination with
# GitHub's pull request files API[1], so I made this script to find the biggest
# commit in a Git repository by iterating over each commit in the repository and
# counting the number of changed files.
#
# Written with assistance from OpenAI's o1-preview model.
#
# [1]: https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-pull-requests-files
# Check if inside 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
biggest_commit=0
biggest_commit_sha=''
# Iterate over all commits in the repository
git rev-list --all | while read commit_hash; do
printf '.'
# Get the number of files changed in this commit
num_files_changed=$(git diff-tree --no-commit-id --name-only -r "$commit_hash" | wc -l)
if [ "$num_files_changed" -gt "$biggest_commit" ]; then
biggest_commit=$num_files_changed
biggest_commit_sha="$commit_hash"
echo
echo "New biggest commit: $biggest_commit_sha with $biggest_commit changed files."
fi
# Check if the number of files changed is greater than 3000
#
# NOTE(ivy): This is to help me find a good pull request to test pagination
# with GitHub's pull request files API
if [ "$num_files_changed" -gt 3000 ]; then
# Output the commit hash and the number of files changed
echo
echo '========================================================='
echo "Commit $commit_hash has $num_files_changed changed files."
echo '========================================================='
fi
done
echo "Biggest commit found: $biggest_commit_sha with $biggest_commit changed files."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment