Skip to content
Myr

git-client.ts

This file

Path

packages/testkit/src/git-client.ts

82 lines · 2.3 KiB · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–82why, one step back
1import { CommitSha } from "@repo/protocol/ids";
2import { Effect } from "effect";3import { init, writeBlob, writeCommit, writeTree } from "isomorphic-git";4import type { FsClient, TreeEntry } from "isomorphic-git";56export type Files = readonly (readonly [path: string, content: string])[];78// Fixed, so the same files and parents always make the same commit id.9const signature = {10 email: "answerer@example.com",11 name: "Answerer",12 timestamp: 1_791_300_000,13 timezoneOffset: 0,14};1516export const gitClient = (fs: FsClient) =>17 Effect.gen(function* () {18 const source = { fs, gitdir: "/client.git" };1920 yield* Effect.promise(() =>21 init({ ...source, bare: true, defaultBranch: "main" })22 );2324 const tree = (files: Files): Effect.Effect<string> =>25 Effect.gen(function* () {26 const entries: TreeEntry[] = [];27 const nested = new Map<string, [string, string][]>();2829 for (const [path, content] of files) {30 const [head = path, ...rest] = path.split("/");3132 if (rest.length === 0) {33 const oid = yield* Effect.promise(() =>34 writeBlob({ ...source, blob: new TextEncoder().encode(content) })35 );3637 entries.push({ mode: "100644", oid, path: head, type: "blob" });38 } else {39 nested.set(head, [40 ...(nested.get(head) ?? []),41 [rest.join("/"), content],42 ]);43 }44 }4546 for (const [path, inner] of nested) {47 const oid = yield* tree(inner);4849 entries.push({ mode: "040000", oid, path, type: "tree" });50 }5152 return yield* Effect.promise(() =>53 writeTree({ ...source, tree: entries })54 );55 });5657 const commit = (58 message: string,59 files: Files,60 parents: readonly CommitSha[] = []61 ) =>62 Effect.gen(function* () {63 const root = yield* tree(files);6465 const oid = yield* Effect.promise(() =>66 writeCommit({67 ...source,68 commit: {69 author: signature,70 committer: signature,71 message: `${message}\n`,72 parent: [...parents],73 tree: root,74 },75 })76 );7778 return CommitSha.make(oid);79 });8081 return { commit, source };82 });