@@ -130,6 +130,8 @@ const cloneCacheRefreshRefspecs = "'+refs/heads/*:refs/heads/*' '+refs/tags/*:re
130130
131131const renderCloneCacheInit = ( config : TemplateConfig ) : string =>
132132 ` CLONE_CACHE_ARGS=""
133+ CLONE_SOURCE_REPO="$AUTH_REPO_URL"
134+ CLONE_USED_CACHE=0
133135 CACHE_REPO_DIR=""
134136 CACHE_ROOT="/home/${ config . sshUser } /.docker-git/.cache/git-mirrors"
135137 if command -v sha256sum >/dev/null 2>&1; then
@@ -150,6 +152,8 @@ const renderCloneCacheInit = (config: TemplateConfig): string =>
150152 echo "[clone-cache] mirror refresh failed for $REPO_URL"
151153 fi
152154 CLONE_CACHE_ARGS="--reference-if-able '$CACHE_REPO_DIR' --dissociate"
155+ CLONE_SOURCE_REPO="$CACHE_REPO_DIR"
156+ CLONE_USED_CACHE=1
153157 echo "[clone-cache] using mirror: $CACHE_REPO_DIR"
154158 else
155159 echo "[clone-cache] invalid mirror removed: $CACHE_REPO_DIR"
@@ -170,19 +174,19 @@ const renderCloneBodyRef = (config: TemplateConfig): string =>
170174 String . raw ` if [[ -n "$REPO_REF" ]]; then
171175 if [[ "$REPO_REF" == refs/pull/* || "$REPO_REF" == refs/merge-requests/* ]]; then
172176 REF_BRANCH="$(printf "%s" "$REPO_REF" | sed -E 's#^refs/pull/([^/]+)/head$#pr-\1#; s#^refs/merge-requests/([^/]+)/head$#mr-\1#')"
173- if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress $CLONE_CACHE_ARGS '$AUTH_REPO_URL ' '$TARGET_DIR'"; then
177+ if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress $CLONE_CACHE_ARGS '$CLONE_SOURCE_REPO ' '$TARGET_DIR'"; then
174178 echo "[clone] git clone failed for $REPO_URL"
175179 CLONE_OK=0
176180 else
177- if ! su - ${ config . sshUser } -c "cd '$TARGET_DIR' && GIT_TERMINAL_PROMPT=0 git fetch --progress origin '$REPO_REF':'$REF_BRANCH' && git checkout '$REF_BRANCH'"; then
181+ if ! su - ${ config . sshUser } -c "cd '$TARGET_DIR' && git remote set-url origin '$AUTH_REPO_URL' && GIT_TERMINAL_PROMPT=0 git fetch --progress origin '$REPO_REF':'$REF_BRANCH' && git checkout '$REF_BRANCH'"; then
178182 echo "[clone] git fetch failed for $REPO_REF"
179183 CLONE_OK=0
180184 fi
181185 fi
182186 else
183- if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress $CLONE_CACHE_ARGS --branch '$REPO_REF' '$AUTH_REPO_URL ' '$TARGET_DIR'"; then
187+ if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress $CLONE_CACHE_ARGS --branch '$REPO_REF' '$CLONE_SOURCE_REPO ' '$TARGET_DIR'"; then
184188 echo "[clone] branch '$REPO_REF' missing; retrying without --branch"
185- if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress $CLONE_CACHE_ARGS '$AUTH_REPO_URL ' '$TARGET_DIR'"; then
189+ if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress $CLONE_CACHE_ARGS '$CLONE_SOURCE_REPO ' '$TARGET_DIR'"; then
186190 echo "[clone] git clone failed for $REPO_URL"
187191 CLONE_OK=0
188192 elif [[ "$REPO_REF" == issue-* ]]; then
@@ -191,12 +195,26 @@ const renderCloneBodyRef = (config: TemplateConfig): string =>
191195 CLONE_OK=0
192196 fi
193197 fi
198+ elif [[ "$CLONE_USED_CACHE" == "1" ]]; then
199+ if ! su - ${ config . sshUser } -c "cd '$TARGET_DIR' && git remote set-url origin '$AUTH_REPO_URL' && GIT_TERMINAL_PROMPT=0 git pull --ff-only origin '$REPO_REF'"; then
200+ echo "[clone-cache] git pull failed for $REPO_REF"
201+ CLONE_OK=0
202+ else
203+ echo "[clone-cache] pulled branch: $REPO_REF"
204+ fi
194205 fi
195206 fi
196207 else
197- if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress $CLONE_CACHE_ARGS '$AUTH_REPO_URL ' '$TARGET_DIR'"; then
208+ if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress $CLONE_CACHE_ARGS '$CLONE_SOURCE_REPO ' '$TARGET_DIR'"; then
198209 echo "[clone] git clone failed for $REPO_URL"
199210 CLONE_OK=0
211+ elif [[ "$CLONE_USED_CACHE" == "1" ]]; then
212+ if ! su - ${ config . sshUser } -c "cd '$TARGET_DIR' && git remote set-url origin '$AUTH_REPO_URL' && GIT_TERMINAL_PROMPT=0 git pull --ff-only"; then
213+ echo "[clone-cache] git pull failed for default branch"
214+ CLONE_OK=0
215+ else
216+ echo "[clone-cache] pulled default branch"
217+ fi
200218 fi
201219 fi`
202220
0 commit comments