Skip to main content

Repository

Files​

When you have a repository from a client, you can read and write to the files within it. The available methods include:

createFile()​

Creates a new commit that creates a new file in the repository.

await repository.createFile("path/to/file.txt", "file content");
caution

Due to API limitations, the GitHub implementation treats creating and updating a file as the same. Creating a file that already exists will overwrite it.

Throws:​

  • CommitError: If there's an unknown error while committing.

readFile()​

Reads a files content from the remote repository.

const fileContent = await repository.readFile("path/to/file.txt");

Throws:​

  • FileNotFoundError: If the file does not exist.

updateFile()​

Creates a new commit that replaces the content of a file in the repository.

await repository.updateFile("path/to/file.txt", "file content");
tip

The updateFile method requires you to pass in the whole file content, just like a traditional file-system API. You cannot pass in a diff/patch/delta like you would with a Git API.

caution

Due to API limitations, the GitHub implementation treats creating and updating a file as the same. Updating a file that does not exist will create it.

Throws:​

  • CommitError: If there's an unknown error while committing.

deleteFile()​

Creates a new commit that deletes a file from the repository.

await repository.deleteFile("path/to/file.txt");

Throws:​

  • CommitError: If there's an unknown error while committing.

JSON Helper Methods​

The create, read, and update methods also come with JSON helper-method equivalents:

await repository.createJsonFile("path/to/file.json", anyObject);
const anyObject = await repository.readJsonFile<ObjectType>("path/to/file.json");
await repository.updateJsonFile("path/to/file.json", anyObject);

Tags​

The repository class also exposes methods for handling tags. You can create tags at the current HEAD of the repository's branch and then read files back from that tag at a later point in time.

await repository.createFile("path/to/file.txt", "original content");

await repository.createTag("myTag");

await repository.updateFile("path/to/file.txt", "new content");

await repository.readFile("path/to/file.txt"); // returns "new content"
await repository.readFile("path/to/file.txt", "myTag"); // returns "original content"

createCommitBuilder()​

Sometimes you might want to apply changes to multiple files using a single commit. To achive this, call the createCommitBuilder() method to get a new CommitBuilder. See the CommitBuilder page for details on how to use it.

const commitBuilder = repository.createCommitBuilder();