Skip to content

Instantly share code, notes, and snippets.

@philroche
Last active August 19, 2024 12:19
Show Gist options
  • Save philroche/b6e7e20a7e25e1d119b3067ea8fc70d2 to your computer and use it in GitHub Desktop.
Save philroche/b6e7e20a7e25e1d119b3067ea8fc70d2 to your computer and use it in GitHub Desktop.
Helpful script to summarize the commits between two git SHAs or branches or tags using the github API
#!/bin/bash -eu
# Helpful script to summarize the commits and files changes between two git SHAs or branches or tags using the github API
# Usage: ./github-compare-summary.sh <repo> <from_sha> <to_sha>
# Example: ./github-compare-summary.sh "awslabs/aws-c-http" "v0.8.7" "v0.8.8"
# Example: ./github-compare-summary.sh "awslabs/aws-c-http" "7db2452238caece2d3a91e6cbed75324edccea7d" "4e74ab1e3702763e0b87bd1752f5a37c2f0400ac"
# Example: ./github-compare-summary.sh "kubernetes-sigs/kubebuilder" "v4.1.1" "v4.2.0"
base_url="https://api.github.com/repos/"
# get the repo as the first argument
repo=$1
from_sha=$2
to_sha=$3
compare_url="${base_url}${repo}/compare/${from_sha}...${to_sha}"
# Fetch the data from the GitHub API
data=$(curl -s "${compare_url}")
# Print the total number of files changes
echo "Total files changed: $(echo "$data" | jq --raw-output '.files | length')"
# Print the summary of the file changes - total changes, additions, deletions
echo "$data" | jq --raw-output '
.files | reduce .[] as $item (
{"changes": 0, "additions": 0, "deletions": 0};
.changes += ($item.changes // 0) |
.additions += ($item.additions // 0) |
.deletions += ($item.deletions // 0)
) | "\nTotal changes: \(.changes)\nTotal additions: \(.additions)\nTotal deletions: \(.deletions)"'
# Print the details of the file changes
echo "$data" | jq --raw-output '.files[] | "\n* \(.filename)\n\tChanges: \(.changes)\n\tAdditions: \(.additions)\n\tDeletions: \(.deletions)"'
# Print the total number of commits
echo -e "\nTotal commits: $(echo "$data" | jq --raw-output '.total_commits')"
echo "$data" | jq --raw-output '.commits[] | "\n* \(.commit.message | split("\n") | first)\n\n\t\(.commit.author.name) (\(.commit.author.email)) @ \(.commit.author.date)\n"'
# print the human readable github compare URL by removing ``
echo -e "\nGitHub compare URL: https://github.com/${repo}/compare/${from_sha}...${to_sha}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment