-
-
Save xai/3f6f3c0518e77bc87afd922519a9b370 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/sh | |
usage() { | |
echo "Usage: $0 [-m] [-s] hash file" | |
echo "\t-m use md5 for hashing" | |
echo "\t-s use sha1 for hashing (this is the default)" | |
exit 1 | |
} | |
HASHCMD="sha1sum" | |
while getopts ":m:s" opt; do | |
case $opt in | |
m) | |
HASHCMD="md5sum" | |
shift | |
;; | |
s) | |
HASHCMD="sha1sum" | |
shift | |
;; | |
esac | |
done | |
CHECKSUM=$1 | |
FILE=$2 | |
if [ -z "$CHECKSUM" -o -z "$FILE" ]; then | |
usage | |
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 | |
cd $ROOT | |
# git revision for file | |
REVS=$(git rev-list --all -- $FILE) | |
# temp file | |
file_to_check=$(mktemp) | |
# check each revision for checksum | |
FOUND="" | |
for rev in $REVS; do | |
git show $rev:$FILE > $file_to_check 2>/dev/null | |
if $HASHCMD $file_to_check | grep -q $CHECKSUM; then | |
FOUND="$rev" | |
# intentionally no break to see if we find an older revision | |
# insert a break if you want the most recent commit instead of the oldest | |
fi | |
done | |
# cleanup | |
rm $file_to_check | |
# output | |
if [ -n "$FOUND" ]; then | |
echo "$FOUND" | |
exit 0 | |
else | |
echo "Not found: $CHECKSUM $FILE" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment