Skip to content

Commit 32683c7

Browse files
committed
chore(launcher): ensure cloned repos track origin refs
- Expand clone flow to set fetch refspecs, use tokens, and checkout main - Harden sync_main to reuse existing repos, fetch all refs, and reset clean
1 parent dc674ed commit 32683c7

File tree

1 file changed

+57
-7
lines changed

1 file changed

+57
-7
lines changed

crates/agent-workspace/src/launcher/container.rs

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,37 @@ fi
145145
146146
mkdir -p "$(dirname "$dest")"
147147
148+
clone_repo() {
149+
if [[ -n "${GH_TOKEN:-${GITHUB_TOKEN:-}}" ]]; then
150+
askpass="/tmp/agent-workspace-git-askpass"
151+
cat >"$askpass" <<EOS
152+
#!/usr/bin/env bash
153+
case "${1-}" in
154+
*Username*) echo "x-access-token" ;;
155+
*Password*) echo "${GH_TOKEN:-${GITHUB_TOKEN:-}}" ;;
156+
*) echo "" ;;
157+
esac
158+
EOS
159+
chmod 700 "$askpass"
160+
if [[ -n "$ref" ]]; then
161+
GIT_TERMINAL_PROMPT=0 GIT_ASKPASS="$askpass" git clone "$repo_url" "$dest"
162+
else
163+
GIT_TERMINAL_PROMPT=0 GIT_ASKPASS="$askpass" git clone --branch main "$repo_url" "$dest"
164+
fi
165+
rm -f "$askpass"
166+
else
167+
if [[ -n "$ref" ]]; then
168+
GIT_TERMINAL_PROMPT=0 git clone "$repo_url" "$dest"
169+
else
170+
GIT_TERMINAL_PROMPT=0 git clone --branch main "$repo_url" "$dest"
171+
fi
172+
fi
173+
}
174+
175+
clone_repo
176+
177+
git -C "$dest" config --unset-all remote.origin.fetch || true
178+
git -C "$dest" config --add remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
148179
if [[ -n "${GH_TOKEN:-${GITHUB_TOKEN:-}}" ]]; then
149180
askpass="/tmp/agent-workspace-git-askpass"
150181
cat >"$askpass" <<EOS
@@ -156,14 +187,17 @@ case "${1-}" in
156187
esac
157188
EOS
158189
chmod 700 "$askpass"
159-
GIT_TERMINAL_PROMPT=0 GIT_ASKPASS="$askpass" git clone "$repo_url" "$dest"
190+
GIT_TERMINAL_PROMPT=0 GIT_ASKPASS="$askpass" git -C "$dest" fetch --prune origin "+refs/heads/*:refs/remotes/origin/*"
160191
rm -f "$askpass"
161192
else
162-
GIT_TERMINAL_PROMPT=0 git clone "$repo_url" "$dest"
193+
GIT_TERMINAL_PROMPT=0 git -C "$dest" fetch --prune origin "+refs/heads/*:refs/remotes/origin/*"
163194
fi
164195
165196
if [[ -n "$ref" ]]; then
166197
git -C "$dest" checkout "$ref"
198+
else
199+
git -C "$dest" checkout -B main origin/main
200+
git -C "$dest" reset --hard origin/main
167201
fi
168202
"#;
169203

@@ -195,9 +229,25 @@ nils_formula="${AGENT_WORKSPACE_NILS_CLI_FORMULA:-graysurf/tap/nils-cli}"
195229
sync_main() {
196230
local target_dir="$1"
197231
local repo_url="$2"
198-
rm -rf "$target_dir"
199-
mkdir -p "$(dirname "$target_dir")"
200-
GIT_TERMINAL_PROMPT=0 git clone --branch main --single-branch "$repo_url" "$target_dir"
232+
233+
if [[ -d "$target_dir/.git" ]]; then
234+
if git -C "$target_dir" remote get-url origin >/dev/null 2>&1; then
235+
git -C "$target_dir" remote set-url origin "$repo_url"
236+
else
237+
git -C "$target_dir" remote add origin "$repo_url"
238+
fi
239+
else
240+
mkdir -p "$target_dir"
241+
find "$target_dir" -mindepth 1 -maxdepth 1 -exec rm -rf {} + 2>/dev/null || true
242+
GIT_TERMINAL_PROMPT=0 git clone --branch main "$repo_url" "$target_dir"
243+
fi
244+
245+
git -C "$target_dir" config --unset-all remote.origin.fetch || true
246+
git -C "$target_dir" config --add remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
247+
GIT_TERMINAL_PROMPT=0 git -C "$target_dir" fetch --prune origin "+refs/heads/*:refs/remotes/origin/*"
248+
git -C "$target_dir" checkout -B main origin/main
249+
git -C "$target_dir" reset --hard origin/main
250+
git -C "$target_dir" clean -fdx
201251
}
202252
203253
echo "+ sync zsh-kit -> $zsh_dir (main)"
@@ -2123,9 +2173,9 @@ fn create_workspace_container(
21232173
.arg("-e")
21242174
.arg("CODEX_AUTH_FILE=/home/agent/.agents/auth.json")
21252175
.arg("-e")
2126-
.arg("ZSH_KIT_DIR=~/.config/zsh")
2176+
.arg("ZSH_KIT_DIR=/home/agent/.config/zsh")
21272177
.arg("-e")
2128-
.arg("AGENT_KIT_DIR=~/.agents")
2178+
.arg("AGENT_KIT_DIR=/home/agent/.agents")
21292179
.arg("-e")
21302180
.arg("ZDOTDIR=/home/agent/.config/zsh")
21312181
.arg("-v")

0 commit comments

Comments
 (0)