blob: 1255f6f87c3a871418ecde9b00a103dfa49b4ec7 [file] [log] [blame]
#!/bin/bash
# Copyright 2025 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
set -o errexit
readonly SCRIPTS_COG_BIN_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
readonly PY_TOOL="${SCRIPTS_COG_BIN_DIR}/../../fuchsia-vendored-python"
readonly REPO_ROOT="$(realpath ${SCRIPTS_COG_BIN_DIR}/../../..)"
# This script acts as a wrapper around the system 'git' command.
# However, the underlying Python script (git.py) may still need to invoke the
# actual 'git' binary (e.g., for 'git citc' commands).
#
# We need to explicitly locate and pass the path to the real 'git' binary
# because 'fuchsia-vendored-python' executes within a hermetic environment
# that strips out most environment variables. This prevents us from simply
# passing the location via an environment variable like FUCHSIA_REAL_GIT.
function find_real_git() {
local cmd_name
cmd_name=$(basename "$0")
local current_script_path
current_script_path=$(realpath "$0")
for candidate in $(which -a "$cmd_name"); do
local candidate_resolved
candidate_resolved=$(realpath "$candidate")
if [ "$candidate_resolved" != "$current_script_path" ]; then
echo "$candidate"
return 0
fi
done
return 1
}
function main() {
local real_git
if ! real_git=$(find_real_git); then
echo "Error: Could not find real git binary." >&2
exit 1
fi
local log_args=()
if [[ -n "${FUCHSIA_LOG_GIT_POLYFILL_COMMANDS}" ]]; then
log_args+=(--log-file "${FUCHSIA_LOG_GIT_POLYFILL_COMMANDS}")
fi
# We need to run this script from within the fuchsia directory or else the hermetic-env will not work.
# This is because we might be running this from the build directory which is symlinked to the
# fuchsia directory and the hermetic-env will not work if we are not in the fuchsia directory.
# We could run this from the FUCHSIA_DIR but this might not be set and we want to ensure that we
# are using a variable that is set so we run it in the scripts/cog/bin directory.
exec env -C "${SCRIPTS_COG_BIN_DIR}" "$PY_TOOL" "git.py" \
"${log_args[@]}" \
--invoker-cwd "$(pwd)" \
--real-git "${real_git}" \
--repository-root "${REPO_ROOT}" \
-- "$@"
}
main "$@"