69045946e96746c4c2e285315ac2ede38975f66a
[openwrt/staging/stintel.git] / scripts / kernel_bump.sh
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-2.0-or-later
3 #
4 # Copyright (C) 2024 Olliver Schinagl <oliver@schinagl.nl>
5
6 set -eu
7 if [ -n "${DEBUG_TRACE_SH:-}" ] && \
8 [ "${DEBUG_TRACE_SH:-}" != "${DEBUG_TRACE_SH#*"$(basename "${0}")"*}" ] || \
9 [ "${DEBUG_TRACE_SH:-}" = 'all' ]; then
10 set -x
11 fi
12
13 REQUIRED_COMMANDS='
14 [
15 basename
16 command
17 echo
18 exit
19 git
20 printf
21 set
22 shift
23 '
24
25 _msg()
26 {
27 _level="${1:?Missing argument to function}"
28 shift
29
30 if [ "${#}" -le 0 ]; then
31 echo "${_level}: No content for this message ..."
32 return
33 fi
34
35 echo "${_level}: ${*}"
36 }
37
38 e_err()
39 {
40 _msg 'err' "${*}" >&2
41 }
42
43 e_warn()
44 {
45 _msg 'warning' "${*}"
46 }
47
48 e_notice()
49 {
50 _msg 'notice' "${*}"
51 }
52
53 usage()
54 {
55 echo "Usage: ${0}"
56 echo 'Helper script to bump the target kernel version, whilst keeping history.'
57 echo " -p Optional Platform name (e.g. 'ath79' [PLATFORM_NAME]"
58 echo " -s Source version of kernel (e.g. 'v6.1' [SOURCE_VERSION])"
59 echo " -t Target version of kernel (e.g. 'v6.6' [TARGET_VERSION]')"
60 echo
61 echo 'All options can also be passed in environment variables (listed between [BRACKETS]).'
62 echo 'Note that this script must be run from within the OpenWrt git repository.'
63 echo 'Example: scripts/kernel_bump.sh -p realtek -s v6.1 -t v6.6'
64 }
65
66 cleanup()
67 {
68 trap - EXIT HUP INT QUIT ABRT ALRM TERM
69
70 if [ -n "${initial_branch:-}" ] && \
71 [ "$(git rev-parse --abbrev-ref HEAD)" != "${initial_branch:-}" ]; then
72 git switch "${initial_branch}"
73 fi
74 }
75
76 init()
77 {
78 src_file="$(readlink -f "${0}")"
79 src_dir="${src_file%%"${src_file##*'/'}"}"
80 initial_branch="$(git rev-parse --abbrev-ref HEAD)"
81 initial_commitish="$(git rev-parse HEAD)"
82
83 if [ -n "$(git status --porcelain | grep -v '^?? .*')" ]; then
84 echo 'Git respository not in a clean state, will not continue.'
85 exit 1
86 fi
87
88 if [ -n "${src_dir##*'/scripts/'}" ]; then
89 echo "This script '${src_file}' is not in the scripts subdirectory, this is unexpected, cannot continue."
90 exit 1
91 fi
92
93 source_version="${source_version#v}"
94 target_version="${target_version#v}"
95
96 trap cleanup EXIT HUP INT QUIT ABRT ALRM TERM
97 }
98
99 bump_kernel()
100 {
101 if [ -z "${platform_name}" ] || \
102 [ -d "${PWD}/image" ]; then
103 platform_name="${PWD}"
104 fi
105 platform_name="${platform_name##*'/'}"
106
107 _target_dir="${src_dir}/../target/linux/${platform_name}"
108
109 if [ ! -d "${_target_dir}/image" ]; then
110 e_err "Cannot find target linux directory '${_target_dir:-not defined}'. Not in a platform directory, or -p not set."
111 exit 1
112 fi
113
114 git switch --force-create '__openwrt_kernel_files_mover'
115
116 for _path in "${_target_dir}/"*; do
117 if [ ! -s "${_path}" ] || \
118 [ "${_path}" = "${_path%%"-${source_version}"}" ]; then
119 continue
120 fi
121
122 _target_path="${_path%%"-${source_version}"}-${target_version}"
123 if [ -s "${_target_path}" ]; then
124 e_err "Target '${_target_path}' already exists!"
125 exit 1
126 fi
127
128 git mv \
129 "${_path}" \
130 "${_target_path}"
131 done
132
133 find "${_target_dir}" -iname "config-${source_version}" | while read -r _config; do
134 _path="${_config%%"/config-${source_version}"}"
135 git mv "${_config}" "${_path}/config-${target_version}"
136 done
137
138 git commit \
139 --signoff \
140 --message "kernel/${platform_name}: Create kernel files for v${target_version} (from v${source_version})" \
141 --message 'This is an automatically generated commit.' \
142 --message 'When doing `git bisect`, consider `git bisect --skip`.'
143
144 git checkout 'HEAD~' "${_target_dir}"
145 git commit \
146 --signoff \
147 --message "kernel/${platform_name}: Restore kernel files for v${source_version}" \
148 --message "$(printf "This is an automatically generated commit which aids following Kernel patch history,\nas git will see the move and copy as a rename thus defeating the purpose.\n\nSee: https://lists.openwrt.org/pipermail/openwrt-devel/2023-October/041673.html\nfor the original discussion.")"
149 git switch "${initial_branch:?Unable to switch back to original branch. Quitting.}"
150 GIT_EDITOR=true git merge --no-ff '__openwrt_kernel_files_mover'
151 git branch --delete '__openwrt_kernel_files_mover'
152
153 echo "Original commitish was '${initial_commitish}'."
154 echo 'Kernel bump complete. Remember to use `git log --follow`.'
155 }
156
157 check_requirements()
158 {
159 for _cmd in ${REQUIRED_COMMANDS}; do
160 if ! _test_result="$(command -V "${_cmd}")"; then
161 _test_result_fail="${_test_result_fail:-}${_test_result}\n"
162 else
163 _test_result_pass="${_test_result_pass:-}${_test_result}\n"
164 fi
165 done
166
167 echo 'Available commands:'
168 # As the results contain \n, we expect these to be interpreted.
169 # shellcheck disable=SC2059
170 printf "${_test_result_pass:-none\n}"
171 echo
172 echo 'Missing commands:'
173 # shellcheck disable=SC2059
174 printf "${_test_result_fail:-none\n}"
175 echo
176
177 if [ -n "${_test_result_fail:-}" ]; then
178 echo 'Command test failed, missing programs.'
179 test_failed=1
180 fi
181 }
182
183 main()
184 {
185 while getopts 'hp:s:t:' _options; do
186 case "${_options}" in
187 'h')
188 usage
189 exit 0
190 ;;
191 'p')
192 platform_name="${OPTARG}"
193 ;;
194 's')
195 source_version="${OPTARG}"
196 ;;
197 't')
198 target_version="${OPTARG}"
199 ;;
200 ':')
201 e_err "Option -${OPTARG} requires an argument."
202 exit 1
203 ;;
204 *)
205 e_err "Invalid option: -${OPTARG}"
206 exit 1
207 ;;
208 esac
209 done
210 shift "$((OPTIND - 1))"
211
212 platform_name="${platform_name:-${PLATFORM_NAME:-}}"
213 source_version="${source_version:-${SOURCE_VERSION:-}}"
214 target_version="${target_version:-${TARGET_VERSION:-}}"
215
216 if [ -z "${source_version:-}" ] || [ -z "${target_version:-}" ]; then
217 e_err "Source (${source_version:-missing source version}) and target (${target_version:-missing target version}) versions need to be defined."
218 echo
219 usage
220 exit 1
221 fi
222
223 check_requirements
224
225 init
226 bump_kernel
227 cleanup
228 }
229
230 main "${@}"
231
232 exit 0