-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathentrypoint.sh
executable file
·438 lines (330 loc) · 10.9 KB
/
entrypoint.sh
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#! /bin/bash -x
#
# The goal is to regularly sync 'net-next' branch on this repo with netdev's one.
# Then our topgit tree can be updated and the modifications can be pushed only
# if there were no merge conflicts.
#
# In case of questions about this script, please notify Matthieu Baerts.
# We should manage all errors in this script
set -e
# Env vars that can be set to change the behaviour
UPD_TG_FORCE_SYNC="${INPUT_FORCE_SYNC:-0}"
UPD_TG_NOT_BASE="${INPUT_NOT_BASE:-0}"
UPD_TG_FORCE_UPD_NET="${INPUT_FORCE_UPD_NET:-0}"
# Github remote
GIT_REMOTE_GITHUB_NAME="origin"
# Netdev remote
GIT_REMOTE_URL_NET="git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git"
GIT_REMOTE_BRANCH_NET="main"
GIT_REMOTE_URL_NET_NEXT="git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git"
GIT_REMOTE_BRANCH_NET_NEXT="main"
# Local repo
TG_TOPIC_BASE_NET_NEXT="net-next"
TG_TOPIC_BASE_NET="net"
TG_TOPIC_BASE_SHA_ORIG_NET_NEXT="${TG_TOPIC_BASE_NET_NEXT}" # will become a sha later
TG_TOPIC_BASE_SHA_ORIG_NET="${TG_TOPIC_BASE_NET}" # will become a sha later
TG_TOPIC_TOP_NET_NEXT="t/upstream"
TG_TOPIC_TOP_NET="${TG_TOPIC_TOP_NET_NEXT}-net"
TG_EXPORT_BRANCH_NET_NEXT="export"
TG_EXPORT_BRANCH_NET="${TG_EXPORT_BRANCH_NET_NEXT}-net"
TG_FOR_REVIEW_BRANCH_NET_NEXT="for-review"
TG_FOR_REVIEW_BRANCH_NET="${TG_FOR_REVIEW_BRANCH_NET_NEXT}-net"
ERR_MSG=""
TG_NEW_BASE_NET=0
###########
## Utils ##
###########
# $@: message to display before quiting
err() {
echo "ERROR: ${*}" >&2
}
# $1: last return code
print_err() { local rc
rc="${1}"
# check return code: if different than 0, we exit with an error: reset
if [ "${rc}" -eq 0 ]; then
return 0
fi
# in the notif, only the end is displayed
set +x
err "${ERR_MSG}"
return "${rc}"
}
git_init() {
git config --global user.name "Matthieu Baerts (NGI0)"
git config --global user.email "[email protected]"
# To avoid this error:
# unsafe repository ('/github/workspace' is owned by someone else)
git config --global --add safe.directory "${PWD}" || true
}
# $1: branch ; [ $2: remote, default: origin ]
git_checkout() { local branch remote
branch="${1}"
remote="${2:-${GIT_REMOTE_GITHUB_NAME}}"
git checkout -f "${branch}" || git checkout -b "${branch}" "${remote}/${branch}"
}
# [ $1: ref, default: HEAD ]
git_get_sha() {
git rev-parse "${1:-HEAD}"
}
git_get_current_branch() {
git rev-parse --abbrev-ref HEAD
}
topic_has_been_upstreamed() { local subject range
subject="${1}"
range="${2}"
git log \
--fixed-strings \
-i --grep "${subject}" \
--format="format:==%s==" \
"${range}" | \
grep -q --fixed-strings -i "==${subject}=="
}
###############
## TG Update ##
###############
# $1: branch
tg_update_base_github() { local branch
branch="${1}"
git_checkout "${branch}"
git pull --no-stat --ff-only \
"${GIT_REMOTE_GITHUB_NAME}" \
"${branch}"
}
tg_update_base_local() {
tg_update_base_github "${TG_TOPIC_BASE_NET}"
TG_TOPIC_BASE_SHA_ORIG_NET=$(git_get_sha HEAD)
tg_update_base_github "${TG_TOPIC_BASE_NET_NEXT}"
TG_TOPIC_BASE_SHA_ORIG_NET_NEXT=$(git_get_sha HEAD)
}
tg_update_base_net_is_up_to_date() {
# The -net base is updated after net-next.
# If net-next is up to date, -net is as well if we take a common base
# except if -net is forced to be updated to the latest commit.
if [ "${UPD_TG_FORCE_UPD_NET}" != 1 ]; then
return 0
fi
git fetch "${GIT_REMOTE_URL_NET}" "${GIT_REMOTE_BRANCH_NET}" || exit 1
[ "${TG_TOPIC_BASE_SHA_ORIG_NET}" = "$(git_get_sha FETCH_HEAD)" ]
}
tg_update_base_net_next() {
if [ "${UPD_TG_NOT_BASE}" = 1 ]; then
return 0
fi
git_checkout "${TG_TOPIC_BASE_NET_NEXT}"
# this branch has to be in sync with upstream, no merge
git pull --no-stat --ff-only \
"${GIT_REMOTE_URL_NET_NEXT}" \
"${GIT_REMOTE_BRANCH_NET_NEXT}"
# check both -net (updated after) and net-next before exiting
if [ "${UPD_TG_FORCE_SYNC}" != 1 ] && tg_update_base_net_is_up_to_date && \
[ "${TG_TOPIC_BASE_SHA_ORIG_NET_NEXT}" = "$(git_get_sha HEAD)" ]; then
echo "Already sync with ${GIT_REMOTE_URL_NET_NEXT}" \
"(${TG_TOPIC_BASE_SHA_ORIG_NET_NEXT}) and" \
"${GIT_REMOTE_URL_NET} (${TG_TOPIC_BASE_SHA_ORIG_NET})"
exit 0
fi
}
tg_update_base_net() { local new_base
if [ "${UPD_TG_NOT_BASE}" = 1 ]; then
return 0
fi
git_checkout "${TG_TOPIC_BASE_NET}"
# FETCH_HEAD == net/main
git fetch "${GIT_REMOTE_URL_NET}" "${GIT_REMOTE_BRANCH_NET}"
if [ "${UPD_TG_FORCE_UPD_NET}" = 1 ]; then
new_base="FETCH_HEAD"
else
# to avoid having to resolve conflicts when merging -net and
# net-next, we take the last common commit between the two
new_base=$(git merge-base FETCH_HEAD "${TG_TOPIC_BASE_NET_NEXT}")
if git merge-base --is-ancestor "${TG_TOPIC_BASE_NET}" "${new_base}"; then
echo "Going to update the -net base (if new_base is different)"
else
echo "The -net base is newer than the common commit, no modif"
new_base="${TG_TOPIC_BASE_NET}"
fi
fi
# this branch has to be in sync with upstream, no merge
git merge --no-stat --ff-only "${new_base}"
if [ "${TG_TOPIC_BASE_SHA_ORIG_NET}" != "$(git_get_sha HEAD)" ]; then
TG_NEW_BASE_NET=1
fi
}
tg_update_abort_exit() {
ERR_MSG+=": $(git_get_current_branch)"
tg update --abort
exit 1
}
# $1: git range of new commits
tg_update_resolve_or_exit() { local range subject
range="${1}"
subject=$(grep "^Subject: " .topmsg 2>/dev/null | cut -d\] -f2- | sed "s/^ //")
if [ -z "${subject}" ] ||
! topic_has_been_upstreamed "${subject}" "${range}"; then
# display useful info in the log for the notifications
git --no-pager diff || true
tg_update_abort_exit
fi
echo "The commit '${subject}' has been upstreamed, trying auto-fix:"
git checkout --theirs .
git add -u
git commit -s --no-edit
if [ -n "$(tg files)" ]; then
echo "This topic was supposed to be empty because the commit " \
"seems to have been sent upstream: abording."
# display useful info in the log for the notifications
tg patch || true
tg_update_abort_exit
fi
}
# $@: arg for tg_update_resolve_or_exit
tg_update() {
if ! tg update; then
tg_update_resolve_or_exit "${@}"
while ! tg update --continue; do
tg_update_resolve_or_exit "${@}"
done
fi
}
# $1: top branch, $2+: arg for tg_update_resolve_or_exit
tg_update_tree_common() { local branch
branch="${1}"
shift
git_checkout "${branch}"
# fetch and update-ref will be done
tg remote "${GIT_REMOTE_GITHUB_NAME}" --populate
# do that twice (if there is no error) just in case the base and the
# rest of the tree were not sync. It can happen if the tree has been
# updated by someone else and after, the base (only) has been updated.
# At the beginning of this script, we force an update of the base.
tg_update "${@}"
tg_update "${@}"
}
tg_update_tree_net_next() { local range
range="${TG_TOPIC_BASE_SHA_ORIG_NET_NEXT}..${TG_TOPIC_BASE_NET_NEXT}"
tg_update_base_net_next
tg_update_tree_common "${TG_TOPIC_TOP_NET_NEXT}" "${range}"
}
tg_update_tree_net() { local range
range="${TG_TOPIC_BASE_SHA_ORIG_NET}..${TG_TOPIC_BASE_NET}"
tg_update_base_net
# first the tree for -net
tg_update_tree_common "${TG_TOPIC_TOP_NET}" "${range}"
# then the tree for net-next
tg_update_tree_common "${TG_TOPIC_TOP_NET_NEXT}" "${range}"
}
tg_update_tree() {
git fetch "${GIT_REMOTE_GITHUB_NAME}"
# force to add TG refs in refs/top-bases/: needed for restrictions/clean-up
git config --local topgit.top-bases refs
tg_update_tree_net_next
tg_update_tree_net
}
tg_get_all_topics() {
git for-each-ref --format="%(refname)" "refs/remotes/${GIT_REMOTE_GITHUB_NAME}/top-bases/" | \
sed -e "s#refs/remotes/${GIT_REMOTE_GITHUB_NAME}/top-bases/\\(.*\\)#\\1#g"
}
tg_reset() { local topic
for topic in $(tg_get_all_topics); do
git update-ref "refs/top-bases/${topic}" \
"refs/remotes/${GIT_REMOTE_GITHUB_NAME}/top-bases/${topic}"
git update-ref "refs/heads/${topic}" "refs/remotes/${GIT_REMOTE_GITHUB_NAME}/${topic}"
done
# the bases should be already up to date anyway.
git update-ref "refs/heads/${TG_TOPIC_BASE_NET}" \
"refs/remotes/${GIT_REMOTE_GITHUB_NAME}/${TG_TOPIC_BASE_NET}"
git update-ref "refs/heads/${TG_TOPIC_BASE_NET_NEXT}" \
"refs/remotes/${GIT_REMOTE_GITHUB_NAME}/${TG_TOPIC_BASE_NET_NEXT}"
}
# $1: last return code
tg_trap_reset() { local rc
rc="${1}"
# print the error message is any.
if print_err "${rc}"; then
return 0
fi
tg_reset
return "${rc}"
}
############
## TG End ##
############
# $1: branch
tg_push() { local branch
branch="${1}"
git_checkout "${branch}"
tg push -r "${GIT_REMOTE_GITHUB_NAME}"
}
tg_push_tree() {
tg_push "${TG_TOPIC_TOP_NET_NEXT}"
tg_push "${TG_TOPIC_TOP_NET}"
}
tg_export_common() { local branch_top branch_export current_date tag
branch_top="${1}"
branch_export="${2}"
current_date="${3}"
git_checkout "${branch_top}"
tag="${branch_export}/${current_date}"
tg export --force --notes "${branch_export}"
# change the committer for the last commit to let Intel's kbuild starting tests
GIT_COMMITTER_NAME="Matthieu Baerts" \
GIT_COMMITTER_EMAIL="[email protected]" \
git commit --amend --no-edit
git push --force "${GIT_REMOTE_GITHUB_NAME}" "${branch_export}"
# send a tag to Github to keep previous commits: we might have refs to them
git tag "${tag}" "${branch_export}"
git push "${GIT_REMOTE_GITHUB_NAME}" "${tag}"
}
tg_export() { local current_date
current_date=$(date --utc +%Y%m%dT%H%M%S)
tg_export_common "${TG_TOPIC_TOP_NET_NEXT}" "${TG_EXPORT_BRANCH_NET_NEXT}" "${current_date}"
if [ "${TG_NEW_BASE_NET}" = "1" ]; then
tg_export_common "${TG_TOPIC_TOP_NET}" "${TG_EXPORT_BRANCH_NET}" "${current_date}"
fi
}
tg_for_review_common() { local branch_top branch_review tg_conflict_files
branch_top="${1}"
branch_review="${2}"
git_checkout "${branch_review}"
git pull --no-stat --ff-only \
"${GIT_REMOTE_GITHUB_NAME}" "${branch_review}"
if ! git merge --no-edit --signoff "${branch_top}"; then
# the only possible conflict would be with the topgit files, manage this
tg_conflict_files=$(git status --porcelain | grep -E "^DU\\s.top(deps|msg)$")
if [ -n "${tg_conflict_files}" ]; then
echo "${tg_conflict_files}" | awk '{ print $2 }' | xargs git rm
if ! git commit -s --no-edit; then
err "Unexpected other conflicts: ${tg_conflict_files}"
return 1
fi
else
err "Unexpected conflicts when updating ${branch_review}"
return 1
fi
fi
git push "${GIT_REMOTE_GITHUB_NAME}" "${branch_review}"
}
tg_for_review() {
tg_for_review_common "${TG_TOPIC_TOP_NET_NEXT}" "${TG_FOR_REVIEW_BRANCH_NET_NEXT}"
if [ "${TG_NEW_BASE_NET}" = "1" ]; then
tg_for_review_common "${TG_TOPIC_TOP_NET}" "${TG_FOR_REVIEW_BRANCH_NET}"
fi
}
##########
## Main ##
##########
trap 'print_err "${?}"' EXIT
ERR_MSG="Unable to init git"
git_init
ERR_MSG="Unable to update the local topgit base"
tg_update_base_local
trap 'tg_trap_reset "${?}"' EXIT
ERR_MSG="Unable to update the topgit tree"
tg_update_tree
ERR_MSG="Unable to push the update of the Topgit tree"
tg_push_tree
ERR_MSG="Unable to export the TopGit tree"
tg_export
ERR_MSG="Unable to update the ${TG_FOR_REVIEW_BRANCH} branch"
tg_for_review