2 # SPDX-License-Identifier: GPL-2.0-or-later
4 # Copyright (C) 2024 Olliver Schinagl <oliver@schinagl.nl>
7 if [ -n "${DEBUG_TRACE_SH:-}" ] && \
8 [ "${DEBUG_TRACE_SH:-}" != "${DEBUG_TRACE_SH#*"$(basename "${0}")"*}" ] || \
9 [ "${DEBUG_TRACE_SH:-}" = 'all' ]; then
29 _level
="${1:?Missing argument to function}"
32 if [ "${#}" -le 0 ]; then
33 echo "${_level}: No content for this message ..."
37 echo "${_level}: ${*}"
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 " -r Optional comma separated list of sub-targets (e.g. 'rtl930x' [SUBTARGET_NAMES]"
62 echo " -s Source version of kernel (e.g. 'v6.1' [SOURCE_VERSION])"
63 echo " -t Target version of kernel (e.g. 'v6.6' [TARGET_VERSION]')"
65 echo 'All options can also be passed in environment variables (listed between [BRACKETS]).'
66 echo 'Note that this script must be run from within the OpenWrt git repository.'
67 echo 'Example: scripts/kernel_bump.sh -p realtek -s v6.1 -t v6.6'
72 trap - EXIT HUP INT QUIT ABRT ALRM TERM
74 if [ -n "${initial_branch:-}" ] && \
75 [ "$(git rev-parse --abbrev-ref HEAD)" != "${initial_branch:-}" ]; then
76 git switch
"${initial_branch}"
82 src_file
="$(readlink -f "${0}")"
83 src_dir
="${src_file%%"${src_file##*'/'}"}"
84 initial_branch
="$(git rev-parse --abbrev-ref HEAD)"
85 initial_commitish
="$(git rev-parse HEAD)"
87 if [ -n "$(git status --porcelain | grep -v '^?? .*')" ]; then
88 echo 'Git respository not in a clean state, will not continue.'
92 if [ -n "${src_dir##*'/scripts/'}" ]; then
93 echo "This script '${src_file}' is not in the scripts subdirectory, this is unexpected, cannot continue."
97 source_version
="${source_version#v}"
98 target_version
="${target_version#v}"
100 trap cleanup EXIT HUP INT QUIT ABRT ALRM TERM
105 if [ -z "${platform_name}" ] || \
106 [ -d "${PWD}/image" ]; then
107 platform_name
="${PWD}"
109 platform_name
="${platform_name##*'/'}"
111 _target_dir
="${src_dir}/../target/linux/${platform_name}"
113 if [ ! -d "${_target_dir}/image" ]; then
114 e_err
"Cannot find target linux directory '${_target_dir:-not defined}'. Not in a platform directory, or -p not set."
118 git switch
--force-create '__openwrt_kernel_files_mover'
120 if [ "${config_only:-false}" != 'true' ]; then
121 for _path
in $
(git ls-tree
-d -r --name-only '__openwrt_kernel_files_mover' "${_target_dir}" |
122 sed -n "s|^\(.*-${source_version}\).*|\1|p" |
124 if [ ! -e "${_path}" ] || \
125 [ "${_path}" = "${_path%%"-${source_version}"}" ]; then
129 _target_path
="${_path%%"-${source_version}"}-${target_version}"
130 if [ -e "${_target_path}" ]; then
131 e_err
"Target '${_target_path}' already exists!"
141 for _config
in $
(git ls-files
"${_target_dir}" |
142 sed -n "s|^\(.*config-${source_version}\).*|\1|p" |
144 if [ ! -e "${_config}" ]; then
148 _subtarget
="${_config%%"/config-${source_version}"}"
149 if [ -n "${subtarget_names:-}" ]; then
150 echo "${subtarget_names:-}" |
while IFS
=',' read -r _subtarget_name
; do
151 if [ "${_subtarget_name}" = "${_subtarget##*'/'}" ]; then
152 git
mv "${_config}" "${_subtarget}/config-${target_version}"
156 git
mv "${_config}" "${_subtarget}/config-${target_version}"
162 --message "kernel/${platform_name}: Create kernel files for v${target_version} (from v${source_version})" \
163 --message 'This is an automatically generated commit.' \
164 --message 'When doing `git bisect`, consider `git bisect --skip`.'
166 git checkout
'HEAD~' "${_target_dir}"
169 --message "kernel/${platform_name}: Restore kernel files for v${source_version}" \
170 --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.
")"
171 git switch
"${initial_branch:?Unable to switch back to original branch. Quitting.}"
172 GIT_EDITOR
=true git merge
--no-ff '__openwrt_kernel_files_mover'
173 git branch
--delete '__openwrt_kernel_files_mover'
174 echo "Deleting merge commit ($(git rev-parse HEAD))."
177 echo "Original commitish was '${initial_commitish}'."
178 echo 'Kernel bump complete. Remember to use `git log --follow`.'
183 for _cmd
in ${REQUIRED_COMMANDS}; do
184 if ! _test_result
="$(command -V "${_cmd}")"; then
185 _test_result_fail
="${_test_result_fail:-}${_test_result}\n"
187 _test_result_pass
="${_test_result_pass:-}${_test_result}\n"
191 echo 'Available commands:'
192 # As the results contain \n, we expect these to be interpreted.
193 # shellcheck disable=SC2059
194 printf "${_test_result_pass:-none\n}"
196 echo 'Missing commands:'
197 # shellcheck disable=SC2059
198 printf "${_test_result_fail:-none\n}"
201 if [ -n "${_test_result_fail:-}" ]; then
202 echo 'Command test failed, missing programs.'
209 while getopts 'chp:r:s:t:' _options
; do
210 case "${_options}" in
219 platform_name
="${OPTARG}"
222 subtarget_names
="${OPTARG}"
225 source_version
="${OPTARG}"
228 target_version
="${OPTARG}"
231 e_err
"Option -${OPTARG} requires an argument."
235 e_err
"Invalid option: -${OPTARG}"
240 shift "$((OPTIND - 1))"
242 platform_name
="${platform_name:-${PLATFORM_NAME:-}}"
243 subtarget_names
="${subtarget_names:-${SUBTARGET_NAMES:-}}"
244 source_version
="${source_version:-${SOURCE_VERSION:-}}"
245 target_version
="${target_version:-${TARGET_VERSION:-}}"
247 if [ -z "${source_version:-}" ] ||
[ -z "${target_version:-}" ]; then
248 e_err
"Source (${source_version:-missing source version}) and target (${target_version:-missing target version}) versions need to be defined."