-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit-import-srpm
More file actions
executable file
·476 lines (395 loc) · 15 KB
/
git-import-srpm
File metadata and controls
executable file
·476 lines (395 loc) · 15 KB
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/bin/bash -eu
#
# tl;dr:
# - To import a specific revision, provided you're in the srpm repo:
# git-import-srpm HEAD
#
# - To import all revisions (the script will dedupe the branches):
# git-import-srpm --all
#
# This is a poor man's script importing a SRPM into a git repository. It
# has a few limitations in its current quick&dirty implementation:
#
# - All changes made during the %prep step are squashed into one commit at
# the end of the import.
#
# - Loses any comments appended to patches in the spec file, these could be
# added to the commit description.
#
# On Debian derivatives, you'll need:
# sudo apt-get install rpm
#
# Simply run it inside a SRPM git repository and give it a revision (can be
# tag, branch, sha1, ...), e.g.:
#
# CODE_REPO_PATH=/path/to/linux/repo SRPM_REPO_PATH=/path/kernel/srpm/repo git-import-srpm XS-8.2.1
#
# By default, the SRPM_REPO_PATH is the current working directory. If no
# CODE_REPO_PATH is given, a default path is inferred that is
# SRPM_REPO_PATH/../(linux|xen|qemu) (depending on the product).
set -o pipefail
THIS_SCRIPT="$(readlink -f "$0")"
SRPM_REPO_PATH=${SRPM_REPO_PATH:-${PWD}}
PRODUCT="$(basename "${SRPM_REPO_PATH}/SPECS/"*.spec .spec)"
OOT_DRIVER_IMPORT=${OOT_DRIVER_IMPORT:-}
CODE_REPO_PATH="${CODE_REPO_PATH:-${SRPM_REPO_PATH}/../${PRODUCT/kernel/linux}/}"
SPEC_FILE="SPECS/${PRODUCT}.spec"
if [[ ${PRODUCT} = "kernel" || ${PRODUCT} = "qemu" ]]; then
RELEASE_PREFIX="v"
elif [[ ${PRODUCT} = "xen" ]]; then
RELEASE_PREFIX="RELEASE-"
elif [[ ${PRODUCT} = "rpm" ]]; then
RELEASE_PREFIX="rpm-"
RELEASE_SUFFIX="-release"
else
if [[ -z "${OOT_DRIVER_IMPORT}" ]]; then
echo "Unknown package: '${PRODUCT}'"
exit 1
fi
RELEASE_PREFIX=""
RELEASE_SUFFIX=""
fi
GIT_COMMIT_OPTIONS="--no-gpg-sign"
function check_spec_file() {
local worktree="$1"
local revision="$2"
local spec_file
spec_file="$(git -C "${worktree}" ls-tree --name-only "${revision}" SPECS/)"
if [[ ${spec_file} != "${SPEC_FILE}" ]]; then
echo "${SPEC_FILE} does not exist in ${worktree}:${revision}" 1>&2
exit 1
fi
}
function get_build_dir() {
local worktree="$1"
local revision="$2"
check_spec_file "${worktree}" "${revision}"
local build_dirname_query_filter
build_dirname_query_filter='%{name}-%{version}\n'
if [[ $(git -C "${worktree}" show "${revision}:${SPEC_FILE}" | grep '^%autosetup' ) =~ [[:space:]]-n[[:space:]]+([^[:space:]]+) ]]; then
build_dirname_query_filter="${BASH_REMATCH[1]}"
fi
local build_dirname
build_dirname=$(git -C "${worktree}" show "${revision}:${SPEC_FILE}" | \
rpmspec --query --define "_topdir ${worktree}" --qf "${build_dirname_query_filter}" /dev/stdin | \
awk 'NR == 1'
)
local build_dir
build_dir="${worktree}/BUILD/${build_dirname}"
if [[ ! -d "${build_dir}" ]] ; then
# rpmbuild version 6.x are building in a sub-directory
build_dir="${worktree}/BUILD/${build_dirname}-build/${build_dirname}"
fi
if [[ ! -d "${build_dir}" ]]; then
echo "Could not infer an existing build directory from ${build_dirname}" 1>&2
exit 1
fi
echo "${build_dir}"
}
function get_base_version() {
local worktree="$1"
local revision="$2"
check_spec_file "${worktree}" "${revision}"
git -C "${worktree}" show "${revision}:${SPEC_FILE}" | rpmspec -P /dev/stdin | awk "/^Version:/{print \"${RELEASE_PREFIX}\" \$NF \"${RELEASE_SUFFIX:-}\"}"
}
function get_release() {
local worktree="$1"
local revision="$2"
check_spec_file "${worktree}" "${revision}"
git -C "${worktree}" show "${revision}:${SPEC_FILE}" | rpmspec -P /dev/stdin | awk '/^Release:/{print $NF}'
}
function get_full_version() {
local worktree="$1"
local revision="$2"
local base_version
base_version=$(get_base_version "${worktree}" "${revision}")
local release
release="$(get_release "${worktree}" "${revision}")"
local raw_version="${base_version#"${RELEASE_PREFIX}"}"
raw_version="${raw_version%"${RELEASE_SUFFIX:-}"}"
echo "${raw_version}-${release}"
}
function get_vendor() {
local worktree="$1"
local revision="$2"
check_spec_file "${worktree}" "${revision}"
git -C "${worktree}" show "${revision}:${SPEC_FILE}" | grep -c -e XCP-ng -e xcp > /dev/null \
&& echo "xcpng" \
|| echo "xenserver"
}
function get_branch_name() {
local worktree="$1"
local revision="$2"
local vendor
vendor=$(get_vendor "${worktree}" "${revision}")
full_version="$(get_full_version "${worktree}" "${revision}")"
if [[ -z ${vendor} || -z ${full_version} ]]; then
echo "unknown_branch_name_for_rev_${revision} vendor=${vendor:-} full_version=${full_version:-}"
else
echo "${PRODUCT}/${vendor}-${full_version}/patched"
fi
}
function init_worktrees() {
local srpm_repo_worktree="$1"
local code_repo_worktree="$2"
local srpm_repo_revision="$3"
git -C "${SRPM_REPO_PATH}" worktree add --detach "${srpm_repo_worktree}" "${srpm_repo_revision}^0" >> "${COMMAND_LOGS}"
# Upstream stable base
local base_version
if [[ -z "${OOT_DRIVER_IMPORT}" ]]; then
base_version=$(get_base_version "${srpm_repo_worktree}" HEAD)
if [[ ${PRODUCT} = "xen" ]]; then
if [[ ${base_version} = "RELEASE-4.13.0" ]]; then
base_version=85e1424de2dda
fi
fi
else
base_version=$(git -C "${CODE_REPO_PATH}" rev-parse "origin/source/8.3^{/Initial commit.}")
fi
BRANCH_NAME="$(get_branch_name "${srpm_repo_worktree}" HEAD)"
echo "[+] Constructing ${BRANCH_NAME} from rev ${srpm_repo_revision}"
if git -C "${CODE_REPO_PATH}" show-ref --verify --quiet "refs/heads/${BRANCH_NAME}"; then
echo "[-] branch ${BRANCH_NAME} already exists"
if [[ "${FORCE_IMPORT:-}" ]]; then
git branch -D "${BRANCH_NAME}"
else
exit 1
fi
fi
git -C "${CODE_REPO_PATH}" rev-parse "${base_version}" 2> /dev/null > /dev/null || {
echo "- ${BRANCH_NAME}: ${base_version} cannot be found, have you fetched"
exit 1
}
git -C "${CODE_REPO_PATH}" worktree add -B "${BRANCH_NAME}-partial" "${code_repo_worktree}" "${base_version}" >> "${COMMAND_LOGS}"
git -C "${CODE_REPO_PATH}" branch -f "${BRANCH_NAME%patched}source" "${base_version}"
}
# Fix the date in case it is not present in the patch file to get
# idempotent runs.
function fixup_date() {
local p="${1}"
author_date="Sat Sep 19 2015 06:09:42 GMT+0000"
awk '
BEGIN { has_date=0 }
/^Date:/ { has_date=1 }
{ lines[NR]=$0 }
END {
if (!has_date) {
for (i=1; i<=NR; i++) {
print lines[i]
if (lines[i] ~ /^From:/) {
print "Date: '"${author_date}"'"
}
}
} else {
for (i=1; i<=NR; i++) print lines[i]
}
}
' "${p}" > "${p}.fixed"
mv "${p}.fixed" "${p}"
}
function load_author_and_title() {
local p="${1}"
local git_dir="${2}"
mailinfo="$(git mailinfo /dev/null /dev/null < "${p}" | \
sed -n -e 's/^Author: \(.*\)$/export GIT_AUTHOR_NAME=\"\1\"/p' \
-e 's/^Email: \(.*\)$/export GIT_AUTHOR_EMAIL=\"\1\"/p' \
-e 's/^Subject: \(.*\)$/local subject=\"\1\"/p')"
eval "${mailinfo}"
if [[ -n ${subject:-} ]]; then
sed -i "1i\${subject}\n\n" "${git_dir}"/rebase-apply/msg
fi
}
function export_author() {
export GIT_COMMITTER_NAME="Vates Git Importer"
export GIT_COMMITTER_EMAIL="gitimporter@vates.tech"
export GIT_COMMITTER_DATE="1519512702"
# These are simply default values in case the information is not
# present inside the patch file - it allows idempotent runs as well.
export GIT_AUTHOR_NAME="${GIT_COMMITTER_NAME}"
export GIT_AUTHOR_EMAIL="${GIT_COMMITTER_EMAIL}"
}
function apply_patch() {
local worktree="$1"
local patch="$2"
cd "${worktree}"
# Quirk to get idempotent runs and properly stitched history in Xen
# sources. This patch creates a .gitarchive-info-pq file with a sha1
# to the head of the patch-qeueu, as such it is different every time
# and causes history to diverge early in similar branches. Simply do
# not apply it.
if [[ $(basename "${p}") = "changeset-info.patch" ]] ; then
return 0
fi
export_author
fixup_date "${patch}"
local git_dir
git_dir=$(git rev-parse --git-dir)
if ! git am ${GIT_COMMIT_OPTIONS} -3 < "${patch}"; then
local unmerged_files
unmerged_files="$(git status -s | grep '^UU')"
local changes
changes="$(git status -s)"
if [[ -n ${unmerged_files} || -z ${changes} ]]; then
# The patch didn't apply. Sometimes git am is a bit more
# peculiar than a patch command, especially if there are index
# information it doesn't have in its tree. In that case
# fallback to patch.
git reset --hard
patch -p1 --fuzz=2 < "${patch}" || return 1
git add -A
# An error for this git am falls through, likely caused by
# missing author information, so we'll use git commit directly
# below.
if git am ${GIT_COMMIT_OPTIONS} --continue; then
return 0
fi
fi
# This one is used when we fallback to plain git commit
export GIT_AUTHOR_DATE="1442642982"
if [[ -s ${git_dir}/rebase-apply/msg ]]; then
load_author_and_title "${patch}" "${git_dir}"
if [[ $(basename "${p}") = "qemu-4.2.1-CVE-2023-3354.backport.patch" ]]; then
# Patch is malformed
sed -i '1i\io: remove io watch if TLS channel is closed during handshake\n' "${git_dir}"/rebase-apply/msg
export GIT_AUTHOR_NAME="Daniel P. Berrangé"
export GIT_AUTHOR_EMAIL="<berrange@redhat.com>"
fi
# There was a commit description, so try and use it
git commit ${GIT_COMMIT_OPTIONS} -F "${git_dir}"/rebase-apply/msg && git am --skip
else
# No commit description, use the patch path
git commit ${GIT_COMMIT_OPTIONS} -m "$(basename "${p}")" && git am --skip
fi
return $?
fi
return 0
}
function apply_spec_quirks() {
# This one is no-where to be found, and it doesn't really change any
# source code, simply stuff a binary tarball in the sources
sed -i "s@cp /usr/src/ipxe-source.tar.gz.*@@" "${SRPM_REPO_WORKTREE}/${SPEC_FILE}"
# This is a remote source which is not available anymore, so rpmbuild
# would fail
awk '
/^Source[0-9]:/{
if ($2 == "https://repo.citrite.net/list/ctx-local-contrib/citrix/branding/Citrix_Logo_Black.png") {
sub(/:/, "", $1)
source_logo=toupper($1)
}
}
source_logo && $0 ~ source_logo {
next
}
{print}
' "${SRPM_REPO_WORKTREE}/${SPEC_FILE}" > "${SRPM_REPO_WORKTREE}/${SPEC_FILE}.fixed"
mv "${SRPM_REPO_WORKTREE}/${SPEC_FILE}.fixed" "${SRPM_REPO_WORKTREE}/${SPEC_FILE}"
}
function import_all_branches() {
local -A revs_for_version
local nproc
nproc=$(nproc)
echo "[+] Gathering unique branches"
cd "${SRPM_REPO_PATH}"
for revision in $(git rev-list --min-parents=1 --topo-order HEAD); do
local branch_name
branch_name="$(get_branch_name "${PWD}" "${revision}")"
if [[ -z ${revs_for_version[${branch_name}]:-} ]]; then
revs_for_version[${branch_name}]="${revision}"
fi
done
for b in "${!revs_for_version[@]}"; do
local revision="${revs_for_version[${b}]}"
case ${b} in
unknown_branch_name*)
echo "Could not infer a branch name for revision: ${revision}" 1>&2
;;
*)
echo "${revs_for_version[${b}]}"
;;
esac
done | (SRPM_REPO_PATH="${SRPM_REPO_PATH}" CODE_REPO_PATH="${CODE_REPO_PATH}" xargs -n 1 -P "${nproc}" "${THIS_SCRIPT}")
}
COMMAND_LOGS=/tmp/git-import-srpm.${PRODUCT}.${1}.debug.log
if [[ -f ${COMMAND_LOGS} ]]; then
rm "${COMMAND_LOGS}"
fi
exec 2> "${COMMAND_LOGS}"
if [[ "$1" = "--all" ]]; then
import_all_branches
exit 0
fi
SRPM_REPO_REVISION=$1
TMPDIR=$(mktemp -d -t src-import-xen-XXX)
trap 'LAST_LINE=$LINENO' DEBUG
# shellcheck disable=SC2154 # shellcheck is confused and think exit_code is used without being assigned
trap '
exit_code=$?
if [[ ${exit_code} -ne 0 ]]; then
echo "Error at line:${LAST_LINE} '\''${BASH_COMMAND}'\'' exited with code ${exit_code}"
echo " Log file: ${COMMAND_LOGS}"
if [[ -z "${DONT_DELETE_TMP_DIR:-}" ]]; then
echo " You can re-run the script with DONT_DELETE_TMP_DIR=y to look around in the temporary worktree."
else
echo " Temporary worktrees at: ${TMPDIR}"
fi
fi
if [[ -z "${DONT_DELETE_TMP_DIR:-}" ]]; then
test "${RPMBUILD_PID:-}" && kill ${RPMBUILD_PID} || :
rm -Rf ${TMPDIR:-/a/path/that/does/not/exist}
cd ${CODE_REPO_PATH}
git worktree prune
cd ${SRPM_REPO_PATH}
git worktree prune
fi
' EXIT
SRPM_REPO_WORKTREE="${TMPDIR}/srpm"
CODE_REPO_WORKTREE="${TMPDIR}/code"
init_worktrees "${SRPM_REPO_WORKTREE}" "${CODE_REPO_WORKTREE}" "${SRPM_REPO_REVISION}"
apply_spec_quirks
# Newer rpmbuild use a default --fuzz=0 to the patch command, when packages
# built on older version of rpmbuild would apply it with --fuzz=2, so make
# sure to use --fuzz=2 here.
rpmbuild --nodeps \
--define "_buildshell /bin/bash" \
--define "_topdir ${SRPM_REPO_WORKTREE}" \
--define "_default_patch_fuzz 2" \
--eval "%{_default_patch_fuzz}" \
-bp "${SRPM_REPO_WORKTREE}/${SPEC_FILE}" 2>> "${COMMAND_LOGS}" >> "${COMMAND_LOGS}" &
RPMBUILD_PID=$!
if [[ -z "${OOT_DRIVER_IMPORT}" ]]; then
cd "${SRPM_REPO_WORKTREE}"
git show "HEAD:${SPEC_FILE}" | while IFS= read -r l; do
case "${l}" in
Patch*)
p="${l#* }"
apply_patch "${CODE_REPO_WORKTREE}" "${SRPM_REPO_WORKTREE}/SOURCES/${p}" >> "${COMMAND_LOGS}" 2>> "${COMMAND_LOGS}" || {
echo "[-] [apply_patch] Failed reconstructing ${BRANCH_NAME} for ${SRPM_REPO_REVISION}, check ${COMMAND_LOGS}"
if [[ -z "${CARRY_ON:-}" ]]; then
exit 1
else
git am --abort || :
git reset --hard || :
fi
}
;;
*)
;;
esac
done
fi
wait "${RPMBUILD_PID}" || {
echo "[-] [rpmbuild] Failed reconstructing ${BRANCH_NAME} for ${SRPM_REPO_REVISION}, check ${COMMAND_LOGS}"
exit 1
} && {
build_dir="$(get_build_dir "${SRPM_REPO_WORKTREE}" "${SRPM_REPO_REVISION}")"
git -C "${CODE_REPO_WORKTREE}" --work-tree "${build_dir}" add -A
export_author
if [[ -z "${OOT_DRIVER_IMPORT}" ]]; then
commit_msg="rpmbuild: remaining diff."
else
commit_msg="git-import-srpm: import of $(get_full_version "${SRPM_REPO_WORKTREE}" "${SRPM_REPO_REVISION}")"
fi
git -C "${CODE_REPO_WORKTREE}" commit ${GIT_COMMIT_OPTIONS} -s --date="${GIT_COMMITTER_DATE}" -m "${commit_msg}" 2>> "${COMMAND_LOGS}" >> "${COMMAND_LOGS}" || :
}
# Workaround `git branch` race condition when it moves the branch reflog.
git -C "${CODE_REPO_PATH}" branch -m "${BRANCH_NAME}-partial" "${BRANCH_NAME}"