import { env, waitUntil } from "cloudflare:workers";
import { mergeRootTrees } from "./git/merge";
import type { MergeConflict, TreeMergeResult } from "./git/merge";
import { hasConflictMarkers, MergeTooComplexError } from "./git/merge3";
import { makeCommit, signatureNow } from "./git/objects";
import type { Signature } from "./git/objects";
import { pushToArtifacts, PushRejectedError } from "./git/writer";
import { compareCommits } from "./git/history";
import { branchSha, listRefs } from "./git/refs";
import { withStore, ZERO_SHA } from "./git/store";
import type { GitStore } from "./git/store";
import { bridgePullBase, bridgePullHead } from "./git/pull-bridge";
import { authenticatedRemote, mintRepoToken, revokeRepoTokens } from "./git/artifacts";
import { triggerPush } from "./events/push-trigger";
import { hasPermission, loadRepoById, repoPermission, repoReadOnlyReason } from "./repos";
import { directUpdateViolation, loadProtections, protectionFor } from "./protection";
import { forbidden } from "./errors";
import { MergeError } from "./pulls";
import type { PullRow } from "./pulls";
import type { RepoRow, Viewer } from "~/lib/types";
const UPDATE_TOKEN_TTL_SECONDS = 1800;
const EXIT_CONFLICT = 2;
const EXIT_HEAD_MOVED = 3;
const MAX_RESOLUTION_FILES = 50;
export type PullRepos = { baseRepo: RepoRow; headRepo: RepoRow };
export async function loadPullRepos(pull: PullRow): Promise<PullRepos> {
const [baseRepo, headRepo] = await Promise.all([loadRepoById(pull.repo_id), loadRepoById(pull.head_repo_id)]);
if (!baseRepo || !headRepo) throw new MergeError("Pull request repositories are missing");
return { baseRepo, headRepo };
}
export async function currentBaseSha(repo: RepoRow, branch: string): Promise<string> {
const sha = branchSha(await listRefs(repo), branch);
if (!sha) throw new MergeError("The base branch no longer exists");
return sha;
}