From db6bbad75b79188a88cdc603fd7b27bfde635d86 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sun, 1 Oct 2023 19:39:08 +0200 Subject: [PATCH] fix: git clone by refspec --- rootfs/usr/local/bin/clone-commit | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/rootfs/usr/local/bin/clone-commit b/rootfs/usr/local/bin/clone-commit index be3f696..64632b4 100755 --- a/rootfs/usr/local/bin/clone-commit +++ b/rootfs/usr/local/bin/clone-commit @@ -17,7 +17,7 @@ fi # is empty we checkout the sha directly. Note that # we intentially omit depth flags to avoid failed # clones due to lack of history. -if [[ -z "${DRONE_COMMIT_BRANCH}" ]]; then +if [[ -z "${DRONE_COMMIT_BRANCH}" ]] && [[ -n "${DRONE_COMMIT_SHA}" ]]; then set -e set -x git fetch origin @@ -28,16 +28,18 @@ fi # 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. -if [[ -z "${DRONE_COMMIT_SHA}" ]]; then +if [[ -n "${DRONE_COMMIT_BRANCH}" ]] && [[ -z "${DRONE_COMMIT_SHA}" ]]; then set -e set -x - git fetch "${FLAGS}" origin "+refs/heads/${DRONE_COMMIT_BRANCH}:" - git checkout -b "${DRONE_COMMIT_BRANCH}" "origin/${DRONE_COMMIT_BRANCH}" + git fetch "${FLAGS}" origin "+refs/heads/${DRONE_COMMIT_BRANCH}:refs/remotes/origin/${DRONE_COMMIT_BRANCH}" + git checkout -b "${DRONE_COMMIT_BRANCH}" "refs/remotes/origin/${DRONE_COMMIT_BRANCH}" exit 0 fi +# 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. set -e set -x - -git fetch "${FLAGS}" origin "+refs/heads/${DRONE_COMMIT_BRANCH}:" -git checkout "${DRONE_COMMIT_SHA}" -b "${DRONE_COMMIT_BRANCH}" +git fetch "${FLAGS}" origin "+refs/heads/${DRONE_COMMIT_BRANCH}:refs/remotes/origin/${DRONE_COMMIT_BRANCH}" +git checkout "${DRONE_COMMIT_SHA}" -b "refs/remotes/origin/${DRONE_COMMIT_BRANCH}"