-
Notifications
You must be signed in to change notification settings - Fork 5
/
git-case-common
167 lines (125 loc) · 4.12 KB
/
git-case-common
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# common file for git-case
set -e
PROGRAM=$(basename $0)
# ------------------------------------------------------------------------
# helpers
function die() {
echo "$@" >&2
exit 1
}
# ------------------------------------------------------------------------
# make sure the user has a sane environment
if test -z "$GIT_COMMITTER_NAME" ; then
export GIT_COMMITTER_NAME=$(git config --get user.name)
fi
if test -z "$GIT_COMMITTER_EMAIL" ; then
export GIT_COMMITTER_EMAIL=$(git config --get user.email)
fi
test -z "$GIT_COMMITTER_NAME" \
-o -z "$GIT_COMMITTER_EMAIL" \
&& die "please set GIT_COMMITTER_NAME and GIT_COMMITTER_EMAIL"
LOCAL_GIT_VERSION=$(git version)
LOCAL_GIT_VERSION=${LOCAL_GIT_VERSION##* }
# we expect 1.5.3 or better
echo "${LOCAL_GIT_VERSION}" | tr '.' ' ' \
| ( read major minor patch rest ; \
test "$major" -gt 1 -o "$minor" -gt 5 -o "$patch" -ge 3 ) \
|| die "we need git 1.5.5 or better"
if ! ( echo $HOSTNAME | grep -q '.\..' ) ; then
export HOSTNAME=$(hostname --fqdn)
fi
echo $HOSTNAME | grep -q '.\..' \
|| die "HOSTNAME '$HOSTNAME' is not a fully qualified comain name"
# ------------------------------------------------------------------------
# find the .git dir
GIT_DIR=$(git rev-parse --git-dir)
if test "${GIT_DIR:0:1}" != / ; then
GIT_DIR="$(pwd)/$GIT_DIR"
fi
export GIT_DIR
# ------------------------------------------------------------------------
# define locations of case tracking bits
export GIT_CASE_HEAD="cases"
export GIT_CASE_DIR="$GIT_DIR/case"
export GIT_CASE_WORK_DIR="$GIT_CASE_DIR/tmp-$$"
export GIT_CASE_ACTIVE="$GIT_CASE_DIR/active"
export GIT_CASE_INDEX="$GIT_CASE_WORK_DIR/index"
# ------------------------------------------------------------------------
# prepare the redirection
export GIT_INDEX_FILE="$GIT_CASE_INDEX"
# ------------------------------------------------------------------------
# colours are pretty
black='\E[30;40m'
red='\E[31;40m'
green='\E[32;40m'
yellow='\E[33;40m'
blue='\E[34;40m'
magenta='\E[35;40m'
cyan='\E[36;40m'
white='\E[37;40m'
reset='\e[m'
bold='\e[1m'
underline='\e[4m'
blink='\e[5m'
inverse='\e[7m'
# ------------------------------------------------------------------------
# helper functions
function go_to_git_case_work_dir() {
test -d "$GIT_CASE_WORK_DIR" && die "stale $GIT_CASE_WORK_DIR already exists"
trap "cd $GIT_DIR ; rm -rf $GIT_CASE_WORK_DIR; exit" INT TERM EXIT
mkdir -p "$GIT_CASE_WORK_DIR"
cd "$GIT_CASE_WORK_DIR"
}
function generate_case_id() {
local description=$1
local date=$(date +%Y%m%d-%H%M%S.%N)
local salt="Commiter: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
Date: $date
Description: $description"
local sha1=$(echo "$salt" | git hash-object --stdin)
test -z "$(git ls-tree "$GIT_CASE_HEAD" -- "$sha1" 2>/dev/null)" \
|| die "holly crap, there is a collision on $sha1"
echo "$sha1"
}
function add_file_to_index() {
local FILE_NAME=$1
local CONTENTS=$2 # optional
local FILE_ID=$(
if [ "${#@}" = 2 ];
then
echo "$CONTENTS"
else
cat
fi | git hash-object -t blob -w --stdin)
git update-index --add --cacheinfo 100644 "$FILE_ID" "$FILE_NAME"
}
function commit_index_and_update_branch() {
local DESCRIPTION=$1
local COMMIT_PARENT_ARGS=$2
local TREE=$(git write-tree)
test -z "$TREE" && die "failed to write tree"
local COMMIT=$(echo "$DESCRIPTION" | git commit-tree ${TREE} ${COMMIT_PARENT_ARGS})
test -z "$COMMIT" && die "failed to create commit"
git-update-ref "refs/heads/$GIT_CASE_HEAD" "$COMMIT"
}
function gen_short_case_id() {
local long=$1
echo "${long:0:6}"
}
function print_pretty_comment() {
local file=$1
local object=$2
local short=${file##*/}
echo -e " * $bold$black""$short""$reset"
git cat-file blob $object \
| ( while read line ; do
test -z "$line" && break
name=${line%%:*}
value=${line##*: }
echo -e "$bold$black""$name: $reset""$value"
done
echo ; cat ) \
| sed 's/^/ /'
echo
}
# vim: set ft=sh ts=8 noet sw=8 tw=72