Skip to content
File

Blob: src/worker/dispatch/shared/run-environment/git/branch-checkout.ts

typescript43 lines
1import type { ExecutionSession } from "@cloudflare/sandbox";
2 
3import {
4 addOriginRemote,
5 checkoutFetchedBranch,
6 fetchBranch,
7 initializeRepository,
8 resolveShallowRepositoryState,
9 setFetchedBranchUpstream,
10} from "./commands";
11 
12export const ensureBranchCheckout = async (
13 session: ExecutionSession,
14 repoRoot: string,
15 repoUrl: string,
16 branch: string,
17 hasAuthHeader: boolean,
18): Promise<void> => {
19 await initializeRepository(session, repoRoot);
20 await addOriginRemote(session, repoRoot, repoUrl);
21 await fetchBranch(session, repoRoot, branch, hasAuthHeader, 1, `Failed to fetch branch ${branch} at depth 1.`);
22 await checkoutFetchedBranch(session, repoRoot, branch);
23 await setFetchedBranchUpstream(session, repoRoot, branch);
24};
25 
26export const ensureBranchCheckoutDepth = async (
27 session: ExecutionSession,
28 repoRoot: string,
29 branch: string,
30 depth: number | undefined,
31 hasAuthHeader: boolean,
32): Promise<void> => {
33 if (!depth || depth <= 1) {
34 return;
35 }
36 
37 if (!(await resolveShallowRepositoryState(session, repoRoot))) {
38 return;
39 }
40 
41 await fetchBranch(session, repoRoot, branch, hasAuthHeader, depth, `Failed to deepen checkout to depth ${depth}.`);
42};