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