File
Blob: src/worker/dispatch/shared/run-environment/git/pinned-checkout.ts
| 1 | import type { ExecutionSession } from "@cloudflare/sandbox"; |
| 2 | |
| 3 | import type { CommitSha as CommitShaType } from "@/contracts"; |
| 4 | |
| 5 | import { |
| 6 | checkoutPinnedCommit, |
| 7 | fetchPinnedCommitBySha, |
| 8 | hasCommitObject, |
| 9 | resolveCheckedOutCommitSha, |
| 10 | resolveShallowRepositoryState, |
| 11 | unshallowRepository, |
| 12 | } from "./commands"; |
| 13 | |
| 14 | const PINNED_COMMIT_SHALLOW_FETCH_DEPTHS = [2, 4, 8, 16, 32, 64] as const; |
| 15 | |
| 16 | export const ensurePinnedCommitCheckout = async ( |
| 17 | session: ExecutionSession, |
| 18 | repoRoot: string, |
| 19 | commitSha: CommitShaType, |
| 20 | hasAuthHeader: boolean, |
| 21 | ): Promise<void> => { |
| 22 | let commitAvailable = await hasCommitObject(session, repoRoot, commitSha); |
| 23 | let repositoryIsShallow = true; |
| 24 | |
| 25 | if (!commitAvailable) { |
| 26 | await fetchPinnedCommitBySha( |
| 27 | session, |
| 28 | repoRoot, |
| 29 | commitSha, |
| 30 | hasAuthHeader, |
| 31 | 1, |
| 32 | `Failed to fetch pinned commit ${commitSha} at depth 1.`, |
| 33 | ); |
| 34 | commitAvailable = await hasCommitObject(session, repoRoot, commitSha); |
| 35 | if (!commitAvailable) { |
| 36 | repositoryIsShallow = await resolveShallowRepositoryState(session, repoRoot); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | for (const depth of PINNED_COMMIT_SHALLOW_FETCH_DEPTHS) { |
| 41 | if (commitAvailable || !repositoryIsShallow) { |
| 42 | break; |
| 43 | } |
| 44 | |
| 45 | await fetchPinnedCommitBySha( |
| 46 | session, |
| 47 | repoRoot, |
| 48 | commitSha, |
| 49 | hasAuthHeader, |
| 50 | depth, |
| 51 | `Failed to deepen checkout to depth ${depth} for pinned commit ${commitSha}.`, |
| 52 | ); |
| 53 | commitAvailable = await hasCommitObject(session, repoRoot, commitSha); |
| 54 | if (!commitAvailable) { |
| 55 | repositoryIsShallow = await resolveShallowRepositoryState(session, repoRoot); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (!commitAvailable) { |
| 60 | if (repositoryIsShallow) { |
| 61 | await fetchPinnedCommitBySha(session, repoRoot, commitSha, hasAuthHeader); |
| 62 | commitAvailable = await hasCommitObject(session, repoRoot, commitSha); |
| 63 | |
| 64 | if (!commitAvailable) { |
| 65 | repositoryIsShallow = await resolveShallowRepositoryState(session, repoRoot); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (!commitAvailable && repositoryIsShallow) { |
| 70 | await unshallowRepository(session, repoRoot, hasAuthHeader); |
| 71 | commitAvailable = await hasCommitObject(session, repoRoot, commitSha); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (!commitAvailable) { |
| 76 | throw new Error(`Pinned commit ${commitSha} is not available after fetch.`); |
| 77 | } |
| 78 | |
| 79 | await checkoutPinnedCommit(session, repoRoot, commitSha); |
| 80 | }; |
| 81 | |
| 82 | export const ensurePinnedCheckoutDepth = async ( |
| 83 | session: ExecutionSession, |
| 84 | repoRoot: string, |
| 85 | commitSha: CommitShaType, |
| 86 | depth: number | undefined, |
| 87 | hasAuthHeader: boolean, |
| 88 | ): Promise<void> => { |
| 89 | if (!depth || depth <= 1) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | if (!(await resolveShallowRepositoryState(session, repoRoot))) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | await fetchPinnedCommitBySha( |
| 98 | session, |
| 99 | repoRoot, |
| 100 | commitSha, |
| 101 | hasAuthHeader, |
| 102 | depth, |
| 103 | `Failed to deepen pinned checkout to depth ${depth}.`, |
| 104 | ); |
| 105 | |
| 106 | const checkedOutCommitSha = await resolveCheckedOutCommitSha(session, repoRoot); |
| 107 | if (checkedOutCommitSha !== commitSha) { |
| 108 | throw new Error(`Pinned checkout drifted from ${commitSha} to ${checkedOutCommitSha} after depth expansion.`); |
| 109 | } |
| 110 | }; |