Skip to content

Instantly share code, notes, and snippets.

@boltronics
Created September 13, 2018 05:50
Show Gist options
  • Save boltronics/9f7eac74399b5678d9de9e9be1d6e3a5 to your computer and use it in GitHub Desktop.
Save boltronics/9f7eac74399b5678d9de9e9be1d6e3a5 to your computer and use it in GitHub Desktop.
Locate the git commit of a file that contains a specific MD5 checksum
#!/bin/bash
#
# Find a specific version of a file in the current directory by
# searching back through Git commit history. The file version is
# identified by the MD5 checksum.
#
declare -r check_file="${1}"
declare -r target_hash="${2}"
declare -r orig_commit="$(git rev-parse HEAD)"
declare checksum
if [ -z "${js_hash}" -a -z "${target_hash}" ]
then
echo "Usage: $(basename "${0}") FILE HASH" 1>&2
exit 1
elif [ ! -f "${js_hash}" ]
then
echo "Error: ${check_file} not found."
exit 1
fi
for commit in $(git log --pretty=oneline | cut -d ' ' -f 1)
do
git checkout --quiet "${commit}"
if [ ! -f "${check_file}" ]
then
echo "${check_file} no longer found"
break
fi
checksum="$(md5sum "${check_file}" | cut -d ' ' -f 1)"
if [ "${checksum}" = "${target_hash}" ]
then
echo "Target hash found in commit ${commit}"
break
else
echo "${checksum} != ${target_hash}"
fi
done
git checkout --quiet "${orig_commit}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment