DevelopExecution & jobs
@holtkeeper/workspace
Local checkouts of remote repositories for analyzers and the task engine.
import { checkout } from '@holtkeeper/workspace'
const tree = await checkout({ cloneUrl: repository.cloneUrl, ref: 'v1.4.0', // default: the remote's default branch credentials: { username: 'oauth2', password: token }, cacheDir: '.holtkeeper/cache', runner: localRunner, signal,})// tree.dir, tree.sha, tree.refawait tree.cleanup()How it works
Section titled “How it works”- One bare mirror per repository lives in
cacheDir(<name>-<hash>.git, seemirrorName). The first call runsgit clone --mirror, later callsgit fetch --prune --tags, so only new objects cross the network. - Each call adds a detached git worktree of the mirror in a fresh temporary directory (
workDir, default the OS temp dir). Checkouts have full history, every branch and every tag, and are private to the caller: modifying one does not affect others.cleanupremoves the worktree and its directory and is safe to call twice. Every git command of the checkout runs withcore.hooksPath=/dev/nullandcore.fsmonitor=false(asGIT_CONFIG_*variables), so hooks or an fsmonitor command a repository script wrote into the shared mirror never run in Holtkeeper’s own git. - Calls for the same mirror are serialized by
<mirror>.lock(created withO_EXCL), in one process and across processes alike, socacheDircan be one volume shared by several hosts (NFS or another ReadWriteMany volume). Waiting for it honorssignaland logs oncewaiting for the mirror lock held by <host> pid <pid>. The holder refreshes the lock every 30 s for at most 60 minutes, then logs that it stopped; a lock not refreshed for 5 minutes belongs to a killed or hung holder and is taken over. “Not refreshed” is judged two ways: by the lock’s mtime, and by the waiter’s own clock (the lock did not change while it watched for 5 minutes). The second covers other hosts’ clocks and the garbage timestamps an exclusive create on NFS leaves when its process dies right after it, which can lie years in the future and would otherwise keep the lock forever. Holding it, git’s own leftover*.lockfiles at the top of the mirror and underrefs/are removed, andfetchruns garbage collection in the foreground (gc.autoDetach=false) so it finishes under the lock. - Worktrees are added locked (
--lock, reasonholtkeeper <host> <time>): on a shared mirrorgit worktree prunewould otherwise drop other hosts’ live worktrees, whose directories it cannot see.cleanupremoves its own with--force --force; locks of ours older than a day are lifted so prune drops worktrees of processes that died. - Nothing waits on the network forever. Clone, fetch and push are stopped after
remoteTimeoutMs(default 20 minutes), local git calls after 10 minutes;signalstops any of them. HTTP transfers slower than 1000 bytes/s for 120 s fail (http.lowSpeedLimit/http.lowSpeedTime, set asGIT_CONFIG_*variables), and SSH gives up on a host that does not connect within 30 s or misses four keepalives 15 s apart. An aborted call releases the mirror at once: the runner stops git together with its remote helpers (seelocalRunner). - Credentials never touch disk: they are passed only to the git processes that talk to the remote, through environment variables read by an inline credential helper (
-c credential.helper=...). They do not appear in argv, the mirror’s config or the remote URL. Git never prompts (GIT_TERMINAL_PROMPT=0).
SSH transport and signed commits
Section titled “SSH transport and signed commits”const tree = await checkout({ cloneUrl: repository.cloneUrl, // https://… becomes git@host:path.git ssh: { privateKey, knownHosts }, // from an `ssh` credential of @holtkeeper/ssh-keys signing: { privateKey, publicKey, name: 'Holtkeeper Bot', email: 'bot@example.com' }, cacheDir, runner,})await Bun.write(join(tree.dir, 'package.json'), updated)const commit = await tree.commit({ message: 'chore(deps): update x', paths: ['package.json'] })if (commit) await tree.push({ branch: 'holtkeeper/update/x' })- With
ssh, every remote git call (clone, fetch, push) runs withGIT_SSH_COMMAND='ssh -i <key> -o IdentitiesOnly=yes -o UserKnownHostsFile=<known_hosts> -o StrictHostKeyChecking=yes -o BatchMode=yes -o ConnectTimeout=30 -o ServerAliveInterval=15 -o ServerAliveCountMax=4 -F /dev/null'. The key andknown_hostsare written to a fresh private temp directory (0700, files0600, underworkDiror the OS temp dir) right before the call and removed right after it. Local git calls never see them, and nothing lands in git config.sshtakes precedence overcredentials. sshCloneUrl(url)turnshttps://host/path(.git)intogit@host:path.gitand keeps SSH URLs as they are. Ports and path prefixes of the HTTPS URL are dropped; pass the SSH URL directly when the forge’s SSH endpoint differs. HTTPS and SSH checkouts of the same repository use separate mirrors.commit({ message, paths? })stages the given paths (all changes when omitted; deletions included) and commits on the detachedHEAD; it returnsundefinedwhen nothing is staged. Withsigningit runsgit -c gpg.format=ssh -c user.signingkey=<temp key> -c commit.gpgsign=true(the key file again lives only for that call) and sets author and committer tosigning.name/signing.email; otherwiseauthoror git’s own config is used.checkoutrejects a signing key whose halves do not match.push({ branch })pushesHEADtorefs/heads/<branch>with--force-with-leaseagainst the commit the mirror last saw there, so it replaces what an earlier run pushed but never a branch someone else moved since the fetch. It skips the push (pushed: false) when the branch already holds the same tree.- These paths are local: with a runner that executes elsewhere (a container), the temp directory must be visible there too, e.g. by putting
workDiron a shared mount.
checkout(options): Promise<GitCheckout>—Checkout = { dir, sha, ref, cleanup() }, pluscommitandpushonGitCheckout; rejects on unknown refs or git failures without leaving a directory behind. Options:cloneUrl,ref?,credentials?,ssh?: { privateKey, knownHosts, user? },signing?: { privateKey, publicKey, name, email },author?,cacheDir,runner,signal?,workDir?,remoteTimeoutMs?,log?.sshCloneUrl(cloneUrl, user?),gitSshCommand(keyFile, knownHostsFile)— the URL andGIT_SSH_COMMANDused for SSH.mirrorName(cloneUrl)— stable, readable cache directory name; credentials in the URL do not change it.GitCredentials—{ username, password }, whatForgeDefinition.gitCredentials?()returns.listMirrors(cacheDir)—{ name, bytes, lastUsedAt }[], largest first;lastUsedAtis the mirror’sFETCH_HEAD(orHEAD) mtime.pruneMirrors(cacheDir, { unusedForDays })— deletes mirrors not fetched for that many days (0: all); do not run it while jobs use the cache.mirrorStorage(cacheDir)— the cache as aStorageProvider(@holtkeeper/artifactsshape) for the server’s storage overview;clear(days?)prunes.directorySize(path)— bytes of a directory tree.
All git calls go through a CommandRunner; test/checkout.test.ts runs real git against file:// repositories in temp directories (and against a server that accepts connections and never answers, to check that an aborted fetch frees the mirror), and test/ssh.test.ts clones and pushes through a fake ssh on PATH that checks the arguments and key file modes and serves a local repository with git-upload-pack / git-receive-pack, then verifies the commit signature with ssh-keygen -Y verify.