Skip to content

Instantly share code, notes, and snippets.

@gojimmypi

gojimmypi/GitHashCheck.sh

Last active Mar 7, 2020
Embed
What would you like to do?
Is the specified file the same file found in GitHub repo?
#!/bin/bash
#"***************************************************************************************************"
# CheckForGitFileChange() bash function. Compare hash of local file to one on GitHub
#
# The MIT License (MIT)
#
# Copyright (c) 2020 gojimmypi
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Note that checking out a repo in Windows command prompt, then comparing
# in WSL may have unexpected results. See also git status for each.
#
# example: check specific file:
# CheckForGitFileChange ~/workspace/ulx3s-toolchain/README.md
#
# example: check this bash file:
# CheckForGitFileChange $0
#
# see https://gist.github.com/gojimmypi/22b7d43ce69e9c828e97534ff0a1d27f
#
#"***************************************************************************************************"
CheckForGitFileChange() {
SAVED_CURRENT_PATH=$(pwd)
if [ ! -f "$1" ]; then
echo "File not found: "$1
return 1
fi
## in case we get called from some other directory, we need to know the entire path of this file
## as git commands need to be run in the repository directory.
GIT_REL_PATH=$(dirname $1 | tr --delete '\t\r\n')
GIT_FULL_PATH="$(cd "${GIT_REL_PATH}"; pwd)"
GIT_FILE_NAME=$(basename $1 | tr --delete '\t\r\n' )
GIT_THIS_BRANCH=$(git rev-parse --abbrev-ref HEAD)
cd $GIT_FULL_PATH
echo "Checking $GIT_FULL_PATH/$GIT_FILE_NAME on branch origin/$GIT_THIS_BRANCH"
# compares occur to local files only, so quietly fetch
git fetch > /dev/null
# we first need to get the latest commit hash (non-blank if remote is newer)
COMMIT_HASH=$(git show --format=%H --no-patch --no-abbrev-commit HEAD..origin/$GIT_THIS_BRANCH)
# view the file in the commit hash found and pipe to `git hash-object` to compute hash
GIT_HASH=$(git show $COMMIT_HASH:$1 | git hash-object --stdin )
# simple `git hash-object` to compute hash of the local file
THIS_HASH=$(git hash-object $1)
# some visual entertainment
echo "Remote URL = $(git config --get remote.origin.url)"
echo "COMMIT_HASH = $COMMIT_HASH"
echo "GIT_HASH = $GIT_HASH"
echo "THIS_HASH = $THIS_HASH"
if [ "$THIS_HASH" == "$GIT_HASH" ]; then
echo "Confirmed $1 is the most recent version found in GitHub."
else
echo "Warning! This version of $1 does not match the most recent version in GitHub!"
git status | tr -s ' ' | grep "modified: $1"
read -p "Press enter to continue, or Ctrl-C to abort. (manually push/pull recent file)"
fi
cd $SAVED_CURRENT_PATH
}
if [ "$1" == "" ]; then
CheckForGitFileChange $0
else
CheckForGitFileChange $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment