Skip to content

Instantly share code, notes, and snippets.

@mloberg
Created September 19, 2012 16:35
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mloberg/3750653 to your computer and use it in GitHub Desktop.
Save mloberg/3750653 to your computer and use it in GitHub Desktop.
Find file in git based on md5 checksum.
#!/bin/sh
CHECKSUM=$1
FILE=$2
if [[ -z "$CHECKSUM" ]]; then
echo "Usage: $0 md5 file"
exit 1
elif [[ -z "$FILE" ]]; then
echo "Usage: $0 md5 file"
exit 1
fi
# Check if valid git repo
git status &> /dev/null
if [[ $? -ne 0 ]]; then
echo "Not a valid git repo."
exit 1
fi
# git revision for file
REVS=`git log --pretty=%H -- $FILE`
# check each revision for checksum
for rev in $REVS; do
git show $rev:$FILE > _file_to_check
# if you are on a Linux system, change md5 to md5sum
if [[ -n `md5 _file_to_check | grep $CHECKSUM` ]]; then
echo $rev
fi
rm _file_to_check
done
@giosh94mhz
Copy link

I just found this piece of code which helped me while debugging. Just in case someone else will need it, here's my changes:

 # Check if valid git repo
-git status &> /dev/null
+ROOT=$(git rev-parse --show-toplevel)
 if [[ $? -ne 0 ]]; then
     echo "Not a valid git repo."
     exit 1
 fi
+cd "$ROOT"
-if [[ -n `md5 _file_to_check | grep $CHECKSUM` ]]; then
+if [[ -n `md5sum _file_to_check | grep $CHECKSUM` ]]; then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment