-
-
Save turboBasic/7b5ac4b1524f19b4520ff61a4a9e78ed to your computer and use it in GitHub Desktop.
Calculate size of a git commit object #git
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
# Calculate the size of a git commit in bytes. | |
function git-commit-size() { | |
# This script calculates the size of a git commit in bytes. | |
# Usage: git-commit-size.sh <commit hash> | |
# | |
# Example: | |
# git-commit-size.sh 1234567 | |
# | |
# Output: | |
# The total size of the commit in bytes, formatted with thousands separators. | |
local BLOB_HASH_LIST COMMIT_SIZE HASH ITEM_LIST SIZE_LIST | |
if (( $# != 1 )); then | |
echo "Usage: git-commit-size.sh <commit hash>" 1>&2 | |
return 1 | |
fi | |
HASH=$1 | |
# -r: recurse; -c: show differences from each parent of a merge commit simulaneously | |
ITEM_LIST=$( | |
git -c diff.renameLimit=0 diff-tree -r -c --find-copies --find-renames --no-commit-id "$HASH" | |
) | |
BLOB_HASH_LIST=$(echo "$ITEM_LIST" | awk '{ print $4 }') | |
SIZE_LIST="$( | |
echo "$BLOB_HASH_LIST" \ | |
| git cat-file --batch-check \ | |
| grep "blob" \ | |
| awk '{ print $3}' | |
)" | |
COMMIT_SIZE="$(echo "$SIZE_LIST" | awk '{ sum += $1 } END { print sum }')" | |
LC_NUMERIC=en_US.UTF-8 printf "%'.0f\n" "$COMMIT_SIZE" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment