hostapd: add support for authenticating with multiple PSKs via ubus helper
[openwrt/staging/nbd.git] / scripts / kernel_bump.sh
index 5c147d78a58354417a0c71b395d27a9dea1cae2e..b1c17d6c3970e8457937fcc9e7715061c067b9fa 100755 (executable)
@@ -18,8 +18,10 @@ REQUIRED_COMMANDS='
        exit
        git
        printf
+       sed
        set
        shift
+       sort
 '
 
 _msg()
@@ -54,11 +56,14 @@ usage()
 {
        echo "Usage: ${0}"
        echo 'Helper script to bump the target kernel version, whilst keeping history.'
+       echo '    -c  Migrate config files (e.g. subtargets) only.'
        echo "    -p  Optional Platform name (e.g. 'ath79' [PLATFORM_NAME]"
+       echo "    -r  Optional comma separated list of sub-targets (e.g. 'rtl930x' [SUBTARGET_NAMES]"
        echo "    -s  Source version of kernel (e.g. 'v6.1' [SOURCE_VERSION])"
        echo "    -t  Target version of kernel (e.g. 'v6.6' [TARGET_VERSION]')"
        echo
        echo 'All options can also be passed in environment variables (listed between [BRACKETS]).'
+       echo 'Note that this script must be run from within the OpenWrt git repository.'
        echo 'Example: scripts/kernel_bump.sh -p realtek -s v6.1 -t v6.6'
 }
 
@@ -74,6 +79,8 @@ cleanup()
 
 init()
 {
+       src_file="$(readlink -f "${0}")"
+       src_dir="${src_file%%"${src_file##*'/'}"}"
        initial_branch="$(git rev-parse --abbrev-ref HEAD)"
        initial_commitish="$(git rev-parse HEAD)"
 
@@ -82,6 +89,11 @@ init()
                exit 1
        fi
 
+       if [ -n "${src_dir##*'/scripts/'}" ]; then
+               echo "This script '${src_file}' is not in the scripts subdirectory, this is unexpected, cannot continue."
+               exit 1
+       fi
+
        source_version="${source_version#v}"
        target_version="${target_version#v}"
 
@@ -90,45 +102,59 @@ init()
 
 bump_kernel()
 {
-       platform_name="${platform_name##*'/'}"
-
-       if [ -z "${platform_name:-}" ]; then
-               platform_name="${PWD##*'/'}"
+       if [ -z "${platform_name}" ] || \
+          [ -d "${PWD}/image" ]; then
+               platform_name="${PWD}"
        fi
+       platform_name="${platform_name##*'/'}"
 
-       if [ "${PWD##*'/'}" = "${platform_name}" ]; then
-               _target_dir='./'
-       else
-               _target_dir="target/linux/${platform_name}"
-       fi
+       _target_dir="${src_dir}/../target/linux/${platform_name}"
 
        if [ ! -d "${_target_dir}/image" ]; then
-               e_err 'Cannot find target linux directory.'
+               e_err "Cannot find target linux directory '${_target_dir:-not defined}'. Not in a platform directory, or -p not set."
                exit 1
        fi
 
        git switch --force-create '__openwrt_kernel_files_mover'
 
-       for _path in "${_target_dir}/"*; do
-               if [ ! -s "${_path}" ] || \
-                  [ "${_path}" = "${_path%%"-${source_version}"}" ]; then
+       if [ "${config_only:-false}" != 'true' ]; then
+               for _path in $(git ls-tree -d -r --name-only '__openwrt_kernel_files_mover' "${_target_dir}" |
+                              sed -n "s|^\(.*-${source_version}\).*|\1|p" |
+                              sort -u); do
+                       if [ ! -e "${_path}" ] || \
+                          [ "${_path}" = "${_path%%"-${source_version}"}" ]; then
+                               continue
+                       fi
+
+                       _target_path="${_path%%"-${source_version}"}-${target_version}"
+                       if [ -e "${_target_path}" ]; then
+                               e_err "Target '${_target_path}' already exists!"
+                               exit 1
+                       fi
+
+                       git mv \
+                               "${_path}" \
+                               "${_target_path}"
+               done
+       fi
+
+       for _config in $(git ls-files "${_target_dir}" |
+                        sed -n "s|^\(.*config-${source_version}\).*|\1|p" |
+                        sort -u); do
+               if [ ! -e "${_config}" ]; then
                        continue
                fi
 
-               _target_path="${_path%%"-${source_version}"}-${target_version}"
-               if [ -s "${_target_path}" ]; then
-                       e_err "Target '${_target_path}' already exists!"
-                       exit 1
+               _subtarget="${_config%%"/config-${source_version}"}"
+               if [ -n "${subtarget_names:-}" ]; then
+                       echo "${subtarget_names:-}" | while IFS=',' read -r _subtarget_name; do
+                               if [ "${_subtarget_name}" = "${_subtarget##*'/'}" ]; then
+                                       git mv "${_config}" "${_subtarget}/config-${target_version}"
+                               fi
+                       done
+               else
+                       git mv "${_config}" "${_subtarget}/config-${target_version}"
                fi
-
-               git mv \
-                       "${_path}" \
-                       "${_target_path}"
-       done
-
-       find "${_target_dir}" -iname "config-${source_version}" | while read -r _config; do
-               _path="${_config%%"/config-${source_version}"}"
-               git mv "${_config}" "${_path}/config-${target_version}"
        done
 
        git commit \
@@ -141,10 +167,12 @@ bump_kernel()
        git commit \
                --signoff \
                --message "kernel/${platform_name}: Restore kernel files for v${source_version}" \
-               --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.")"
+               --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")"
        git switch "${initial_branch:?Unable to switch back to original branch. Quitting.}"
        GIT_EDITOR=true git merge --no-ff '__openwrt_kernel_files_mover'
        git branch --delete '__openwrt_kernel_files_mover'
+       echo "Deleting merge commit ($(git rev-parse HEAD))."
+       git rebase HEAD~1
 
        echo "Original commitish was '${initial_commitish}'."
        echo 'Kernel bump complete. Remember to use `git log --follow`.'
@@ -178,8 +206,11 @@ check_requirements()
 
 main()
 {
-       while getopts 'hp:s:t:' _options; do
+       while getopts 'chp:r:s:t:' _options; do
                case "${_options}" in
+               'c')
+                       config_only='true'
+                       ;;
                'h')
                        usage
                        exit 0
@@ -187,6 +218,9 @@ main()
                'p')
                        platform_name="${OPTARG}"
                        ;;
+               'r')
+                       subtarget_names="${OPTARG}"
+                       ;;
                's')
                        source_version="${OPTARG}"
                        ;;
@@ -206,6 +240,7 @@ main()
        shift "$((OPTIND - 1))"
 
        platform_name="${platform_name:-${PLATFORM_NAME:-}}"
+       subtarget_names="${subtarget_names:-${SUBTARGET_NAMES:-}}"
        source_version="${source_version:-${SOURCE_VERSION:-}}"
        target_version="${target_version:-${TARGET_VERSION:-}}"