@discoveryjs/scan-git
Usage
npm install @discoveryjs/scan-git
API
import { createGitReader } from '@discoveryjs/scan-git';
const repo = await createGitReader('path/to/.git');
const commits = await repo.log({ ref: 'my-branch', depth: 10 });
console.log(commits);
await repo.dispose();
createGitReader(gitdir, options?)
gitdir
: string - path to the git repooptions
– optional settings:
maxConcurrency
– limit the number of file system operations (default: 50)cruftPacks
– defines how cruft packs are processed:
'include'
or true
(default) - process all packs'exclude'
or false
- exclude cruft packs from processing'only'
- process cruft packs only
Refs
Common parameters:
ref
: string – a reference to an object in repositorywithOid
: boolean – a flag to include resolved oid for a reference
repo.defaultBranch()
Returns default branch name used in a repo:
const defaultBranch = await repo.defaultBranch();
The algorithm to identify a default branch name:
- if there is only one branch, that must be the default
- otherwise looking for specific branch names, in this order:
upstream/HEAD
origin/HEAD
main
master
repo.currentBranch()
Returns the current branch name along with its commit oid.
If the repository is in a detached HEAD state, name
will be null
.
const currentBranch = repo.currentBranch();
const detachedHead = repo.currentBranch();
repo.isRefExists(ref)
Checks if a ref
exists.
const isValidRef = repo.isRefExists('main');
repo.expandRef(ref)
Expands a ref
into a full form, e.g. 'main'
-> 'refs/heads/main'
.
Returns null
if ref
doesn't exist. For the symbolic ref names ('HEAD'
, 'FETCH_HEAD'
, 'CHERRY_PICK_HEAD'
, 'MERGE_HEAD'
and 'ORIG_HEAD'
) returns a name without changes.
const fullPath = repo.expandRef('heads/main');
repo.resolveRef(ref)
Resolves ref
into oid if it exists, otherwise throws an exception.
In case if ref
is oid, returns this oid back. If ref is not a full path, expands it first.
const oid = repo.resolveRef('main');
repo.describeRef(ref)
Returns an info object for provided ref
.
const info = repo.describeRef('HEAD');
const info = repo.describeRef('main');
const info = repo.describeRef('origin/HEAD');
repo.isOid(value)
Checks if a value
is a valid oid.
repo.isOid('7b84f676f2fbea2a3c6d83924fa63059c7bdfbe2');
repo.isOid('main');
repo.listRemotes()
const remotes = repo.listRemotes();
repo.listRemoteBranches(remote, withOid?)
Get a list of branches for a remote.
const originBranches = await repo.listRemoteBranches('origin');
const originBranches = await repo.listRemoteBranches('origin', true);
repo.listBranches(withOid?)
Get a list of local branches.
const localBranches = await repo.listBranches();
const localBranches = await repo.listBranches(true);
repo.listTags(withOid?)
Get a list of tags.
const tags = await repo.listTags();
const tags = await repo.listTags(true);
File lists
repo.treeOidFromRef(ref)
Resolve a tree oid by a commit reference.
ref
: string (default: 'HEAD'
) – commit reference
const treeOid = await repo.treeOidFromRef('HEAD');
repo.listFiles(ref, filesWithHash)
List all files in the repository at the specified commit reference.
ref
: string (default: 'HEAD'
) – commit referencefilesWithHash
: boolean (default: false
) – specify to return blob's hashes
const headFiles = repo.listFiles();
const headFilesWithHashes = repo.listFiles('HEAD', true);
repo.getPathEntry(path, ref)
Retrieve a tree entry (file or directory) by its path at the specified commit reference.
path
: string - the path to the file or directoryref
: string (default: 'HEAD'
) - commit reference
const entry = await repo.getPathEntry('path/to/file.txt');
repo.getPathsEntries(paths, ref)
Retrieve a list of tree entries (files or directories) by their paths at the specified commit reference.
paths
: string[] - an array of paths to files or directoriesref
: string (default: 'HEAD'
) - commit reference
const entries = await repo.getPathsEntries([
'path/to/file1.txt',
'path/to/dir1',
'path/to/file2.txt'
]);
repo.deltaFiles(nextRef, prevRef)
Compute the file delta (changes) between two commit references, including added, modified, and removed files.
nextRef
: string (default: 'HEAD'
) - commit reference for the "next" stateprevRef
: string (optional) - commit reference for the "previous" state
const fileDelta = await repo.deltaFiles('HEAD', 'branch-name');
Commits
repo.readCommit(ref)
repo.log(options)
Return a list of commits in topological order.
Options:
ref
– oid, hash, refdepth
(default 50
) – limits commits count
const commits = await repo.log({ ref: 'my-branch', depth: 10 });
Note: Pass Infinity
as depth
value to load all the commits that are reachable from ref
at once.
Statistics & info
repo.readObjectByHash(hash, cache?)
repo.readObjectByOid(oid, cache?)
repo.stat()
Returns statistics for a repo:
const stats = await repo.stat();
Utils
parseContributor()
parseTimezone()
parseAnnotatedTag()
parseCommit()
parseTree()
diffTrees()
Compare
scan-git | isomorphic-git | Feature |
---|
✅ | ✅ | loose refs |
✅ | ✅ | packed refs |
🚫 | ✅ | index file Boosts fetching a file list for HEAD |
✅ | ✅ | loose objects |
✅ | ✅ | packed objects (*.pack + *.idx files) |
✅ | 🚫 | 2Gb+ packs support Version 2 pack-*.idx files support packs larger than 4 GiB by adding an optional table of 8-byte offset entries for large offsets |
✅ | 🚫 | On-disk reverse indexes (*.rev files) Reverse index is boosting operations such as a seeking an object by offset or scanning objects in a pack order |
🚫 | 🚫 | multi-pack-index (MIDX) Stores a list of objects and their offsets into multiple packfiles, can provide O(log N) lookup time for any number of packfiles |
🚫 | 🚫 | multi-pack-index reverse indexes (RIDX) Similar to the pack-based reverse index |
✅ | 🚫 | Cruft packs A cruft pack eliminates the need for storing unreachable objects in a loose state by including the per-object mtimes in a separate file alongside a single pack containing all loose objects |
🚫 | 🚫 | Pack and multi-pack bitmaps Bitmaps store reachability information about the set of objects in a packfile, or a multi-pack index |
🚫 (TBD) | 🚫 | commit-graph A binary file format that creates a structured representation of Git’s commit history, boost some operations |
License
MIT