Created
September 13, 2018 05:50
Locate the git commit of a file that contains a specific MD5 checksum
This file contains hidden or 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 | |
# | |
# 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