Skip to main content
The Repository class is the main entry point for all repository operations.

Import

import { Repository } from 'wit';

Static Methods

Repository.init()

Create a new repository.
static init(path?: string): Repository
Parameters:
NameTypeDescription
pathstringDirectory to initialize (default: current directory)
Returns: Repository instance Example:
// Initialize in current directory
const repo = Repository.init();

// Initialize in specific path
const repo = Repository.init('/path/to/project');

Repository.open()

Open an existing repository.
static open(path?: string): Repository
Parameters:
NameTypeDescription
pathstringRepository path (default: current directory)
Returns: Repository instance Throws: NotFoundError if no repository found Example:
const repo = Repository.open('/path/to/repo');

Repository.isRepository()

Check if a directory is a wit repository.
static isRepository(path: string): boolean
Example:
if (Repository.isRepository('/some/path')) {
  const repo = Repository.open('/some/path');
}

Instance Methods

Staging

add()

Stage files for commit.
add(path: string | string[]): void
Parameters:
NameTypeDescription
pathstring | string[]File(s) to stage
Example:
repo.add('file.ts');
repo.add(['file1.ts', 'file2.ts']);
repo.add('.');  // Stage all

unstage()

Remove files from staging area.
unstage(path: string | string[]): void
Example:
repo.unstage('file.ts');

Committing

commit()

Create a new commit.
commit(message: string, options?: CommitOptions): string
Parameters:
NameTypeDescription
messagestringCommit message
optionsCommitOptionsOptional settings
Options:
interface CommitOptions {
  all?: boolean;      // Stage all tracked files
  amend?: boolean;    // Amend last commit
  author?: string;    // Override author
  email?: string;     // Override email
}
Returns: Commit hash Example:
const hash = repo.commit('Add feature');

// Stage all and commit
const hash = repo.commit('Update', { all: true });

// Amend last commit
repo.commit('Fixed message', { amend: true });

Status

status()

Get repository status.
status(): Status
Returns:
interface Status {
  branch: string;
  staged: FileStatus[];
  modified: FileStatus[];
  untracked: string[];
  ahead: number;
  behind: number;
}
Example:
const status = repo.status();

console.log('On branch:', status.branch);
console.log('Staged files:', status.staged.length);
console.log('Modified files:', status.modified.length);
console.log('Untracked:', status.untracked);

History

log()

Get commit history.
log(options?: LogOptions): Commit[]
Options:
interface LogOptions {
  limit?: number;     // Max commits to return
  since?: Date;       // Commits after this date
  until?: Date;       // Commits before this date
  author?: string;    // Filter by author
  path?: string;      // Filter by path
}
Example:
// All commits
const commits = repo.log();

// Last 10 commits
const recent = repo.log({ limit: 10 });

// Commits affecting a file
const fileHistory = repo.log({ path: 'src/index.ts' });

getCommit()

Get a specific commit.
getCommit(ref: string): Commit
Parameters:
NameTypeDescription
refstringCommit hash or reference
Example:
const commit = repo.getCommit('abc123');
const head = repo.getCommit('HEAD');
const parent = repo.getCommit('HEAD~1');

Diffing

diff()

Get differences.
diff(options?: DiffOptions): DiffResult
Options:
interface DiffOptions {
  staged?: boolean;   // Diff staged vs HEAD
  from?: string;      // Start commit/ref
  to?: string;        // End commit/ref
  path?: string;      // Limit to path
}
Example:
// Unstaged changes
const unstaged = repo.diff();

// Staged changes
const staged = repo.diff({ staged: true });

// Between commits
const changes = repo.diff({ from: 'abc', to: 'def' });

Branches

branches()

List all branches.
branches(): Branch[]
Returns:
interface Branch {
  name: string;
  hash: string;
  current: boolean;
  upstream?: string;
}
Example:
const branches = repo.branches();
for (const branch of branches) {
  console.log(branch.current ? '* ' : '  ', branch.name);
}

currentBranch()

Get current branch name.
currentBranch(): string | null
Returns null if in detached HEAD state.

createBranch()

Create a new branch.
createBranch(name: string, startPoint?: string): void
Example:
repo.createBranch('feature');
repo.createBranch('from-tag', 'v1.0.0');

deleteBranch()

Delete a branch.
deleteBranch(name: string, force?: boolean): void

switch()

Switch to a branch.
switch(branch: string, options?: SwitchOptions): void
Options:
interface SwitchOptions {
  create?: boolean;   // Create branch if doesn't exist
}
Example:
repo.switch('main');
repo.switch('new-feature', { create: true });

Merging

merge()

Merge a branch.
merge(branch: string, options?: MergeOptions): MergeResult
Returns:
interface MergeResult {
  success: boolean;
  hash?: string;           // Merge commit hash
  conflicts?: string[];    // Conflicting files
}
Example:
const result = repo.merge('feature');
if (!result.success) {
  console.log('Conflicts in:', result.conflicts);
}

Undo

undo()

Undo operations.
undo(options?: UndoOptions): void
Options:
interface UndoOptions {
  steps?: number;    // Number of operations to undo
}
Example:
repo.undo();
repo.undo({ steps: 3 });

restore()

Restore file from index or commit.
restore(path: string, options?: RestoreOptions): void
Options:
interface RestoreOptions {
  staged?: boolean;    // Unstage file
  source?: string;     // Restore from commit
}

Low-Level Access

getTree()

Get a tree object.
getTree(hash: string): Tree

getBlob()

Get a blob (file content).
getBlob(hash: string): Blob

getFileContent()

Get file content from the working tree or index.
getFileContent(path: string, options?: { staged?: boolean }): string

Properties

PropertyTypeDescription
pathstringRepository root path
witDirstring.wit directory path
workingDirstringWorking directory path
configConfigRepository configuration
journalJournalOperation journal (for undo)

Example: Full Workflow

import { Repository } from 'wit';

// Open or create repo
const repo = Repository.isRepository('.')
  ? Repository.open('.')
  : Repository.init('.');

// Make changes and stage
repo.add('src/feature.ts');
repo.add('tests/feature.test.ts');

// Check what's staged
const status = repo.status();
console.log('Staged:', status.staged.map(f => f.path));

// Review diff
const diff = repo.diff({ staged: true });
for (const file of diff.files) {
  console.log(`\n${file.path}:`);
  for (const hunk of file.hunks) {
    console.log(hunk.header);
  }
}

// Commit
const hash = repo.commit('Add new feature');
console.log('Created commit:', hash);

// View history
const commits = repo.log({ limit: 5 });
for (const commit of commits) {
  console.log(`${commit.hash.slice(0, 7)} ${commit.message}`);
}