From 1c799c7480f838e08fa3d09eea26eeb7c837813c Mon Sep 17 00:00:00 2001
From: Andreas Gnau <andreas.gnau@iopsys.eu>
Date: Fri, 8 Jan 2021 17:36:13 +0100
Subject: [PATCH] bcmkernel: bcm_git: Factor out importing and merging

Factor out importing and merging to separate functions. This will make
it easier to run the steps just for one tarball or tarballs with
differing naming conventions such as wifi-tarballs.
---
 bcmkernel/scripts/bcm_git.sh | 319 +++++++++++++++++++----------------
 1 file changed, 174 insertions(+), 145 deletions(-)

diff --git a/bcmkernel/scripts/bcm_git.sh b/bcmkernel/scripts/bcm_git.sh
index ee3da0b..d764c72 100755
--- a/bcmkernel/scripts/bcm_git.sh
+++ b/bcmkernel/scripts/bcm_git.sh
@@ -104,6 +104,165 @@ is_module_already_added_to_repo()
     esac
 }
 
+import_module()
+{
+    local repo="$1"
+    local module="$2"
+    local version="$3"
+    local tar_path="$4"
+
+    local tar="$(basename "$tar_path")"
+
+    if is_module_already_added_to_repo "$repo" "$module" "$version"
+    then
+        echo "Already added ${version} "$module" (corresponding branch/tag exists already). Skipping!"
+        return 0
+    fi
+    set -u
+
+    # Set up git branch
+    case ${module} in
+    "data_full_release")
+        # for each data_full_release we create a new commit on the broadcom branch
+        echo -e "\t${tar}  this is the base module"
+
+        # prep repo. clean out all old files
+        git -C ${repo} checkout broadcom
+        git -C ${repo} rm -rq .
+        ;;
+    *)
+        # for all addon-modules, branch off from data_full_release tag
+        # to apply addon in the next step
+        git_new_branch "$repo" "$version" "$module"
+    esac
+
+    # Unpack outer tarball to temporary directory inside repo
+    mkdir "${repo}/git_tmp"
+    echo "Unpacking outer tarball $tar"
+    pv ${tar_path}  | tar -xz -C ${repo}/git_tmp
+    local latest_file="$(get_latest_file_in_path "${repo}/git_tmp")"
+    local git_commit_date="$(get_file_modification_date_as_git_date "$latest_file")"
+
+    # XXX: These special cases can be generalized and the cd-ing can be avoided
+    # XXX: maybe check that README in outer tar matches what we expect, so we get notified
+    #      when the script needs updating for new instructions. Overkill or useful sanity check?
+    case ${module} in
+    "data_full_release")
+        tar_src=$( basename $tar | sed -e "s/${module}/data_src/")
+        echo "unpack $tar_src"
+        mkdir ${repo}/bcm963xx
+        # exclude any included git repo
+        pv ${repo}/git_tmp/${tar_src} | tar -xz --exclude=\.git -C ${repo}/bcm963xx
+        do_merge=1
+        ;;
+    "voice_addon"|"voice_eptapp_addon")
+        ( cd_repo $repo
+        cd git_tmp
+        # if you think that you can do "yes | ./bcmvoiceinstall ../" you are wrong. does not work.
+        echo "yy" | ./bcmvoiceinstall ../bcm963xx
+        )
+        ;;
+    "data_gfast_src")
+        ( cd_repo $repo
+        tar -xf git_tmp/*tar.gz -C bcm963xx
+        )
+        ;;
+    "xpon_src")
+        ( cd_repo $repo
+
+        rm -rf bcm963xx/userspace/private/apps/omcid/*
+        rm -rf bcm963xx/bcmdrivers/broadcom/char/gpon/*
+        rm -rf bcm963xx/userspace/private/libs/eponsdk
+
+        tar -xf git_tmp/*tar.gz -C bcm963xx
+        )
+        ;;
+    "bdkuserspace_src"|"openwrtsupport_src")
+        ( cd_repo $repo
+        for tar in git_tmp/*tar.gz
+        do
+            tar --overwrite -xf $tar -C bcm963xx
+        done
+        )
+        ;;
+
+    *)
+        echo -e "\t ${module} from ${tar} not supported"
+        return 1
+        ;;
+    esac
+
+    rm -rf ${repo}/git_tmp
+    # add files to git
+    echo "Now adding files to git. this takes some time"
+    git -C "$repo" add -A -f
+    local tar_md5="$(md5sum "${tar_path}" | cut -f1 -d ' ')"
+    local tar_sha256="$(sha256sum "${tar_path}" | cut -f1 -d ' ')"
+
+    case ${module} in
+    "data_full_release")
+        ( echo "Add Broadcom release $version"
+            echo
+            echo "Filename: ${tar}"
+            echo "SHA256:   ${tar_sha256}"
+            echo "MD5:      ${tar_md5}" ) | \
+            git -C "$repo" commit \
+                --author="broadcom release <broadcom_release@broadcom.com>" \
+                --date="$git_commit_date" \
+                -F - \
+                -q
+        git -C ${repo} tag bcm_${version}_t
+        ;;
+    *)
+        ( echo "Add ${module} to release ${version}"
+            echo
+            echo "Filename: ${tar}"
+            echo "SHA256:   ${tar_sha256}"
+            echo "MD5:      ${tar_md5}" ) | \
+            git -C "$repo" commit \
+                --author="broadcom release <broadcom_release@broadcom.com>" \
+                --date="$git_commit_date" \
+                -F - \
+                -q
+        ;;
+    esac
+}
+
+merge_module_into_bcm_branch()
+{
+    # requires to be on the right branch bcm_$version
+    local repo="$1"
+    local module="$2"
+    local version="$3"
+
+    case ${module} in
+    "data_full_release")
+        true
+        ;;
+    *)
+        echo "trying to merge ${module} from branch ${version}_${module}"
+        if git -C ${repo} branch | grep "bcm_${version}_${module}$"
+        then
+            if ! git -C ${repo} merge --no-commit --no-ff -Xtheirs bcm_${version}_${module}
+            then
+                # so this merge did not work. clean up and skip it.
+                git -C ${repo} merge --abort
+                git -C ${repo} reset --hard
+                git -C ${repo} clean -ffxd
+                return
+            fi
+            local status="$(git -C "$repo" status --porcelain)"
+            if [ -n "$status" ]; then
+                git -C ${repo} commit --author="broadcom release <broadcom_release@broadcom.com>" \
+                    -m "Merge in bcm_${version}_${module}"
+            else
+                echo "No changes => not committing anything. ${module} ${version} has probably already been merged"
+            fi
+        fi
+        ;;
+    esac
+}
+
 add_release ()
 {
     local version=$1
@@ -120,161 +279,31 @@ add_release ()
     for module in $bcm_modules
     do
         tar="bcm963xx_${version}_${module}.tar.gz"
-        if is_module_already_added_to_repo "$repo" "$module" "$version"
-        then
-            echo "Already added ${version} "$module" (corresponding branch/tag exists already). Skipping!"
-            continue
-        elif ! [ -f ${tar_dir}/${tar} ]
+        if ! [ -f ${tar_dir}/${tar} ]
         then
             echo -e "\ttar not found. skipping $tar"
             continue
         fi
-
-        # Set up git branch
-        case ${module} in
-        "data_full_release")
-            # for each data_full_release we create a new commit on the broadcom branch
-            echo -e "\t${tar}  this is the base module"
-
-            # prep repo. clean out all old files
-            git -C ${repo} checkout broadcom
-            git -C ${repo} rm -rq .
-            ;;
-        *)
-            # for all addon-modules, branch off from data_full_release tag
-            # to apply addon in the next step
-            git_new_branch "$repo" "$version" "$module"
-        esac
-
-        # Unpack outer tarball to temporary directory inside repo
-        mkdir "${repo}/git_tmp"
-        echo "Unpacking outer tarball $tar"
-        local tar_name="$(basename $tar)"
-        pv ${tar_dir}/${tar}  | tar -xz -C ${repo}/git_tmp
-        local latest_file="$(get_latest_file_in_path "${repo}/git_tmp")"
-        local git_commit_date="$(get_file_modification_date_as_git_date "$latest_file")"
-
-        case ${module} in
-        "data_full_release")
-            tar_src=$( basename $tar | sed -e "s/${module}/data_src/")
-            echo "unpack $tar_src"
-            mkdir ${repo}/bcm963xx
-            # exclude any included git repo
-            pv ${repo}/git_tmp/${tar_src} | tar -xz --exclude=\.git -C ${repo}/bcm963xx
-            do_merge=1
-            ;;
-        "voice_addon"|"voice_eptapp_addon")
-            ( cd_repo $repo
-            cd git_tmp
-            # if you think that you can do "yes | ./bcmvoiceinstall ../" you are wrong. does not work.
-            echo "yy" | ./bcmvoiceinstall ../bcm963xx
-            )
-            ;;
-        "data_gfast_src")
-            ( cd_repo $repo
-            tar -xf git_tmp/*tar.gz -C bcm963xx
-            )
-            ;;
-        "xpon_src")
-            ( cd_repo $repo
-
-            rm -rf bcm963xx/userspace/private/apps/omcid/*
-            rm -rf bcm963xx/bcmdrivers/broadcom/char/gpon/*
-            rm -rf bcm963xx/userspace/private/libs/eponsdk
-
-            tar -xf git_tmp/*tar.gz -C bcm963xx
-            )
-            ;;
-        "bdkuserspace_src"|"openwrtsupport_src")
-            ( cd_repo $repo
-            for tar in git_tmp/*tar.gz
-            do
-                tar --overwrite -xf $tar -C bcm963xx
-            done
-            )
-            ;;
-
-        *)
-            echo -e "\t${tar}"
-            ;;
-        esac
-
-        rm -rf ${repo}/git_tmp
-        # add files to git
-        echo "Now adding files to git. this takes some time"
-        git -C "$repo" add -A -f
-        local tar_md5="$(md5sum "${tar_dir}/${tar}" | cut -f1 -d ' ')"
-        local tar_sha256="$(sha256sum "${tar_dir}/${tar}" | cut -f1 -d ' ')"
-
-        case ${module} in
-        "data_full_release")
-            ( echo "Add Broadcom release $version"
-                echo
-                echo "Filename: ${tar_name}"
-                echo "SHA256:   ${tar_sha256}"
-                echo "MD5:      ${tar_md5}" ) | \
-                git -C "$repo" commit \
-                    --author="broadcom release <broadcom_release@broadcom.com>" \
-                    --date="$git_commit_date" \
-                    -F - \
-                    -q
-            git -C ${repo} tag bcm_${version}_t
-            ;;
-        *)
-            ( echo "Add ${module} to release ${version}"
-                echo
-                echo "Filename: ${tar_name}"
-                echo "SHA256:   ${tar_sha256}"
-                echo "MD5:      ${tar_md5}" ) | \
-                git -C "$repo" commit \
-                    --author="broadcom release <broadcom_release@broadcom.com>" \
-                    --date="$git_commit_date" \
-                    -F - \
-                    -q
-            ;;
-        esac
+        import_module "$repo" "$module" "$version" "${tar_dir}/${tar}"
     done
 
     # All modules have been added. Make the main branch and do the octopus merge.
     # but only if the tag exist.
     # BUG: so octopus do not work since broadcom adds the same binary file in mutiple addon modules. :(
-    if [ "$do_merge" == "1" ]
+    if git -C ${repo} tag  | grep "bcm_${version}_t$"
     then
-        if git -C ${repo} tag  | grep "bcm_${version}_t$"
-        then
-            git -C ${repo} reset --hard
-            git -C ${repo} clean -ffxd
-            git -C ${repo} checkout bcm_${version}_t
-            git -C ${repo} branch bcm_${version}
-            git -C ${repo} checkout bcm_${version}
-            git -C ${repo} reset --hard
-            git -C ${repo} clean -ffxd
-            branch_list=""
-            for module in $bcm_modules
-            do
-                case ${module} in
-                "data_full_release")
-                    true
-                    ;;
-                *)
-                    echo "trying to merge ${module} from branch ${version}_${module}"
-                    if git -C ${repo} branch | grep "bcm_${version}_${module}$"
-                    then
-                        if ! git -C ${repo} merge --no-commit --no-ff -Xtheirs bcm_${version}_${module}
-                        then
-                            # so this merge did not work. clean up and skip it.
-                            git -C ${repo} merge --abort
-                            git -C ${repo} reset --hard
-                            git -C ${repo} clean -ffxd
-                        return
-                        fi
-                        git -C ${repo} commit --author="broadcom release<broadcom_release@broadcom.com>" \
-                        -m "Merge in bcm_${version}_${module}"
-                    fi
-                    ;;
-                esac
-            done
-        fi
+        git -C ${repo} reset --hard
+        git -C ${repo} clean -ffxd
+        git -C ${repo} checkout bcm_${version}_t
+        git -C ${repo} branch bcm_${version} || true   # Ignore if branch exists already
+        git -C ${repo} checkout bcm_${version}
+        git -C ${repo} reset --hard
+        git -C ${repo} clean -ffxd
+        branch_list=""
+        for module in $bcm_modules
+        do
+            merge_module_into_bcm_branch "$repo" "$module" "$version"
+        done
     fi
 }
 
-- 
GitLab