#!/bin/sh

# Determines the start and end commits for a CI job.
# Works under Azure Pipelines, CircleCI, GitHub Actions, and Travis CI.
#
# The client `eval`s this script's output.  Script `set-git-range` computes
# the same information, but the client sources it.  Prefer to use
# `set-git-range` instead:  this script's exit status is lost by the `eval`,
# which reports the status of the text it evaluated rather than of this script.

# Typical use:
#
#   PLUME_SCRIPTS="/tmp/${USER:-$(id -un)}/plume-scripts"
#   if [ -d "$PLUME_SCRIPTS" ] ; then
#     git -C "$PLUME_SCRIPTS" pull -q > /dev/null 2>&1
#   else
#     git clone --depth=1 -q https://github.com/plume-lib/plume-scripts.git "$PLUME_SCRIPTS"
#   fi
#   eval "$("$PLUME_SCRIPTS"/git-changes DEFAULT-ORGANIZATION)"
#
# Alternatively, to use a specific version of this script rather than the latest:
#
#   PLUME_SCRIPTS="/tmp/${USER:-$(id -un)}/plume-scripts"
#   if [ ! -d "$PLUME_SCRIPTS" ] ; then
#     git clone --revision=e768754ffafad4a9f1e26c5305591d4061d890b4 --depth=1 -q https://github.com/plume-lib/plume-scripts.git "$PLUME_SCRIPTS"
#   fi
#   eval "$("$PLUME_SCRIPTS"/git-changes DEFAULT-ORGANIZATION)"

# Optional argument DEFAULT-ORGANIZATION is used if no organization can be
# determined; variable CI_DEFAULT_ORGANIZATION is used if it is not supplied.

# Either snippet of code sets these variables:
# CI_COMMIT_RANGE: An argument to `git diff` with the range of commit IDs.
#    (Don't use it with `git log`, which interprets its argument differently.)
# CI_COMMIT_RANGE_START: The start element of CI_COMMIT_RANGE, a commit ID/SHA.
# CI_COMMIT_RANGE_END: The end element of CI_COMMIT_RANGE, a commit ID/SHA.
#    For an Azure Pipelines pull request, it is the merge commit that Azure
#    checks out, which is on no branch of the repository.
# CI_ORGANIZATION and CI_BRANCH, which `ci-org-and-branch` also sets.
#
# If called not under CI, or under CI but not in the main or toplevel
# clone, then gets information from the local repository.
#
# If it cannot determine the information, it sets none of the variables and its
# output is `exit 2`, so that the client's `eval` exits with status 2.
#
# Diagnostics requested by --verbose or --debug are written to standard error,
# so that only the variable settings appear on standard output.

# Requires the `jq` program, either `curl` or `wget`, and the scripts
# `eval-wrapper.sh`, `set-git-range`, and `set-ci-org-and-branch` in the same
# directory as this script.
#
# If environment variable GITHUB_PAT or GH_TOKEN is set when this script is
# called, it is used as GitHub Personal Access Token when making GitHub API
# calls.  This can avoid "403 rate limit exceeded" failures.

_ew_script_dir="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)"
_ew_helper="set-git-range"
_ew_variables="CI_ORGANIZATION CI_BRANCH CI_COMMIT_RANGE_START CI_COMMIT_RANGE_END CI_COMMIT_RANGE"
# `eval-wrapper.sh` parses the arguments, runs `set-git-range`, and prints the
# variables.  `ci-org-and-branch` shares it, so that the two scripts cannot
# drift apart.
#
# `.` on a file that does not exist is an error in a special builtin, so the
# shell aborts at once, without running anything below and without writing
# anything on standard output.  The client's `eval` would then succeed, with no
# value (or a stale value) for each variable -- the silent success that the
# `exit` commands in `eval-wrapper.sh` exist to prevent.  A client that copies
# individual scripts rather than cloning this repository can easily be missing
# this one, because it is new.
if [ ! -f "${_ew_script_dir}/eval-wrapper.sh" ] || [ ! -r "${_ew_script_dir}/eval-wrapper.sh" ]; then
  echo "exit 2"
  echo "$0: cannot read ${_ew_script_dir}/eval-wrapper.sh" >&2
  exit 2
fi
# shellcheck source=eval-wrapper.sh
. "${_ew_script_dir}"/eval-wrapper.sh
