| 1 | import { PermanentImportError } from "./errors.ts"; |
| 2 | |
| 3 | const GITHUB = " |
| 4 | |
| 5 | // What an import carries over. `refs/pull/*/head` keeps every pull request's commits, including ones whose branch is gone or lived in a fork. |
| 6 | const REFSPECS = [ |
| 7 | |
| 8 | /** Credentials go in through the environment rather than argv, where any local user could read them from `ps`, and are scoped to one origin so a redirect cannot carry them elsewhere. */ |
| 9 | export function credentialEnv( |
| 10 | origin: string, |
| 11 | username: string, |
| 12 | password: string, |
| 13 | ) { |
| 14 | return { |
| 15 | GIT_TERMINAL_PROMPT: "0", |
| 16 | GIT_CONFIG_COUNT: "1", |
| 17 | GIT_CONFIG_KEY_0: `http.${origin |
| 18 | GIT_CONFIG_VALUE_0: `Authorization: Basic ${Buffer |
| 19 | }; |
| 20 | } |
| 21 | |
| 22 | async function git( |
| 23 | args: string[] |
| 24 | { |
| 25 | env = {}, |
| 26 | signal, |
| 27 | stdin, |
| 28 | }: { env |
| 29 | ) { |
| 30 | const child = |
| 31 | env: { ...process |
| 32 | stdin: stdin === |
| 33 | stdout: "pipe", |
| 34 | stderr: "pipe", |
| 35 | signal, |
| 36 | }); |
| 37 | const [stdout, |
| 38 | new Response(child |
| 39 | new Response(child |
| 40 | child.exited, |
| 41 | ]); |
| 42 | if (code !== |
| 43 | throw new Error( |
| 44 | `git ${args.join |
| 45 | ); |
| 46 | return stdout; |
| 47 | } |
| 48 | |
| 49 | export async function fetchFromGitHub |
| 50 | dir, |
| 51 | source, |
| 52 | token, |
| 53 | signal, |
| 54 | }: { |
| 55 | dir: string |
| 56 | source: string |
| 57 | token: string |
| 58 | signal: AbortSignal |
| 59 | }) { |
| 60 | await git(["init |
| 61 | await git( |
| 62 | [ |
| 63 | "-C", |
| 64 | dir, |
| 65 | "fetch", |
| 66 | "--quiet", |
| 67 | "--no-tags", |
| 68 | `${GITHUB}/${source |
| 69 | ...REFSPECS.map( |
| 70 | ], |
| 71 | { env: credentialEnv( |
| 72 | ).catch((error |
| 73 | if (/Repository not found|Authentication failed|403 |
| 74 | throw new PermanentImportError( |
| 75 | throw error; |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | export async function listRefs |
| 80 | dir: string, |
| 81 | signal: AbortSignal |
| 82 | ): Promise<Set |
| 83 | const output = |
| 84 | signal, |
| 85 | }); |
| 86 | return new Set |
| 87 | } |
| 88 | |
| 89 | /** Which of `shas` the fetched history holds. GitHub reports merge commits that a later force-push left unreachable. */ |
| 90 | export async function presentCommits |
| 91 | dir: string, |
| 92 | shas: string[] |
| 93 | signal: AbortSignal |
| 94 | ): Promise<Set |
| 95 | if (!shas.length |
| 96 | const output = |
| 97 | ["-C", dir |
| 98 | { signal, stdin: |
| 99 | ); |
| 100 | return new Set |
| 101 | output |
| 102 | .split("\n") |
| 103 | .map((line) |
| 104 | .filter(([, type |
| 105 | .map(([sha |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | /** Not forced: refs a previous attempt already pushed are up to date, and anything else on the other side is a conflict worth failing on. */ |
| 110 | export async function pushToGhost |
| 111 | dir, |
| 112 | apiUrl, |
| 113 | destination, |
| 114 | token, |
| 115 | refs, |
| 116 | signal, |
| 117 | }: { |
| 118 | dir: string |
| 119 | apiUrl: string |
| 120 | destination: string |
| 121 | token: string |
| 122 | refs: Set |
| 123 | signal: AbortSignal |
| 124 | }) { |
| 125 | const specs = |
| 126 | [...refs].some |
| 127 | ); |
| 128 | if (!specs.length |
| 129 | const origin = |
| 130 | await git( |
| 131 | [ |
| 132 | "-C", |
| 133 | dir, |
| 134 | "push", |
| 135 | "--quiet", |
| 136 | `${origin}/${destination |
| 137 | ...specs.map( |
| 138 | ], |
| 139 | { |
| 140 | env: credentialEnv(origin |
| 141 | signal, |
| 142 | }, |
| 143 | ); |
| 144 | } |