-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegit-setup
executable file
·340 lines (281 loc) · 7.77 KB
/
legit-setup
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/bin/sh
return_to_orig_head()
{
# If this is a new repository, it's possible that the branch we were
# just in is actually empty (and therefore doesn't exist). If that's the
# case - make one
if ! git show-ref --quiet refs/heads/$orig_head; then
git checkout --orphan $orig_head > /dev/null
git rm --force --quiet -r . > /dev/null
else
git checkout $orig_head > /dev/null
#if [ $stashed -eq 1 ]; then
# git stash pop > /dev/null
#fi
fi
}
require_clean_work_tree()
{
require_work_tree
if ! git diff-index --quiet HEAD --
then
exit "fatal: Cannot $1 in dirty working tree"
fi
}
require_work_tree()
{
test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true ||
die "fatal: $0 cannot be used without a working tree."
}
cd_to_toplevel()
{
cdup=$(git rev-parse --show-toplevel) &&
cd "$cdup" || {
echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
exit 1
}
}
usage()
{
echo $USAGE
die
}
die()
{
die_with_status 1 "$@"
}
die_with_status ()
{
status=$1
shift
printf >&2 '%s\n' "$*"
exit "$status"
}
die_neatly()
{
return_to_orig_head
die $1
}
git_editor()
{
if test -z "${GIT_EDITOR:+set}"
then
GIT_EDITOR="$(git var GIT_EDITOR)" || return $?
fi
eval "$GIT_EDITOR" '"$@"'
}
do_merge()
{
local name=$1
local branch=$2
local keep=$3
git checkout $branch --quiet
if git merge $name --quiet --no-ff --no-commit > /dev/null 2>&1
then
git commit --quiet -m "Merged: $name"
git checkout tracking --quiet
sed "/$name/d" .tracking/proposals/pending | cat > .tracking/proposals/pending
git add .tracking/proposals/pending >> /dev/null 2>&1
replace_header Status Merged .tracking/proposals/$name/proposal
git add .tracking/proposals/$name/proposal >> /dev/null 2>&1
git commit --quiet -m "Merged: $name"
else
if test ! -n "$keep"
then
git merge --abort
fi
return 1
fi
return 0
}
merge()
{
local name=$1
local keep=$2
local check=$name
while true
do
local start=$(read_header start .tracking/proposals/$check/proposal)
for branch in $(git branch --contains $start | sed 's/\*//;s/ *//')
do
# Check if this commit is in a locked branch
local _locked=$(git config --file .tracking/config branch.$branch.locked)
if test -n "$_locked" && [ "true" = "$_locked" ]
then
echo "Attempting to automatically merge..."
if do_merge $name $branch $keep
then
for ext in $(read_header extended-by .tracking/proposals/$name/proposal)
do
if [ $(read_header status .tracking/proposals/$ext/proposal) = "Accepted" ]
then
do_merge $ext $branch
fi
done
return 0
else
echo "Automatic merged failed you can perform a merge proposal if you like"
return 1
fi
fi
done
local _fix=$(read_header fix-of .tracking/proposals/$check/proposal)
if test -n "$_fix" && [ "$_fix" = "$start" ]
then
check=$start
else
return 1
fi
done
}
# Tests to see if the given array contains the given value
contains() {
search=$1
shift
array=$*
if [[ ${array[@]} == *$search* ]]
then
for element in "${array[@]}"
do
if [[ $element == $search ]]
then
return 0
fi
done
fi
return 1
}
# Reads a header from a file
read_header()
{
look_for=$1
file=$2
return_val=1
while IFS=: read key value
do
value=$(echo $value | sed 's/^\s*//;s/\s*$//')
key=$(echo $key | tr '[:upper:]' '[:lower:]')
if [ ! -n "$key" ]
then
break
fi
if [ "$key" = "$look_for" ]
then
echo $value
return_val=0
fi
done < $file
return $return_val
}
replace_header()
{
local _header=$1
local _value=$2
local _file=$3
cat $_file | sed -r "s/^$_header:.+\$/$_header: $_value/I" | cat > $_file
}
append_header()
{
header=$1
value=$2
file=$3
cat $file | sed -r "0,/^$/s//$header: $value\n/" | cat > $file
}
# Finds the branch point of the given commit
find_branch_point()
{
name=$1
first=true
explored=()
# Find what this is based on
for commit in $(git rev-list $name)
do
for branch in $(git branch --contains $commit | sed 's/\*//;s/ *//')
do
if test true = $first
then
explored+=("$branch")
continue
fi
# Check if we've already inspected this branch. If we have it
# obviously didn't yield anything, so we can skip it here
if contains $branch $explored; then
continue
fi
# Mark the branch as explored so we don't have to mess about
# with it again
explored+=("$branch")
# Check if this commit is in a locked branch
# If it is, we must be working of this
locked=`git config --file .tracking/config branch.$branch.locked`
if [ "$locked" = "true" ]; then
echo $commit
return 0
fi
# Check if this commit is in a proposal
branch_head=`git rev-parse --verify $branch`
if [ -d .tracking/proposals/$branch_head ]; then
start=$(read_header start .tracking/proposals/$branch_head/proposal)
if [ $? != 0 ]
then
die "fatal: malformed proposal ($branch_head) is missing start header"
fi
if [ "$start" = "$commit" ] || ! git merge-base --is-ancestor $start $commit
then
continue
else
echo $commit
echo $branch_head
return 0
fi
fi
done
if test true = $first; then
first=false
fi
done
return 1
}
# Parses config files
read_required_values()
{
# Overall Amounts
req_total_proposals=0 # Total amount of proposals
req_total_reviews=0 # Total amount of reviews
req_proposals=0 # Accepted - Rejected Proposals
req_reviews=0 # Good - Bad Reviews
# Specific Amounts
req_accepted=0 # Number of accepted proposals
req_good_reviews=0 # Number of reviews with the correct answer
req_good_accepts=0
req_good_rejects=0
local items=("$1:1")
local i=0
while test $i != ${#items[@]}
do
local item=(${items[$i]//:/ })
local multiplier=${item[1]}
item=${item[0]}
i=$(expr $i + 1)
for part in $(git config --file .tracking/config score.$item)
do
part=(${part//:/ })
local rule=${part[0]}
local num=${part[1]}
if [ -z "$num" ] || [ "$num" -le 0 ]
then
num=1
fi
local value=$(expr $num \* $multiplier)
case $rule in
proposals|reviews|total-proposals|total-reviews|accepted|good-reviews|good-accepts|good-rejects)
rule="req_${rule//-/_}"
eval "$rule=\$(expr \${!rule} + $value)"
;;
*)
items+=("$rule:$value")
;;
esac
done
done
}