-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-uniqueid
executable file
·104 lines (93 loc) · 3.53 KB
/
git-uniqueid
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
# Author: Gokberk Cinbis, 2017
set -e
#set -o pipefail -e # maybe this instead?
function helpandpanic()
{
echo 'git-uniqueid <format>'
echo
echo 'Generate a unique experiment id based on date and git hash.'
echo 'The intended use is to tag the experimental outputs of the current source code.'
echo
echo FORMAT OPTIONS
echo 'date_time_branch_hash_proc (default)'
echo 'date_time_hash_proc'
echo
echo 'GIT HASH STRING'
echo 'Git directory, no modification: only first few characters of the hash is shown'
echo 'Git directory, with modified files: <githash>_modified'
echo 'Not a git directory: "nogit"'
echo
echo 'OTHER VARIABLES'
echo 'branch: The current branch. When in detached-HEAD mode, this is just "HEAD".'
echo 'proc: hostname.pid, where pid belongs to current script, provided just to reduce the chance of a name conflict'
echo
echo 'EXIT CODES'
echo ' 0 if the git directory is "clean"'
echo ' 1 if the git directory has uncommited changes'
echo ' 2 if not a git repository'
echo ' 3 if arguments are not recognized'
echo
echo 'Suggested use of the exit code:'
echo ' if id=$(git-uniqueid); then'
echo ' echo run experiment with log id $id'
echo ' else'
echo ' echo "there are uncommitted changes or not a git directory"'
echo ' fi'
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
}
if [ "$#" -eq 0 ]; then
format="date_time_branch_hash_proc"
elif [ "$#" -eq 1 ]; then
format="$1"
else
helpandpanic
fi
# date, time, process info
d=`command date +%Y%m%d`
t=`command date +%H.%M.%S` # hour.minute.seconds (dots are useful for time, otherwise very unreadible)
host=`command hostname`
pid="$$"
proc="${host}.${pid}"
# git information
# https://stackoverflow.com/questions/2180270/check-if-current-directory-is-a-git-repository
if git rev-parse --git-dir > /dev/null 2>&1; then
# https://stackoverflow.com/questions/4120001/what-is-the-git-equivalent-for-revision-number
# No, this is not reliable and wrong if we're in detached-HEAD state.
# 'index: commit index relative to the first one, a hacky commit index'
# commitindex=`git rev-list --full-history --all --abbrev-commit | wc -l | sed -e 's/^ *//'` # commit index
set +e # forgive errors below
h=`command git-writehash` # hash
writehash_exit=$?
set -e # exit on error
branch=`command git rev-parse --abbrev-ref HEAD`
if [[ -z $(command git status -s) ]]; then # see https://stackoverflow.com/questions/3878624/how-do-i-programmatically-determine-if-there-are-uncommited-changes
rc=$writehash_exit # return code is zero unless git-writehash has reported an error
else
# git-writehash does this: h=${h}_modified
rc=1 # return code
fi
# ih="${commitindex}_${h}"
else
h='notgit' # hash
branch=''
ih='notgit' # index_hash
rc=2 # return code
fi
if [ "$format" == "date_time_branch_hash_proc" ]; then
branch="${branch}_"
echo ${d}_${t}_${branch}${h}_${proc}
elif [ "$format" == "date_time_hash_proc" ]; then
echo ${d}_${t}_${h}_${proc}
#elif [ "$format" == "date_index_hash_time_proc" ]; then
# echo ${d}_${ih}_${t}.${proc}
else
echo "UNRECOGNIZED FORMAT"
helpandpanic
fi
exit $rc