scripts/kernel_bump: Allow for migrating only configuration files
[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 ' -c Migrate config files (e.g. subtargets) only.'
58 echo " -p Optional Platform name (e.g. 'ath79' [PLATFORM_NAME]"
59 echo " -s Source version of kernel (e.g. 'v6.1' [SOURCE_VERSION])"
60 echo " -t Target version of kernel (e.g. 'v6.6' [TARGET_VERSION]')"
61 echo
62 echo 'All options can also be passed in environment variables (listed between [BRACKETS]).'
63 echo 'Note that this script must be run from within the OpenWrt git repository.'
64 echo 'Example: scripts/kernel_bump.sh -p realtek -s v6.1 -t v6.6'
65 }
66
67 cleanup()
68 {
69 trap - EXIT HUP INT QUIT ABRT ALRM TERM
70
71 if [ -n "${initial_branch:-}" ] && \
72 [ "$(git rev-parse --abbrev-ref HEAD)" != "${initial_branch:-}" ]; then
73 git switch "${initial_branch}"
74 fi
75 }
76
77 init()
78 {
79 src_file="$(readlink -f "${0}")"
80 src_dir="${src_file%%"${src_file##*'/'}"}"
81 initial_branch="$(git rev-parse --abbrev-ref HEAD)"
82 initial_commitish="$(git rev-parse HEAD)"
83
84 if [ -n "$(git status --porcelain | grep -v '^?? .*')" ]; then
85 echo 'Git respository not in a clean state, will not continue.'
86 exit 1
87 fi
88
89 if [ -n "${src_dir##*'/scripts/'}" ]; then
90 echo "This script '${src_file}' is not in the scripts subdirectory, this is unexpected, cannot continue."
91 exit 1
92 fi
93
94 source_version="${source_version#v}"
95 target_version="${target_version#v}"
96
97 trap cleanup EXIT HUP INT QUIT ABRT ALRM TERM
98 }
99
100 bump_kernel()
101 {
102 if [ -z "${platform_name}" ] || \
103 [ -d "${PWD}/image" ]; then
104 platform_name="${PWD}"
105 fi
106 platform_name="${platform_name##*'/'}"
107
108 _target_dir="${src_dir}/../target/linux/${platform_name}"
109
110 if [ ! -d "${_target_dir}/image" ]; then
111 e_err "Cannot find target linux directory '${_target_dir:-not defined}'. Not in a platform directory, or -p not set."
112 exit 1
113 fi
114
115 git switch --force-create '__openwrt_kernel_files_mover'
116
117 if [ "${config_only:-false}" != 'true' ]; then
118 for _path in "${_target_dir}/"*; do
119 if [ ! -e "${_path}" ] || \
120 [ "${_path}" = "${_path%%"-${source_version}"}" ]; then
121 continue
122 fi
123
124 _target_path="${_path%%"-${source_version}"}-${target_version}"
125 if [ -e "${_target_path}" ]; then
126 e_err "Target '${_target_path}' already exists!"
127 exit 1
128 fi
129
130 git mv \
131 "${_path}" \
132 "${_target_path}"
133 done
134 fi
135
136 find "${_target_dir}" -iname "config-${source_version}" | while read -r _config; do
137 _path="${_config%%"/config-${source_version}"}"
138 git mv "${_config}" "${_path}/config-${target_version}"
139 done
140
141 git commit \
142 --signoff \
143 --message "kernel/${platform_name}: Create kernel files for v${target_version} (from v${source_version})" \
144 --message 'This is an automatically generated commit.' \
145 --message 'When doing `git bisect`, consider `git bisect --skip`.'
146
147 git checkout 'HEAD~' "${_target_dir}"
148 git commit \
149 --signoff \
150 --message "kernel/${platform_name}: Restore kernel files for v${source_version}" \
151 --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.")"
152 git switch "${initial_branch:?Unable to switch back to original branch. Quitting.}"
153 GIT_EDITOR=true git merge --no-ff '__openwrt_kernel_files_mover'
154 git branch --delete '__openwrt_kernel_files_mover'
155
156 echo "Original commitish was '${initial_commitish}'."
157 echo 'Kernel bump complete. Remember to use `git log --follow`.'
158 }
159
160 check_requirements()
161 {
162 for _cmd in ${REQUIRED_COMMANDS}; do
163 if ! _test_result="$(command -V "${_cmd}")"; then
164 _test_result_fail="${_test_result_fail:-}${_test_result}\n"
165 else
166 _test_result_pass="${_test_result_pass:-}${_test_result}\n"
167 fi
168 done
169
170 echo 'Available commands:'
171 # As the results contain \n, we expect these to be interpreted.
172 # shellcheck disable=SC2059
173 printf "${_test_result_pass:-none\n}"
174 echo
175 echo 'Missing commands:'
176 # shellcheck disable=SC2059
177 printf "${_test_result_fail:-none\n}"
178 echo
179
180 if [ -n "${_test_result_fail:-}" ]; then
181 echo 'Command test failed, missing programs.'
182 test_failed=1
183 fi
184 }
185
186 main()
187 {
188 while getopts 'chp:s:t:' _options; do
189 case "${_options}" in
190 'c')
191 config_only='true'
192 ;;
193 'h')
194 usage
195 exit 0
196 ;;
197 'p')
198 platform_name="${OPTARG}"
199 ;;
200 's')
201 source_version="${OPTARG}"
202 ;;
203 't')
204 target_version="${OPTARG}"
205 ;;
206 ':')
207 e_err "Option -${OPTARG} requires an argument."
208 exit 1
209 ;;
210 *)
211 e_err "Invalid option: -${OPTARG}"
212 exit 1
213 ;;
214 esac
215 done
216 shift "$((OPTIND - 1))"
217
218 platform_name="${platform_name:-${PLATFORM_NAME:-}}"
219 source_version="${source_version:-${SOURCE_VERSION:-}}"
220 target_version="${target_version:-${TARGET_VERSION:-}}"
221
222 if [ -z "${source_version:-}" ] || [ -z "${target_version:-}" ]; then
223 e_err "Source (${source_version:-missing source version}) and target (${target_version:-missing target version}) versions need to be defined."
224 echo
225 usage
226 exit 1
227 fi
228
229 check_requirements
230
231 init
232 bump_kernel
233 cleanup
234 }
235
236 main "${@}"
237
238 exit 0