afdo_tools: Update script to sort profiles by timestamp

Our update_kernel_afdo tries to get all the profiles with gsutil.
However, the command gives results by sorting the profile name,
so the last profile is not necessarily the newest. A counter
example is:
R86-13310.3-1594633089.gcov.xz appears after
R86-13310.18-1595237847.gcov.xz

So this patch sorts the results of gsutil by the timestamp and
grep all the master profiles (otherwise we might get some
beta/stable profiles).

BUG=None
TEST=Tool can be used to generate kernel_afdo.json CL

Change-Id: I9c9f6ee2ab446f5e07ea577e11f0c3d562bff5e3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/2321122
Commit-Queue: Tiancong Wang <tcwang@google.com>
Tested-by: Tiancong Wang <tcwang@google.com>
Reviewed-by: George Burgess <gbiv@chromium.org>
diff --git a/afdo_tools/update_kernel_afdo b/afdo_tools/update_kernel_afdo
index aac891c..64e5e47 100755
--- a/afdo_tools/update_kernel_afdo
+++ b/afdo_tools/update_kernel_afdo
@@ -1,4 +1,4 @@
-#! /bin/sh
+#!/bin/bash
 
 # Due to crbug.com/1081332, we need to update AFDO metadata
 # manually. This script performs a few checks and generates a
@@ -24,6 +24,7 @@
 #
 
 set -eu
+set -o pipefail
 
 GS_BASE=gs://chromeos-prebuilt/afdo-job/vetted/kernel
 KVERS="3.18 4.4 4.14 4.19"
@@ -43,11 +44,17 @@
   expected_time=$(date +%s -d "last Monday")
 fi
 
+# Get the current master branch number (using beta + 1)
+beta=$(git ls-remote -h https://chromium.googlesource.com/chromiumos/overlays/chromiumos-overlay | sed -n -e "s/^.*release-R\([0-9][0-9]*\).*$/\1/p" | sort -g | tail -1)
+master="$(($beta + 1))"
+
 json="{"
 sep=""
 for kver in $KVERS
 do
-  latest=$(gsutil ls -l "$GS_BASE/$kver/" | tail -n 2 | head -n 1)
+  # Sort the gs output by timestamp (default ordering is by name, so
+  # R86-13310.3-1594633089.gcov.xz goes after R86-13310.18-1595237847.gcov.xz)
+  latest=$(gsutil ls -l "$GS_BASE/$kver/" | sort -k2 | grep "R${master}" | tail -1)
 
   # Verify that the file has the expected date.
   file_time=$(echo "$latest" | awk '{print $2}')