armsr: armv8: enable serial console for Renesas platforms
[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 'Example: scripts/kernel_bump.sh -p realtek -s v6.1 -t v6.6'
63 }
64
65 cleanup()
66 {
67 trap - EXIT HUP INT QUIT ABRT ALRM TERM
68
69 if [ -n "${initial_branch:-}" ] && \
70 [ "$(git rev-parse --abbrev-ref HEAD)" != "${initial_branch:-}" ]; then
71 git switch "${initial_branch}"
72 fi
73 }
74
75 init()
76 {
77 initial_branch="$(git rev-parse --abbrev-ref HEAD)"
78 initial_commitish="$(git rev-parse HEAD)"
79
80 trap cleanup EXIT HUP INT QUIT ABRT ALRM TERM
81 }
82
83 do_source_target()
84 {
85 _target_dir="${1:?Missing argument to function}"
86 _op="${2:?Missing argument to function}"
87
88 }
89
90 bump_kernel()
91 {
92 platform_name="${platform_name##*'/'}"
93
94 if [ -z "${platform_name:-}" ]; then
95 platform_name="${PWD##*'/'}"
96 fi
97
98 if [ "${PWD##*'/'}" = "${platform_name}" ]; then
99 _target_dir='./'
100 else
101 _target_dir="target/linux/${platform_name}"
102 fi
103
104 if [ ! -d "${_target_dir}/image" ]; then
105 e_err 'Cannot find target linux directory.'
106 exit 1
107 fi
108
109 git switch --force-create '__openwrt_kernel_files_mover'
110
111 for _path in "${_target_dir}/"*; do
112 if [ ! -s "${_path}" ] || \
113 [ "${_path}" = "${_path%%"-${source_version}"}" ]; then
114 continue
115 fi
116
117 _target_path="${_path%%"-${source_version}"}-${target_version}"
118 if [ -s "${_target_path}" ]; then
119 e_err "Target '${_target_path}' already exists!"
120 exit 1
121 fi
122
123 git mv \
124 "${_path}" \
125 "${_target_path}"
126 done
127
128 find "${_target_dir}" -iname "config-${source_version}" | while read -r _config; do
129 _path="${_config%%"/config-${source_version}"}"
130 git mv "${_config}" "${_path}/config-${target_version}"
131 done
132
133 git commit \
134 --signoff \
135 --message "kernel/${platform_name}: Create kernel files for v${target_version} (from v${source_version})" \
136 --message 'This is an automatically generated commit.' \
137 --message 'During a `git bisect` session, `git bisect --skip` is recommended.'
138
139 git checkout 'HEAD~' "${_target_dir}"
140 git commit \
141 --signoff \
142 --message "kernel/${platform_name}: Restore kernel files for v${source_version}" \
143 --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.")"
144 git switch "${initial_branch:?Unable to switch back to original branch. Quitting.}"
145 GIT_EDITOR=true git merge --no-ff '__openwrt_kernel_files_mover'
146 git branch --delete '__openwrt_kernel_files_mover'
147
148 echo "Original commitish was '${initial_commitish}'."
149 echo 'Kernel bump complete. Remember to use `git log --follow`.'
150 }
151
152 check_requirements()
153 {
154 for _cmd in ${REQUIRED_COMMANDS}; do
155 if ! _test_result="$(command -V "${_cmd}")"; then
156 _test_result_fail="${_test_result_fail:-}${_test_result}\n"
157 else
158 _test_result_pass="${_test_result_pass:-}${_test_result}\n"
159 fi
160 done
161
162 echo 'Available commands:'
163 # As the results contain \n, we expect these to be interpreted.
164 # shellcheck disable=SC2059
165 printf "${_test_result_pass:-none\n}"
166 echo
167 echo 'Missing commands:'
168 # shellcheck disable=SC2059
169 printf "${_test_result_fail:-none\n}"
170 echo
171
172 if [ -n "${_test_result_fail:-}" ]; then
173 echo 'Command test failed, missing programs.'
174 test_failed=1
175 fi
176 }
177
178 main()
179 {
180 while getopts 'hp:s:t:' _options; do
181 case "${_options}" in
182 'h')
183 usage
184 exit 0
185 ;;
186 'p')
187 platform_name="${OPTARG}"
188 ;;
189 's')
190 source_version="${OPTARG#v}"
191 ;;
192 't')
193 target_version="${OPTARG#v}"
194 ;;
195 ':')
196 e_err "Option -${OPTARG} requires an argument."
197 exit 1
198 ;;
199 *)
200 e_err "Invalid option: -${OPTARG}"
201 exit 1
202 ;;
203 esac
204 done
205 shift "$((OPTIND - 1))"
206
207 platform_name="${platform_name:-${PLATFORM_NAME:-}}"
208 source_version="${source_version:-${SOURCE_VERSION:-}}"
209 target_version="${target_version:-${TARGET_VERSION:-}}"
210
211 if [ -z "${source_version:-}" ] || [ -z "${target_version:-}" ]; then
212 e_err "Source (${source_version}) and target (${target_version}) versions need to be defined."
213 exit 1
214 fi
215
216 check_requirements
217
218 init
219 bump_kernel
220 cleanup
221 }
222
223 main "${@}"
224
225 exit 0