Allow latest branch build as a toolchain source

Handle branch name as an argument for the -B command-line switch
as a reference to the latest build off that branch. This way developers
can specify target branch without checking the GCS bucket's content
or CI build grid.

Change-Id: Iff4ae4ee468c32f011f35d0166304ac4706af673
Reviewed-on: https://cos-review.googlesource.com/c/cos/tools/+/28400
Cloud-Build: GCB Service account <228075978874@cloudbuild.gserviceaccount.com>
Reviewed-by: Meena Shanmugam <meenashanmugam@google.com>
Tested-by: Oleksandr Tymoshenko <ovt@google.com>
diff --git a/src/cmd/cos_kernel_devenv/README.md b/src/cmd/cos_kernel_devenv/README.md
index ef870a5..498ee71 100644
--- a/src/cmd/cos_kernel_devenv/README.md
+++ b/src/cmd/cos_kernel_devenv/README.md
@@ -95,7 +95,10 @@
 Options:
   -A <arch>     target architecture. Valid values are x86 and arm64.
   -B <build>    seed the toolchain from the COS build <build>.
-                Example: R93-16623.0.0 Requires -b option.
+                Example: R93-16623.0.0. Instead of the actual
+                build number developer can specify the branch name
+                to use the latest build off that branch.
+                Example: main-R93, release-R89. Requires -b option.
   -C <config>   kernel config target. Example: lakitu_defconfig
   -G <bucket>   seed the toolchain and kernel headers from the custom
                 GCS bucket <bucket>. Directory structure needs to conform
diff --git a/src/cmd/cos_kernel_devenv/devenv.sh b/src/cmd/cos_kernel_devenv/devenv.sh
index 5319a14..1205d48 100755
--- a/src/cmd/cos_kernel_devenv/devenv.sh
+++ b/src/cmd/cos_kernel_devenv/devenv.sh
@@ -424,7 +424,10 @@
 Options:
   -A <arch>     target architecture. Valid values are x86 and arm64.
   -B <build>    seed the toolchain from the COS build <build>.
-                Example: R93-16623.0.0 Requires -b option.
+                Example: R93-16623.0.0. Instead of the actual
+                build number developer can specify the branch name
+                to use the latest build off that branch.
+                Example: main-R93, release-R89. Requires -b option.
   -C <config>   kernel config target. Example: lakitu_defconfig
   -G <bucket>   seed the toolchain and kernel headers from the custom
                 GCS bucket <bucket>. Directory structure needs to conform
@@ -511,6 +514,21 @@
     echo "** COS release: $RELEASE_ID"
   fi
 
+  if [[ -n "$BUILD_ID" ]]; then
+    if ! [[ $BUILD_ID =~ R[0-9]+-[0-9.]+ ]]; then
+      BRANCH="${BUILD_ID}"
+      echo "** Obtaining the latest build # for branch ${BRANCH}..."
+      readonly latest="${COS_CI_DOWNLOAD_GCS}/${BOARD}-release/LATEST-${BUILD_ID}"
+      BUILD_ID=$(gsutil -q cat "${latest}" || true)
+      if [[ -n "$BUILD_ID" ]]; then
+        echo "** Latest build for branch ${BRANCH} is ${BUILD_ID}"
+      else
+        echo "** Failed to find latest build for branch ${BRANCH}"
+        exit 1
+      fi
+    fi
+  fi
+
   if [[ -z "$MODE" && -n "$BOARD" && -n "$BUILD_ID" ]]; then
     MODE="build"
     echo "** COS build: $BOARD-$BUILD_ID"