log
View the commit history.
GitClient Method
Section titled “GitClient Method”await git.log(depth?: number): Promise<LogEntry[]>Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
depth | number | (all commits) | Maximum number of commits to return |
Returns
Section titled “Returns”Returns an array of LogEntry objects:
interface LogEntry { oid: string; commit: { message: string; tree: string; parent: string[]; author: { name: string; email: string; timestamp: number; timezoneOffset: number; }; committer: { name: string; email: string; timestamp: number; timezoneOffset: number; }; gpgsig?: string; }; payload: string;}Low-level Function
Section titled “Low-level Function”import { log, readCommit } from '@keydown-app/ts-git';
await log({ fs, dir, depth?}): Promise<LogEntry[]>Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
fs | FSAdapter | required | Filesystem adapter |
dir | string | required | Working directory path |
gitdir | string | join(dir, '.git') | Git directory path |
depth | number | (all commits) | Maximum commits to return |
Example
Section titled “Example”import { GitClient, MemoryFSAdapter } from '@keydown-app/ts-git';
const fs = new MemoryFSAdapter();const git = new GitClient({ fs, dir: '/my-repo', defaultBranch: 'main' });
await git.init();
// Create some commitsawait fs.writeFile('/my-repo/file1.txt', 'content1', 'utf8');await git.add('file1.txt');await git.commit('First commit', { name: 'John Doe', email: 'john@example.com',});
await fs.writeFile('/my-repo/file2.txt', 'content2', 'utf8');await git.add('file2.txt');await git.commit('Second commit', { name: 'John Doe', email: 'john@example.com',});
// Get all commitsconst commits = await git.log();
console.log(commits.length); // 2console.log(commits[0].commit.message); // 'Second commit'console.log(commits[1].commit.message); // 'First commit'
// Get limited historyconst recent = await git.log(1);console.log(recent.length); // 1readCommit
Section titled “readCommit”Read a single commit by its OID:
import { readCommit } from '@keydown-app/ts-git';
const commit = await readCommit({ fs, dir, oid: 'abc123...' });See Also
Section titled “See Also”- commit - Create a commit