Skip to content

status

Check the status of files in the working tree.

await git.status(filepath: string): Promise<string>
await git.statusMatrix(): Promise<StatusRow[]>
import { status, statusMatrix, classifyStatusRow } from '@keydown-app/ts-git';
await status({
fs,
dir,
filepath
}): Promise<string>
ParameterTypeDefaultDescription
fsFSAdapterrequiredFilesystem adapter
dirstringrequiredWorking directory path
gitdirstringjoin(dir, '.git')Git directory path
filepathstringrequiredFile path to check

Returns one of the following status strings:

StatusDescription
'unmodified'File is unchanged
'modified'File has been modified
'*modified'File has unstaged modifications
'added'File is staged for addition
'*added'File is untracked (not staged)
'deleted'File is staged for deletion
'*deleted'File has been deleted (not staged)
'untracked'File is not tracked by Git

Returns a matrix of status information for all files:

await statusMatrix({
fs,
dir
}): Promise<StatusRow[]>
type StatusRow = [
filepath: string,
headStatus: 0 | 1,
workdirStatus: 0 | 1 | 2,
stageStatus: 0 | 1 | 2 | 3,
];

Status codes:

  • 0 = absent
  • 1 = present (same as HEAD)
  • 2 = modified
  • 3 = added
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();
// Check status of untracked file
await fs.writeFile('/my-repo/README.md', '# Hello', 'utf8');
const status1 = await git.status('README.md');
console.log(status1); // 'untracked'
// After staging
await git.add('README.md');
const status2 = await git.status('README.md');
console.log(status2); // 'added'
// After committing
await git.commit('Initial commit', {
name: 'John Doe',
email: 'john@example.com',
});
const status3 = await git.status('README.md');
console.log(status3); // 'unmodified'
// Modify the file
await fs.writeFile('/my-repo/README.md', '# Hello World!', 'utf8');
const status4 = await git.status('README.md');
console.log(status4); // '*modified'
// Get full status matrix
const matrix = await git.statusMatrix();
console.log(matrix);
// [['README.md', 1, 2, 1], ...]
  • add - Stage files
  • diff - Show differences