Skip to content

Instantly share code, notes, and snippets.

@zulonas
Forked from mloberg/gist:3750653
Last active December 26, 2022 11:45
Show Gist options
  • Save zulonas/5f4eef6859762e0f4da93ca3d69e19f9 to your computer and use it in GitHub Desktop.
Save zulonas/5f4eef6859762e0f4da93ca3d69e19f9 to your computer and use it in GitHub Desktop.
Find file in git based on md5 checksum.
#!/bin/bash
CHECKSUM=$1
FILE=$2
if [[ -z "$CHECKSUM" ]]; then
echo "Usage: $0 md5 file"
exit 1
fi
if [[ -z "$FILE" ]]; then
echo "Usage: $0 md5 file"
exit 1
fi
# Check if valid git repo
ROOT=$(git rev-parse --show-toplevel)
if [[ $? -ne 0 ]]; then
echo "Not a valid git repo."
exit 1
fi
# Get git revisions for file
REVS=$(git --no-pager log --pretty=%H $FILE)
# Get file path in git repo
FILE_REPO_PATH=$(git ls-files --full-name $FILE)
# Check each revision for checksum
TEMP_FILE="$(mktemp)"
for rev in $REVS; do
git show $rev:$FILE_REPO_PATH > $TEMP_FILE
if [[ -n `md5sum $TEMP_FILE | grep $CHECKSUM` ]]; then
echo $rev
break
fi
done
rm -f $TEMP_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment