kernel_bump.sh 6.5 KB

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