xref: /aosp_15_r20/external/skia/bazel/get_workspace_status.sh (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1#!/bin/bash -e
2# Copyright 2023 Google LLC
3#
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# This script is intended to be passed to Bazel using the --workspace_status_command command-line
8# flag. It defines various key/value pairs, such as the Git hash or clean/dirty status, which can be
9# used from BUILD files, e.g. to tag Docker images.
10#
11# See https://bazel.build/docs/user-manual#flag--workspace_status_command.
12
13# Default values used if we are outside of a Git checkout, e.g. when building inside a tryjob.
14STABLE_GIT_REVISION=unversioned
15STABLE_GIT_STATUS=unversioned
16
17# If we are inside a Git checkout, then obtain the Git revision and the clean/dirty status.
18if git status > /dev/null 2> /dev/null; then
19  STABLE_GIT_REVISION=`git rev-parse HEAD`
20
21  # Check whether there are any uncommitted changes.
22  #
23  # Based on:
24  # https://skia.googlesource.com/buildbot/+/cdbd6dc7cd9e06604042bb53a6179a77b4c83c25/bash/docker_build.sh#53
25  STABLE_GIT_STATUS=clean
26  # Detect if we have unchecked in local changes, or if we're not on the main branch (possibly at
27  # an older revision).
28  git fetch > /dev/null
29  # diff-index requires update-index --refresh; see:
30  # https://stackoverflow.com/questions/36367190/git-diff-files-output-changes-after-git-status/36439778#36439778
31  if git update-index --refresh > /dev/null ; then
32    if ! git diff-index --quiet HEAD -- ; then
33      # Repository is dirty due to modified files.
34      STABLE_GIT_STATUS=dirty
35    elif ! git merge-base --is-ancestor HEAD origin/main ; then
36      # Repository is dirty because we're not on the main branch (possibly an older revision).
37      STABLE_GIT_STATUS=dirty
38    fi
39  else
40    # Repository is dirty due to checked out files.
41    STABLE_GIT_STATUS=dirty
42  fi
43fi
44
45BUILD_DATETIME=`date -u +%Y-%m-%dT%H_%M_%SZ`
46
47echo "BUILD_DATETIME $BUILD_DATETIME"
48echo "STABLE_GIT_REVISION $STABLE_GIT_REVISION"
49echo "STABLE_GIT_STATUS $STABLE_GIT_STATUS"
50
51# If the format of this ever changes then please also update k8s_checker/main.go.
52echo "STABLE_DOCKER_TAG ${BUILD_DATETIME}-${USER}-${STABLE_GIT_REVISION:0:7}-${STABLE_GIT_STATUS}"
53