-
-
Save zulonas/5f4eef6859762e0f4da93ca3d69e19f9 to your computer and use it in GitHub Desktop.
Find file in git based on md5 checksum.
This file contains 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
#!/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