git-docker/rootfs/usr/local/bin/clone-commit

46 lines
1.7 KiB
Plaintext
Raw Normal View History

2023-10-01 16:54:37 +00:00
#!/bin/bash
2023-10-01 18:44:02 +00:00
set -e
2023-10-01 16:54:37 +00:00
set -x
2018-08-08 21:43:53 +00:00
2023-10-01 18:44:02 +00:00
GIT_FETCH_ARGS=""
GIT_CHECKOUT_ARGS=""
2023-10-01 16:54:37 +00:00
if [[ -n "${PLUGIN_DEPTH}" ]]; then
2023-10-01 18:44:02 +00:00
GIT_FETCH_ARGS="${GIT_FETCH_ARGS} --depth=${PLUGIN_DEPTH}"
2018-08-08 21:43:53 +00:00
fi
if [ ! -d .git ]; then
2023-10-01 18:44:02 +00:00
git init
git remote add origin "${DRONE_REMOTE_URL}"
2018-08-08 21:43:53 +00:00
fi
2020-04-28 22:54:00 +00:00
# the branch may be empty for certain event types,
# such as github deployment events. If the branch
# is empty we checkout the sha directly. Note that
# we intentially omit depth flags to avoid failed
# clones due to lack of history.
2023-10-01 17:39:08 +00:00
if [[ -z "${DRONE_COMMIT_BRANCH}" ]] && [[ -n "${DRONE_COMMIT_SHA}" ]]; then
2023-10-01 18:44:02 +00:00
GIT_CHECKOUT_ARGS="${GIT_CHECKOUT_ARGS} --quiet --force ${DRONE_COMMIT_SHA}"
GIT_FETCH_ARGS="${GIT_FETCH_ARGS} origin"
2020-04-28 22:54:00 +00:00
fi
2020-10-29 14:18:31 +00:00
# the commit sha may be empty for builds that are
# manually triggered in Harness CI Enterprise. If
# the commit is empty we clone the branch.
2023-10-01 17:39:08 +00:00
if [[ -n "${DRONE_COMMIT_BRANCH}" ]] && [[ -z "${DRONE_COMMIT_SHA}" ]]; then
2023-10-01 18:44:02 +00:00
GIT_CHECKOUT_ARGS="${GIT_CHECKOUT_ARGS} -b ${DRONE_COMMIT_BRANCH} refs/remotes/origin/${DRONE_COMMIT_BRANCH}"
GIT_FETCH_ARGS="${GIT_FETCH_ARGS} origin +refs/heads/${DRONE_COMMIT_BRANCH}:refs/remotes/origin/${DRONE_COMMIT_BRANCH}"
fi
2023-10-01 17:39:08 +00:00
# if the commit sha and branch name not empty, fetch the branch even if a
# fast-forward is not possible. Checkout the specified commit sha which must be
# part of the branch.
2023-10-01 18:44:02 +00:00
if [[ -n "${DRONE_COMMIT_BRANCH}" ]] && [[ -n "${DRONE_COMMIT_SHA}" ]]; then
GIT_CHECKOUT_ARGS="${GIT_CHECKOUT_ARGS} -b ${DRONE_COMMIT_SHA} -b refs/remotes/origin/${DRONE_COMMIT_BRANCH}"
GIT_FETCH_ARGS="${GIT_FETCH_ARGS} origin +refs/heads/${DRONE_COMMIT_BRANCH}:refs/remotes/origin/${DRONE_COMMIT_BRANCH}"
fi
git fetch ${GIT_FETCH_ARGS}
git checkout ${GIT_CHECKOUT_ARGS}