kernel_bump.sh 5.0 KB

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