2022-06-02 11:26:42 -07:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -o pipefail -o errexit
|
2023-02-15 13:50:55 -07:00
|
|
|
BASEDIR=$(RL=$(readlink -n "$0"); SP="${RL:-$0}"; dirname "$(cd "$(dirname "${SP}")"; pwd)/$(basename "${SP}")")
|
2022-06-02 11:26:42 -07:00
|
|
|
|
|
|
|
# Error handling
|
|
|
|
handle_error() {
|
|
|
|
read -n1 -r -p "FAILED: line $1, exit code $2. Press any key to exit..." _
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
trap 'handle_error $LINENO $?' ERR
|
|
|
|
|
|
|
|
# Load default script environment variables
|
|
|
|
# shellcheck source=.script_env
|
|
|
|
. "${BASEDIR}/.script_env"
|
|
|
|
|
|
|
|
# Ask for ref if not provided
|
|
|
|
if [[ -z "$VAULT_VERSION" ]]; then
|
2023-12-16 05:17:35 -07:00
|
|
|
read -rp "Input a git ref (commit hash, branch name, tag name, 'main'): " input
|
2023-12-18 09:09:47 -07:00
|
|
|
VAULT_VERSION="${input:-main}"
|
2022-06-02 11:26:42 -07:00
|
|
|
fi
|
|
|
|
|
2022-07-15 07:40:21 -07:00
|
|
|
# Check the format of the provided vault version
|
|
|
|
# If this is vYYYY.M.B or YYYY.M.B then fix this automatically to prepend web- or web-v
|
|
|
|
if [[ "${VAULT_VERSION}" =~ ^20[0-9]{2}\.[0-9]{1,2}.[0-9]{1} ]]; then
|
|
|
|
VAULT_VERSION="web-v${VAULT_VERSION}"
|
|
|
|
elif [[ "${VAULT_VERSION}" =~ ^v20[0-9]{2}\.[0-9]{1,2}.[0-9]{1} ]]; then
|
|
|
|
VAULT_VERSION="web-${VAULT_VERSION}"
|
|
|
|
fi
|
2024-07-11 09:21:51 -07:00
|
|
|
|
2022-07-15 07:40:21 -07:00
|
|
|
echo "Using: '${VAULT_VERSION}' to checkout bitwarden/client."
|
|
|
|
|
2022-06-02 11:26:42 -07:00
|
|
|
if [ ! -d "${VAULT_FOLDER}" ]; then
|
2023-12-18 09:09:47 -07:00
|
|
|
mkdir -pv "${VAULT_FOLDER}"
|
|
|
|
pushd "${VAULT_FOLDER}"
|
|
|
|
# If this is the first time, init the repo and checkout the requested branch/tag/hash
|
|
|
|
git -c init.defaultBranch=main init
|
|
|
|
git remote add origin https://github.com/bitwarden/clients.git
|
|
|
|
popd
|
2022-06-02 11:26:42 -07:00
|
|
|
else
|
|
|
|
# If there already is a checked-out repo, lets clean it up first.
|
|
|
|
pushd "${VAULT_FOLDER}"
|
2023-12-16 05:17:35 -07:00
|
|
|
# Stash current changes if there are any, we don't want to lose our work if we had some
|
2023-12-18 09:09:47 -07:00
|
|
|
echo "Stashing all custom changes"
|
2022-11-01 18:26:34 -07:00
|
|
|
git stash --include-untracked --quiet &> /dev/null || true
|
2023-12-18 09:09:47 -07:00
|
|
|
|
|
|
|
# Reset hard to make sure no changes are left
|
2022-06-02 11:26:42 -07:00
|
|
|
git reset --hard
|
|
|
|
popd
|
|
|
|
fi
|
|
|
|
|
2024-07-11 09:21:51 -07:00
|
|
|
if [[ "$CHECKOUT_TAGS" == "true" ]]; then
|
|
|
|
CHECKOUT_ARGS="${CHECKOUT_ARGS:-} --tags"
|
|
|
|
fi
|
|
|
|
|
2023-12-18 09:09:47 -07:00
|
|
|
# Checkout the request
|
2022-06-02 11:26:42 -07:00
|
|
|
pushd "${VAULT_FOLDER}"
|
2023-12-18 09:09:47 -07:00
|
|
|
# Update branch and tag metadata
|
2024-07-11 09:21:51 -07:00
|
|
|
git fetch --depth 1 ${CHECKOUT_ARGS:-} origin "${VAULT_VERSION}"
|
2023-12-18 09:09:47 -07:00
|
|
|
# Checkout the branch we want
|
|
|
|
git -c advice.detachedHead=false checkout FETCH_HEAD
|
2022-06-02 11:26:42 -07:00
|
|
|
popd
|