Pure managed C# implementation for reading from Git repositories.
- Portable and simple
- No dependencies on native code.
- Reading from local Git repositories
- Resolve HEAD and other references to commit SHAs.
- Load all files from any commit's tree.
- Load files from a specific subdirectory within a commit's tree.
- Supports both loose objects and pack files.
- Reads indexed packs with 64-bit offsets without loading complete packs.
- Supports worktrees, bare repositories, linked worktrees, and alternate object stores.
- Incremental tree traversal with early file and subtree pruning.
- Working-tree file selection for formatters and similar tools
- Query tracked files using the staging-area index, independently of HEAD.
- Parse and cache immutable gitignore rules, or query all applicable ignore files.
- Substitute filesystem and environment access for testing or virtual filesystems.
- Cloning via Git Smart HTTP
- Efficient partial cloning of subdirectories.
- Configurable API for caching git objects to make cloning more efficient.
NuGet: https://www.nuget.org/packages/GitCore/
dotnet add package GitCore
var gitDir = Path.Combine(repoRootDir, ".git");
// Load all files from the current HEAD commit
var filesAtHead = GitCore.LoadFromLocalFiles.LoadTreeContentsFromHead(gitDir);
// Or resolve HEAD and load from a specific commit
var commitSha = GitCore.LoadFromLocalFiles.ResolveHead(gitDir);
var filesAtCommit = GitCore.LoadFromLocalFiles.LoadTreeContentsFromCommit(gitDir, commitSha);
// Load only files under a specific subdirectory (paths relative to subdirectory)
var subdirFiles =
GitCore.LoadFromLocalFiles.LoadSubdirectoryContentsFromHead(
gitDir,
["implement", "Pine.Core"]);
// Or from a specific commit
var subdirFilesAtCommit =
GitCore.LoadFromLocalFiles.LoadSubdirectoryContentsFromCommit(
gitDir,
commitSha,
["implement", "Pine.Core"]);
// Resolve any reference (branch, tag, etc.)
var branchSha = GitCore.LoadFromLocalFiles.ResolveReference(gitDir, "refs/heads/main");
// Load the full in-memory object store
var repository = GitCore.LoadFromLocalFiles.LoadRepository(gitDir);LocalGitRepository owns pack handles and bounded caches, so keep it open while resolving related
objects and dispose it afterward. Selection runs before a subtree or blob is loaded.
using var repository =
GitCore.LocalGitRepository.Open(
repoRootDir,
new GitCore.LocalRepositoryOptions
{
MaximumCachedObjectBytes = 128 * 1024 * 1024,
MaximumMaterializedObjectSize = 64 * 1024 * 1024
});
var commitId =
repository.ResolveHead()
?? throw new InvalidOperationException("HEAD cannot be resolved.");
await foreach (
var file in repository.EnumerateTreeAsync(
commitId,
new GitCore.TreeTraversalOptions
{
SelectFile =
file =>
file.Name is ".order" ||
file.Name.EndsWith(".md", StringComparison.OrdinalIgnoreCase)
? GitCore.TreeFileSelection.Include
: GitCore.TreeFileSelection.Skip,
SelectSubtree =
subtree =>
subtree.Path.Contains(".attachments", StringComparer.OrdinalIgnoreCase)
? GitCore.TreeSubtreeSelection.Skip
: GitCore.TreeSubtreeSelection.Descend
}))
{
await using var destination = CreateDestination(file.Path);
await file.CopyContentToAsync(destination);
}LookupObject returns Found, Missing, or MissingPromised together with a typed,
context-rich error. GitCore never fetches a missing object implicitly. Applications can opt into
FetchMissing or Custom and supply a MissingObjectProvider, retaining control of networking,
credentials, cancellation, and persistence. Storage and limit failures use typed exceptions such
as GitObjectNotFoundException, InvalidPackIndexException, InvalidPackObjectException,
GitObjectSizeLimitException, and GitResourceLimitException; each exposes a GitErrorContext.
GitObjectReader takes an explicit IGitObjectSource, such as a blob-cache adapter,
the in-memory Repository, or a LocalGitRepository. It does not need refs, a clock,
or a filesystem. An optional IGitObjectInfoSource provides metadata without
falling back to payload reads. These APIs never fetch missing objects implicitly:
| API | Reads |
|---|---|
LookupObjectInfoAsync(objectId) |
Type and logical byte length without materializing the payload. A delta with an unavailable base still reports its declared result length. |
ReadCommitAsync(commitId) |
Commit metadata, ordered parent IDs, and the root tree ID, without opening any of those references. |
ReadTreeAsync(treeId) |
Immediate entries, including gitlinks and subtrees, without loading their targets. NameBytes preserves the filename exactly; Name is for display. |
LookupTreeEntryAsync(treeId, path) |
A path of raw filename components relative to a tree, for example ["src"u8.ToArray()]. A blocked lookup returns the matched prefix length and the next tree ID. The final entry's target is never opened. |
Object reads distinguish Missing and MissingPromised and identify the unavailable object in
Error. A blocked path is distinct from an absent path. If objects have been added to disk
since opening the repository, reopen it to refresh the object index before resuming.
For size analysis, walk tree entries and cache blob sizes by object ID. Walk each path even when two paths share a subtree ID. These sizes describe logical Git payloads, not pack or checkout disk usage.
Use GitDirectoryState to resolve refs and get shallow boundaries or promisor
configuration without opening an object store. A linked worktree supplies its own
Git directory for HEAD and worktree-private refs, and a common directory for
shared refs, configuration, and shallow boundaries. The same object reader can
be reused across worktrees.
var storage = new GitCore.MemoryGitPathStorage(filesByRelativePath);
var repoPath = GitCore.GitStoragePath.FromComponents("repo.git");
var common = new GitCore.GitDirectoryLocation(storage, repoPath);
var worktree = new GitCore.GitDirectoryLocation(storage, repoPath.Append("worktrees").Append("topic"));
var state = new GitCore.GitDirectoryState(worktree, common);
using var repository = GitCore.LocalGitRepository.OpenFromStorage(state);
var reader = new GitCore.GitObjectReader(repository);
var head = state.ResolveReference("HEAD");IGitPathStorage is read-only and takes GitStoragePath, an immutable sequence
of relative components with ordinal, case-sensitive equality and ordering.
Use GitStoragePath.Root for the store root. Parse and ToSlashSeparatedKey
are for serialized keys; native separators are handled by the filesystem adapter.
Git tree names are separate and use raw bytes, including names that cannot be
decoded as UTF-8. A blob-store adapter can list immediate children by prefix and
provide a seekable or range-backed pack stream (IGitRandomAccessRead) without
downloading the whole pack. Virtual layouts take explicit worktree, common, and
alternate object directory locations; native Open still discovers these from
Git's files. For an app-wide object cache, implement IGitObjectSource directly
and pair it with the repository state separately. GitObjects.ComputeObjectId
can verify the content ID at cache ingest without consulting any repository.
Use GitWorkingTree when processing files on disk rather than committed tree contents:
var workingTree = GitCore.GitWorkingTree.Discover(filePath);
if (workingTree is not null && workingTree.ShouldIncludeFile(filePath))
{
// Apply the formatter to this existing regular file.
}Open(repoRootDir) opens a known working-tree root; Discover(path) searches upward
for the nearest repository and returns null outside one. Both support .git
directories and gitdir files, including linked worktrees. Queries accept absolute native
paths or paths relative to RootDirectory; paths outside that tree throw.
The APIs deliberately separate three questions:
| API | Meaning | Corresponding Git command |
|---|---|---|
IsTracked(path) |
An entry exists in the current index, including staged additions, conflict stages and files deleted only on disk. | git ls-files --error-unmatch -- path |
IsIgnored(path) |
Applicable ignore rules exclude this path, regardless of tracking. | git check-ignore --no-index -- path |
ShouldIncludeFile(path) |
An existing regular file is tracked or is untracked and not ignored. | File membership in git ls-files --cached --others --exclude-standard |
An ignored file that is already tracked remains a formatting candidate. Directories,
missing files, symbolic links, Git metadata, and files inside nested repositories or
submodules are not candidates. This predicate does not test whether content has changed,
or reproduce all git status behavior (for example, assume-unchanged flags).
The caller still decides which extensions to format and enumerates the files.
Ignore evaluation supports .gitignore files at each directory level, the shared
info/exclude file, and user-wide excludes (core.excludesFile, or
$XDG_CONFIG_HOME/git/ignore, defaulting to ~/.config/git/ignore).
Later rules override earlier rules; deeper .gitignore files take precedence over
parent files and repository/user excludes. An excluded parent directory must itself
be re-included before any child can be re-included. Symlinked .gitignore files are
not followed. IsIgnored(path, isDirectory: true) also supports hypothetical directories.
Git does follow symlinks for user-wide excludes and info/exclude; these sources
retain that behavior.
Repository configuration determines case sensitivity through core.ignoreCase.
Case-insensitive index and ignore matching fold ASCII only; distinct non-ASCII names
are not conflated.
System, user, repository and enabled worktree configuration are read, including regular
includes and includeIf conditions for gitdir, gitdir/i, and onbranch.
Other conditional include kinds throw NotSupportedException rather than silently
returning an incorrect result. Options can override IgnoreCase and
GlobalExcludesFilePath (an empty path disables user-wide excludes).
An instance snapshots configuration and lazily snapshots the index.
Call RefreshIndex() after staging changes, or open a new instance.
Ignore files are read on each query, and unchanged contents reuse their parsed form.
Instances are not intended for concurrent use. Bare repositories are not working trees.
Git normally stores a checksum at the end of its staging-area index. It also supports
intentionally omitting that checksum to speed up writes: index.skipHash=true writes
an all-zero checksum field instead. feature.manyFiles=true enables this setting by
default unless explicitly overridden. An all-zero field therefore does not necessarily
mean the index is corrupt.
GitCore makes acceptance of this format an explicit application choice:
GitIndexChecksumPolicy |
Behavior |
|---|---|
RequireChecksum (default) |
Requires a nonzero checksum matching the index contents; rejects an omitted checksum. |
ValidateIfPresent |
Accepts Git's all-zero omission marker; verifies every nonzero checksum. |
Both policies validate the index structure and reject nonzero checksum mismatches.
ValidateIfPresent cannot detect content corruption through a checksum when the writer
omitted it. The checksum field must still be present and contain all zero bytes;
truncated fields or arbitrary incorrect checksums are not accepted.
For tools that need to read indexes written with checksum omission, opt in when opening or discovering the working tree:
var workingTree =
GitCore.GitWorkingTree.Discover(
filePath,
options: new GitCore.GitWorkingTreeOptions
{
IndexChecksumPolicy = GitCore.GitIndexChecksumPolicy.ValidateIfPresent
});The chosen policy is exposed as workingTree.IndexChecksumPolicy and applies to
ReadIndex(), IsTracked(), ShouldIncludeFile(), and snapshots read after
RefreshIndex(). Ignore-only queries do not read the index.
For standalone parsing, choose the same policy explicitly:
var index =
GitCore.GitIndex.Parse(
indexBytes,
checksumPolicy: GitCore.GitIndexChecksumPolicy.ValidateIfPresent);Parse takes checksumPolicy before the optional readSharedIndex resolver; callers
supplying a resolver can use the readSharedIndex: named argument.
Neither API derives its policy from Git configuration: the current settings need not
match the settings that produced an existing index. Under the strict default, an
omitted-checksum error explains Git's omission mode and how to select ValidateIfPresent.
Shared indexes referenced by a split index must always contain a valid checksum matching their reference. Neither policy relaxes this identity requirement.
var parsed = GitCore.GitIgnoreFile.Parse(rawGitignoreContent);
var ignored = parsed.IsIgnored("obj/Generated.cs");GitIgnoreFile and its exposed rules are immutable, so applications can cache parsed
files by their complete raw content, independent of a file's location.
Paths in this API are slash-separated and relative to the directory containing that
gitignore file. It supports comments, escaping, negation, directory-only and anchored
patterns, *, ?, character classes, and Git's special ** patterns.
IsIgnored also checks excluded ancestors. MatchPath matches only the supplied
entry and returns true (excluded), false (re-included), or null (no matching rule),
which allows applications to compose multiple ignore sources themselves.
Supply an application-wide parsing cache to working-tree queries:
var cache = new Dictionary<string, GitCore.GitIgnoreFile>();
var options = new GitCore.GitWorkingTreeOptions
{
ParseIgnoreFile = content =>
{
if (!cache.TryGetValue(content, out var parsed))
cache[content] = parsed = GitCore.GitIgnoreFile.Parse(content);
return parsed;
}
};
var workingTree = GitCore.GitWorkingTree.Open(repoRootDir, options: options);ReadIndex() exposes the immutable staging-area snapshot and entry metadata.
GitIndex.Parse(bytes) also parses an index without a repository or filesystem.
The index reader applies the checksum policy described above and accepts versions 2, 3 and 4; unsupported
mandatory extensions fail explicitly rather than treating tracked files as untracked.
Split indexes are resolved through the injected filesystem (GitIndex.Parse accepts
a shared-index resolver). SHA-1 and SHA-256 indexes are supported. Sparse-directory
entries are exposed, but queries beneath an unexpanded sparse-index directory throw
NotSupportedException; use a full index for those paths.
String APIs use UTF-8 Git paths and ignore/config files; malformed UTF-8 input fails
explicitly. Ignore-pattern case folding and POSIX character classes follow Git's
ASCII byte semantics rather than culture-sensitive text matching.
All working-tree filesystem and environment access goes through IGitFileSystem.
Pass a custom implementation to Open or Discover for in-memory tests; the default
is PhysicalGitFileSystem.Instance. Parsing and matching individual ignore files and
indexes require no filesystem access. No API here starts a Git process.
var subdirectoryContents =
await GitCore.LoadFromUrl.LoadSubdirectoryContentsFromGitUrlAsync(
gitUrl: "https://github.com/pine-vm/pine.git",
commitSha: "c837c8199f38aab839c40019a50055e16d100c74",
subdirectoryPath: ["guide"]);