kernel_bump.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. set
  20. shift
  21. '
  22. _msg()
  23. {
  24. _level="${1:?Missing argument to function}"
  25. shift
  26. if [ "${#}" -le 0 ]; then
  27. echo "${_level}: No content for this message ..."
  28. return
  29. fi
  30. echo "${_level}: ${*}"
  31. }
  32. e_err()
  33. {
  34. _msg 'err' "${*}" >&2
  35. }
  36. e_warn()
  37. {
  38. _msg 'warning' "${*}"
  39. }
  40. e_notice()
  41. {
  42. _msg 'notice' "${*}"
  43. }
  44. usage()
  45. {
  46. echo "Usage: ${0}"
  47. echo 'Helper script to bump the target kernel version, whilst keeping history.'
  48. echo " -p Optional Platform name (e.g. 'ath79' [PLATFORM_NAME]"
  49. echo " -s Source version of kernel (e.g. 'v6.1' [SOURCE_VERSION])"
  50. echo " -t Target version of kernel (e.g. 'v6.6' [TARGET_VERSION]')"
  51. echo
  52. echo 'All options can also be passed in environment variables (listed between [BRACKETS]).'
  53. echo 'Example: scripts/kernel_bump.sh -p realtek -s v6.1 -t v6.6'
  54. }
  55. cleanup()
  56. {
  57. trap - EXIT HUP INT QUIT ABRT ALRM TERM
  58. if [ -n "${initial_branch:-}" ] && \
  59. [ "$(git rev-parse --abbrev-ref HEAD)" != "${initial_branch:-}" ]; then
  60. git switch "${initial_branch}"
  61. fi
  62. }
  63. init()
  64. {
  65. initial_branch="$(git rev-parse --abbrev-ref HEAD)"
  66. initial_commitish="$(git rev-parse HEAD)"
  67. trap cleanup EXIT HUP INT QUIT ABRT ALRM TERM
  68. }
  69. bump_kernel()
  70. {
  71. platform_name="${platform_name##*'/'}"
  72. if [ -z "${platform_name:-}" ]; then
  73. platform_name="${PWD##*'/'}"
  74. fi
  75. if [ "${PWD##*'/'}" = "${platform_name}" ]; then
  76. _target_dir='./'
  77. else
  78. _target_dir="target/linux/${platform_name}"
  79. fi
  80. if [ ! -d "${_target_dir}/image" ]; then
  81. e_err 'Cannot find target linux directory.'
  82. exit 1
  83. fi
  84. git switch --force-create '__openwrt_kernel_files_mover'
  85. for _path in "${_target_dir}/"*; do
  86. if [ ! -s "${_path}" ] || \
  87. [ "${_path}" = "${_path%%"-${source_version}"}" ]; then
  88. continue
  89. fi
  90. _target_path="${_path%%"-${source_version}"}-${target_version}"
  91. if [ -s "${_target_path}" ]; then
  92. e_err "Target '${_target_path}' already exists!"
  93. exit 1
  94. fi
  95. git mv \
  96. "${_path}" \
  97. "${_target_path}"
  98. done
  99. find "${_target_dir}" -iname "config-${source_version}" | while read -r _config; do
  100. _path="${_config%%"/config-${source_version}"}"
  101. git mv "${_config}" "${_path}/config-${target_version}"
  102. done
  103. git commit \
  104. --signoff \
  105. --message "kernel/${platform_name}: Create kernel files for v${target_version} (from v${source_version})" \
  106. --message 'This is an automatically generated commit.' \
  107. --message 'When doing `git bisect`, consider `git bisect --skip`.'
  108. git checkout 'HEAD~' "${_target_dir}"
  109. git commit \
  110. --signoff \
  111. --message "kernel/${platform_name}: Restore kernel files for v${source_version}" \
  112. --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.")"
  113. git switch "${initial_branch:?Unable to switch back to original branch. Quitting.}"
  114. GIT_EDITOR=true git merge --no-ff '__openwrt_kernel_files_mover'
  115. git branch --delete '__openwrt_kernel_files_mover'
  116. echo "Original commitish was '${initial_commitish}'."
  117. echo 'Kernel bump complete. Remember to use `git log --follow`.'
  118. }
  119. check_requirements()
  120. {
  121. for _cmd in ${REQUIRED_COMMANDS}; do
  122. if ! _test_result="$(command -V "${_cmd}")"; then
  123. _test_result_fail="${_test_result_fail:-}${_test_result}\n"
  124. else
  125. _test_result_pass="${_test_result_pass:-}${_test_result}\n"
  126. fi
  127. done
  128. echo 'Available commands:'
  129. # As the results contain \n, we expect these to be interpreted.
  130. # shellcheck disable=SC2059
  131. printf "${_test_result_pass:-none\n}"
  132. echo
  133. echo 'Missing commands:'
  134. # shellcheck disable=SC2059
  135. printf "${_test_result_fail:-none\n}"
  136. echo
  137. if [ -n "${_test_result_fail:-}" ]; then
  138. echo 'Command test failed, missing programs.'
  139. test_failed=1
  140. fi
  141. }
  142. main()
  143. {
  144. while getopts 'hp:s:t:' _options; do
  145. case "${_options}" in
  146. 'h')
  147. usage
  148. exit 0
  149. ;;
  150. 'p')
  151. platform_name="${OPTARG}"
  152. ;;
  153. 's')
  154. source_version="${OPTARG#v}"
  155. ;;
  156. 't')
  157. target_version="${OPTARG#v}"
  158. ;;
  159. ':')
  160. e_err "Option -${OPTARG} requires an argument."
  161. exit 1
  162. ;;
  163. *)
  164. e_err "Invalid option: -${OPTARG}"
  165. exit 1
  166. ;;
  167. esac
  168. done
  169. shift "$((OPTIND - 1))"
  170. platform_name="${platform_name:-${PLATFORM_NAME:-}}"
  171. source_version="${source_version:-${SOURCE_VERSION:-}}"
  172. target_version="${target_version:-${TARGET_VERSION:-}}"
  173. if [ -z "${source_version:-}" ] || [ -z "${target_version:-}" ]; then
  174. e_err "Source (${source_version}) and target (${target_version}) versions need to be defined."
  175. exit 1
  176. fi
  177. check_requirements
  178. init
  179. bump_kernel
  180. cleanup
  181. }
  182. main "${@}"
  183. exit 0