-
Notifications
You must be signed in to change notification settings - Fork 7
/
update-patch
executable file
·182 lines (153 loc) · 5.04 KB
/
update-patch
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
#!/usr/bin/env bash
#
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: [email protected] (Yonghyun Hwang)
#
# This script takes patch file and updates it to avoid fuzz matching for
# patching. For the update, this applies the given patch to kernel
# repository and creates a new patch. The updated patch file is required
# for kernel livepatch generation where fuzz matching for the patching is
# not allowed.
#-------------------------------------------------------------
# Shell setting
#-------------------------------------------------------------
set -E
#-------------------------------------------------------------
# Global variables
#-------------------------------------------------------------
declare -r G_KDIR="$(pwd)"
declare -r G_UPDATE_PATCH_CMD="$0"
declare -r G_UPDATE_PATCH_PATH=$(dirname $(readlink -f "${G_UPDATE_PATCH_CMD}"))
declare -r G_OUT_FILE="${2:-}"
declare -r G_TMP_FILE="$(mktemp -t update_patch_tmp.XXXXXXXXXX)"
declare -r G_TMP_MSG_FILE="$(mktemp -t commit_message.XXXXXXXXXX)"
declare G_IN_FILE="${1:-}"
declare -i G_PATCHED_DIRTY=0
declare G_COMMIT_HASH=""
#-------------------------------------------------------------
# Include library
#-------------------------------------------------------------
source "${G_UPDATE_PATCH_PATH}/libutil.bash" "update-patch"
#-------------------------------------------------------------
# Function definitions
#-------------------------------------------------------------
function cleanup()
{
local errCode="${1:-}"
local lineNum="${2:-}"
[[ ${errCode} == 0 ]] || \
util::log_error "trap at line: ${lineNum}, with error:${errCode}."
if [[ ${G_PATCHED_DIRTY} == 1 ]]; then
patch -R -p1 -s -d "${G_KDIR}" -i "${G_IN_FILE}"
G_PATCHED_DIRTY=0
util::log_ok "Patch is reverted"
fi
[[ -z "${G_COMMIT_HASH}" ]] ||
git reset -q --hard "${G_COMMIT_HASH}"
rm -f "${G_TMP_MSG_FILE}" "${G_TMP_FILE}"
exit "${errCode}"
}
# enable exit trap for debugging purpose
trap 'cleanup $? ${LINENO}' ERR INT TERM EXIT
function print_usage()
{
cat <<EOF
Usage: $(basename ${G_UPDATE_PATCH_CMD}) [IN_FILE] [OUT_FILE]
Update .patch file, [IN_FILE] to [OUT_FILE]
File: patch file
EOF
return 0
}
# validate options and prepare livepatch build
function validate_prepare()
{
[[ -f MAINTAINERS && -d init ]] || \
util::error "Should be run under root of kernel repository"
# confirm input patch file has valid format
[[ -f "${G_IN_FILE}" ]] || \
util::error "Given patch file doesn't exist."
declare -r G_IN_FILE="$(readlink -f "${G_IN_FILE}")"
[[ $(file -b --mime-type "${G_IN_FILE}") =~ text.*diff$ ]] || \
util::error "Invalid patch file format."
# dry run for patch
patch -N -p1 -d "${G_KDIR}" --dry-run -i "${G_IN_FILE}" >& /dev/null || \
util::error "Failed to apply patch"
# don't allow overwrite
[[ ! -f "${G_OUT_FILE}" ]] || \
util::error "Output file already exists."
# make sure that git repository is clean
if [[ $(git status -s | wc -l) != 0 ]]; then
git status
util::error "Please clean up git repository"
fi
G_COMMIT_HASH="$(git log -1 --format="%H")"
}
#-------------------------------------------------------------
# Main starts here
#-------------------------------------------------------------
if [[ $# < 2 ]]; then
print_usage
exit 0
fi
validate_prepare
util::log_info "Applying a patch, $(basename "${G_IN_FILE}")"
patch -N -p1 -d "${G_KDIR}" -i "${G_IN_FILE}"
G_PATCHED_DIRTY=1
util::log_ok "Patch is applied"
declare author=""
declare email=""
declare subject=""
while read line; do
case "${line}" in
"Author: "*)
author="${line#Author: }"
util::log_info "Author: ${author}"
;;
"Email: "*)
email="${line#Email: }"
util::log_info "Email: ${email}"
;;
"Subject: "*)
subject="${line#Subject: }"
util::log_info "Subject: ${subject}"
;;
*)
break;
esac
done < <(cat "${G_IN_FILE}" | git mailinfo "${G_TMP_FILE}" /dev/null)
if [[ -z "${author}" || -z "${email}" || -z "${subject}" ]]; then
util::log_info "author|email|subject is not available for git patch"
git diff >| "${G_OUT_FILE}"
util::log_ok "Generated a simple diff, ${G_OUT_FILE}"
exit 0
fi
util::log_info "Commit message"
{
echo "${subject}"
echo ""
cat "${G_TMP_FILE}"
} >| "${G_TMP_MSG_FILE}"
cat "${G_TMP_MSG_FILE}"
echo ---------------
util::log_info "Create a temporary commit"
{
git add .
git commit -F "${G_TMP_MSG_FILE}" --author="${author} <${email}>"
} >& /dev/null
G_PATCHED_DIRTY=0
util::log_info "Generate a patch and remove the temporary commit"
git format-patch -n1 --output="${G_OUT_FILE}" HEAD >& /dev/null
util::log_ok "Generated a new patch, ${G_OUT_FILE}"