-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-checkhash
executable file
·68 lines (60 loc) · 2.6 KB
/
git-checkhash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
# Author: Gokberk Cinbis, 2017
set -e
if [ "$#" -eq 1 ]; then
file="$1"
else
echo 'git-checkhash <file>'
echo ''
echo 'Check the current git hash to make sure the right code-base is being used for evaluating a pre-trained model'
echo 'Test fails if there are uncommitted changes or the current commit hash differs from the one in <file>.'
echo ''
echo 'EXIT CODES'
echo ' 0 if check passes'
echo ' non-zero otherwise, with following details:'
echo ' 1 if <file> does not exist'
echo ' 2 if there are uncommitted changes'
echo ' 3 if working copy corresponds to a different commit'
echo ' 4 if the number of input args is invalid'
echo ' some non-zero if git-writehash reports another error'
echo ''
echo 'Important: for some errors, there may not be any error message printed on screen'
echo ''
echo 'Suggested use of the exit code:'
echo ' if git-checkhash <file>; then echo "git-checkhash passed"; else exit $?; fi'
echo ''
echo 'SUGGESTED USE'
echo 'Call git-uniqueid to generate a log directory in the beginning of an experiment script'
echo 'Call git-writehash to write the current hash into a file into the log directory'
echo 'Call git-checkhash at test time to make sure that the right revision is being used at evaluation time (if necessary)'
echo ''
echo 'PACKAGE'
echo 'git-uniqueid Generate a unique log file/directory name for running an experiment'
echo 'git-writehash Write git hash into a file'
echo 'git-checkhash Check whether the current git hash to make sure the right code-base is being used for evaluating a pre-trained model'
exit 4
fi
if [ ! -f $file ]
then
echo "git-checkhash: $file does not exit."
exit 1
fi
idf=$(command cat $file)
# handle this explicitly to report the problem:
if [[ ! -z $(command git status -s) ]]; then # see https://stackoverflow.com/questions/3878624/how-do-i-programmatically-determine-if-there-are-uncommited-changes
echo "git-checkhash: there are uncommitted changes. Checkout version ${idf} to use the right version."
exit 2
fi
idg=$(command git-writehash) # exits automatically if git-writehash returns a non-zero exit code.
# trim
idf=$(echo -e "${idf}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
idg=$(echo -e "${idg}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ "${idf}" == "${idg}" ]
then
echo "git-checkhash: OK."
exit 0
else
echo "git-checkhash: hash differs. Checkout version ${idf} to use the right version."
# RUN: "git checkout ${idf}"
exit 3
fi