4646 AUTH_REPO_URL="$REPO_URL"
4747 if [[ -n "$GIT_AUTH_TOKEN" && "$REPO_URL" == https://* ]]; then
4848 AUTH_REPO_URL="$(printf "%s" "$REPO_URL" | sed "s#^https://#https://\${GIT_AUTH_USER}:\${GIT_AUTH_TOKEN}@#")"
49+ fi
50+
51+ CLONE_CACHE_ARGS=""
52+ CACHE_REPO_DIR=""
53+ CACHE_ROOT="/home/${ config . sshUser } /.docker-git/.cache/git-mirrors"
54+ if command -v sha256sum >/dev/null 2>&1; then
55+ REPO_CACHE_KEY="$(printf "%s" "$REPO_URL" | sha256sum | awk '{print $1}')"
56+ elif command -v shasum >/dev/null 2>&1; then
57+ REPO_CACHE_KEY="$(printf "%s" "$REPO_URL" | shasum -a 256 | awk '{print $1}')"
58+ else
59+ REPO_CACHE_KEY="$(printf "%s" "$REPO_URL" | tr '/:@' '_' | tr -cd '[:alnum:]_.-')"
60+ fi
61+
62+ if [[ -n "$REPO_CACHE_KEY" ]]; then
63+ CACHE_REPO_DIR="$CACHE_ROOT/$REPO_CACHE_KEY.git"
64+ mkdir -p "$CACHE_ROOT"
65+ chown 1000:1000 "$CACHE_ROOT" || true
66+ if [[ -d "$CACHE_REPO_DIR" ]]; then
67+ if su - ${ config . sshUser } -c "git --git-dir '$CACHE_REPO_DIR' rev-parse --is-bare-repository >/dev/null 2>&1"; then
68+ if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git --git-dir '$CACHE_REPO_DIR' fetch --progress --prune '$REPO_URL' '+refs/*:refs/*'"; then
69+ echo "[clone-cache] mirror refresh failed for $REPO_URL"
70+ fi
71+ CLONE_CACHE_ARGS="--reference-if-able '$CACHE_REPO_DIR' --dissociate"
72+ echo "[clone-cache] using mirror: $CACHE_REPO_DIR"
73+ else
74+ echo "[clone-cache] invalid mirror removed: $CACHE_REPO_DIR"
75+ rm -rf "$CACHE_REPO_DIR"
76+ fi
77+ fi
4978 fi`
5079
5180const renderCloneBodyRef = ( config : TemplateConfig ) : string =>
5281 ` if [[ -n "$REPO_REF" ]]; then
5382 if [[ "$REPO_REF" == refs/pull/* ]]; then
5483 REF_BRANCH="pr-$(printf "%s" "$REPO_REF" | tr '/:' '--')"
55- if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress '$AUTH_REPO_URL' '$TARGET_DIR'"; then
84+ if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress $CLONE_CACHE_ARGS '$AUTH_REPO_URL' '$TARGET_DIR'"; then
5685 echo "[clone] git clone failed for $REPO_URL"
5786 CLONE_OK=0
5887 else
@@ -62,12 +91,12 @@ const renderCloneBodyRef = (config: TemplateConfig): string =>
6291 fi
6392 fi
6493 else
65- if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress --branch '$REPO_REF' '$AUTH_REPO_URL' '$TARGET_DIR'"; then
94+ if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress $CLONE_CACHE_ARGS --branch '$REPO_REF' '$AUTH_REPO_URL' '$TARGET_DIR'"; then
6695 DEFAULT_REF="$(git ls-remote --symref "$AUTH_REPO_URL" HEAD 2>/dev/null | awk '/^ref:/ {print $2}' | head -n 1 || true)"
6796 DEFAULT_BRANCH="$(printf "%s" "$DEFAULT_REF" | sed 's#^refs/heads/##')"
6897 if [[ -n "$DEFAULT_BRANCH" ]]; then
6998 echo "[clone] branch '$REPO_REF' missing; retrying with '$DEFAULT_BRANCH'"
70- if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress --branch '$DEFAULT_BRANCH' '$AUTH_REPO_URL' '$TARGET_DIR'"; then
99+ if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress $CLONE_CACHE_ARGS --branch '$DEFAULT_BRANCH' '$AUTH_REPO_URL' '$TARGET_DIR'"; then
71100 echo "[clone] git clone failed for $REPO_URL"
72101 CLONE_OK=0
73102 elif [[ "$REPO_REF" == issue-* ]]; then
@@ -83,12 +112,27 @@ const renderCloneBodyRef = (config: TemplateConfig): string =>
83112 fi
84113 fi
85114 else
86- if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress '$AUTH_REPO_URL' '$TARGET_DIR'"; then
115+ if ! su - ${ config . sshUser } -c "GIT_TERMINAL_PROMPT=0 git clone --progress $CLONE_CACHE_ARGS '$AUTH_REPO_URL' '$TARGET_DIR'"; then
87116 echo "[clone] git clone failed for $REPO_URL"
88117 CLONE_OK=0
89118 fi
90119 fi`
91120
121+ const renderCloneCacheFinalize = ( config : TemplateConfig ) : string =>
122+ `if [[ "$CLONE_OK" -eq 1 && -d "$TARGET_DIR/.git" && -n "$CACHE_REPO_DIR" && ! -d "$CACHE_REPO_DIR" ]]; then
123+ CACHE_TMP_DIR="$CACHE_REPO_DIR.tmp-$$"
124+ if su - ${ config . sshUser } -c "rm -rf '$CACHE_TMP_DIR' && GIT_TERMINAL_PROMPT=0 git clone --mirror --progress '$TARGET_DIR/.git' '$CACHE_TMP_DIR'"; then
125+ if mv "$CACHE_TMP_DIR" "$CACHE_REPO_DIR" 2>/dev/null; then
126+ echo "[clone-cache] mirror created: $CACHE_REPO_DIR"
127+ else
128+ rm -rf "$CACHE_TMP_DIR"
129+ fi
130+ else
131+ echo "[clone-cache] mirror bootstrap failed for $REPO_URL"
132+ rm -rf "$CACHE_TMP_DIR"
133+ fi
134+ fi`
135+
92136const renderIssueWorkspaceAgentsResolve = ( ) : string =>
93137 `ISSUE_ID="$(printf "%s" "$REPO_REF" | sed -E 's#^issue-##')"
94138ISSUE_URL=""
@@ -180,6 +224,8 @@ const renderCloneBody = (config: TemplateConfig): string =>
180224 "" ,
181225 renderCloneRemotes ( config ) ,
182226 "" ,
227+ renderCloneCacheFinalize ( config ) ,
228+ "" ,
183229 renderIssueWorkspaceAgents ( )
184230 ] . join ( "\n" )
185231
0 commit comments