-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-writehash
executable file
·62 lines (54 loc) · 2.02 KB
/
git-writehash
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
#!/bin/bash
# Author: Gokberk Cinbis, 2017
set -e
function helpandpanic()
{
echo 'git-writehash <file>'
echo 'Echo git hash'
echo ''
echo 'git-writehash <file>'
echo 'Write git hash into a file. Exists if file already exists. Writes <hash>_modified if there are uncommitted changes '
echo ''
echo 'EXIT CODES'
echo ' 0 normally'
echo ' 1 if there are uncommited changes'
echo ' 2 if <file> already exists'
echo ' 3 if the number of input args is invalid'
echo ' other if a subprocess fails (eg, not a git repository)'
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 3
}
# NO! cmd='command git rev-list --full-history --all | head -1'
# If we've checkout an old revision (ie. in detached-HEAD state), this returns an incorrect result.
h=$(command git rev-parse --verify HEAD)
# not a git repository: git-writehash returns 128.
if [[ -z $(command git status -s) ]]; then # see https://stackoverflow.com/questions/3878624/how-do-i-programmatically-determine-if-there-are-uncommited-changes
rc=0 # return code
else
h=${h}_modified
rc=1 # return code
fi
if [ "$#" -eq 0 ]; then
echo $h
exit $rc
elif [ "$#" -eq 1 ]; then
file="$1"
if [ -f "$file" ]
then
echo "git-writehash: "$file" already exists. First delete existing output file."
exit 1
fi
echo $h > "$file"
exit $rc
else
helpandpanic
fi