kernel_bump.sh 5.0 KB

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