blob: 98a0cb877063a197fec5e032e559cf7488f06ee5 [file]
#!/bin/bash
# Copyright 2026 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
# Wrapper script to run gh-ish with prebuilt caching by Git commit hash.
#
# If the current Git commit hash matches the saved hash of the prebuilt binary,
# it executes the prebuilt directly with zero Bazel startup overhead. If the hash
# changed or the prebuilt does not exist, it builds //pw_ghish:gh-ish via
# bazelisk and caches the resulting binary.
set -e
if test -n "$BASH"; then
declare -r _PW_SCRIPT_DIR=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")
elif test -n "$ZSH_NAME"; then
declare -r _PW_SCRIPT_DIR="${0:h:A}"
else
declare -r _PW_SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
fi
export BAZELISK_SKIP_WRAPPER=1
if [ -d "${_PW_SCRIPT_DIR}/environment/cipd/packages/bazelisk" ]; then
export PATH="${_PW_SCRIPT_DIR}/environment/cipd/packages/bazelisk:${PATH}"
elif [ -d "$HOME/bin" ]; then
export PATH="$HOME/bin:${PATH}"
fi
# Prebuilt cache directory (defaults to out/gh, which is .gitignored in Pigweed).
declare -r _GH_DIR="${_PW_GH_OUT_DIR:-${_PW_SCRIPT_DIR}/out/gh}"
declare -r _GH_BIN="${_GH_DIR}/gh-ish"
declare -r _GH_HASH_FILE="${_GH_DIR}/git_hash"
_CURRENT_HASH=$(git -C "$_PW_SCRIPT_DIR" rev-parse HEAD 2>/dev/null || true)
if [ -z "$_CURRENT_HASH" ] && [ -n "${PW_GH_DEBUG:-}" ]; then
echo "Warning: git rev-parse HEAD failed in $_PW_SCRIPT_DIR; caching disabled." >&2
fi
_STALE=1
_DIRTY=$(git -C "$_PW_SCRIPT_DIR" status --porcelain -- pw_ghish 2>/dev/null || true)
if [ -z "${PW_GH_FORCE_BUILD:-}" ] && [ -z "$_DIRTY" ] && [ -f "$_GH_BIN" ] && [ -f "$_GH_HASH_FILE" ] && [ -n "$_CURRENT_HASH" ]; then
_SAVED_HASH=$(cat "$_GH_HASH_FILE" 2>/dev/null || true)
if [ "$_CURRENT_HASH" = "$_SAVED_HASH" ]; then
_STALE=0
fi
fi
if [ $_STALE -eq 1 ]; then
if [ -t 1 ]; then
if [ -n "$_DIRTY" ]; then
printf " ⏱ Building gh-ish (local uncommitted changes in pw_ghish)...\n"
else
printf " ⏱ Building gh-ish (commit %.8s)...\n" "${_CURRENT_HASH:-unknown}"
fi
fi
(
cd "$_PW_SCRIPT_DIR"
bazelisk build \
--noshow_progress \
--noshow_loading_progress \
//pw_ghish:gh-ish
)
_BUILT_BIN="${_PW_SCRIPT_DIR}/bazel-bin/pw_ghish/gh-ish_/gh-ish"
if [ ! -f "$_BUILT_BIN" ]; then
_BUILT_BIN="${_PW_SCRIPT_DIR}/bazel-bin/pw_ghish/gh-ish"
fi
if [ ! -f "$_BUILT_BIN" ]; then
echo "Error: built binary not found at $_BUILT_BIN" >&2
exit 1
fi
mkdir -p "$_GH_DIR"
cp -f "$_BUILT_BIN" "$_GH_BIN"
chmod +x "$_GH_BIN"
if [ -n "$_CURRENT_HASH" ] && [ -z "$_DIRTY" ]; then
echo "$_CURRENT_HASH" > "$_GH_HASH_FILE"
else
rm -f "$_GH_HASH_FILE"
fi
fi
export BUILD_WORKSPACE_DIRECTORY="${_PW_SCRIPT_DIR}"
export BUILD_WORKING_DIRECTORY="${PWD}"
exec "$_GH_BIN" "$@"