Reading and Writing Files
The filesystem API (Level 3) is the primary interface for agents and apps. Read, write, and delete files with simple method calls.
Quickstart
Section titled “Quickstart”import { Client } from "@githosted/sdk";
const client = new Client();const repo = client.repo("my-project");
// List filesconst files = await repo.ls("src/");
// Read a fileconst file = await repo.read("src/main.ts");console.log(file.content); // string contentconsole.log(file.headSha); // branch tip at read time
// Write a fileawait repo.write("src/main.ts", "const x = 1;", { message: "Update main.ts",});API Reference
Section titled “API Reference”repo.ls(path?, options?)
Section titled “repo.ls(path?, options?)”List files and directories at a path.
const entries = await repo.ls("src/");// [{ name: "main.ts", type: "file" }, { name: "lib", type: "directory" }]
// List rootconst root = await repo.ls();
// List at a specific branchconst entries = await repo.ls("src/", { ref: "feature/new-api" });repo.read(path, options?)
Section titled “repo.read(path, options?)”Read a file’s content and metadata.
const file = await repo.read("src/main.ts");
file.content; // string (UTF-8 decoded)file.rawContent; // Uint8Array (raw bytes)file.headSha; // branch tip SHA at read timefile.blobSha; // content-addressable blob hash
// Read from a specific branchconst file = await repo.read("src/main.ts", { ref: "develop" });headSha is the key to optimistic concurrency — pass it as expectedHead on a subsequent write to detect conflicts.
repo.write(path, content, options)
Section titled “repo.write(path, content, options)”Write a file, creating a commit on the target branch.
// Simple writeawait repo.write("README.md", "# My Project", { message: "Update readme",});
// Write to a specific branchawait repo.write("src/main.ts", code, { message: "Fix bug", ref: "feature/fix",});
// Write with optimistic concurrencyconst file = await repo.read("src/main.ts");await repo.write("src/main.ts", modifiedCode, { message: "Fix bug", expectedHead: file.headSha,});Content can be a string or Uint8Array (for binary files).
repo.delete(path, options)
Section titled “repo.delete(path, options)”Delete a file, creating a commit on the target branch.
await repo.delete("src/old_module.ts", { message: "Remove deprecated module",});Optimistic Concurrency
Section titled “Optimistic Concurrency”Use expectedHead to detect when another writer has modified the branch between your read and write:
const file = await repo.read("config.json");const config = JSON.parse(file.content);config.version = "2.0";
try { await repo.write("config.json", JSON.stringify(config, null, 2), { message: "Bump version", expectedHead: file.headSha, });} catch (err) { if (err instanceof StaleHeadError) { console.log(`Branch moved to ${err.actualHead}, re-read and retry`); }}See Error Handling for more on StaleHeadError.