Skip to main content

Overview

The Git Repository provides a complete abstraction over Git operations using LibGit2Sharp. It supports commits, branches, remotes, stashing, tagging, and authentication. Interface: IGitRepository
Implementation: LibGit2SharpRepository
Namespace: Chapi.Domain.Interfaces / Chapi.Infrastructure.Git

Commits

CommitAsync

Creates a new commit with specified files and message.
Task<Result<GitCommit>> CommitAsync(
    string projectPath,
    string message,
    IEnumerable<string> files
)
projectPath
string
required
Path to the Git repository
message
string
required
Commit message
files
IEnumerable<string>
required
Files to stage and commit
GitCommit
Result<GitCommit>
Returns commit details including Hash, Message, Author, Date, and RelativeDate

GetCommitsAsync

Retrieves commit history with specified limit.
Task<IEnumerable<GitCommit>> GetCommitsAsync(
    string projectPath,
    int limit
)
projectPath
string
required
Path to the Git repository
limit
int
required
Maximum number of commits to retrieve
commits
IEnumerable<GitCommit>
Collection of commits with Hash, Message, Description, Author, Date, RelativeDate, and Tags

GetUnpushedCommitsAsync

Gets commits that exist locally but haven’t been pushed to remote.
Task<HashSet<string>> GetUnpushedCommitsAsync(
    string projectPath,
    string branch
)
commits
HashSet<string>
Set of commit hashes that are ahead of the remote branch

Changes

GetChangesAsync

Retrieves all file changes in the working directory.
Task<IEnumerable<FileChange>> GetChangesAsync(string projectPath)
changes
IEnumerable<FileChange>
Collection of FileChange objects with FilePath, Status (Added/Modified/Deleted/Renamed), Additions, and Deletions

StageFilesAsync

Stages specified files for commit.
Task<Result> StageFilesAsync(
    string projectPath,
    IEnumerable<string> files
)

UnstageFilesAsync

Unstages previously staged files.
Task<Result> UnstageFilesAsync(
    string projectPath,
    IEnumerable<string> files
)

DiscardChangesAsync

Discards uncommitted changes.
Task<Result> DiscardChangesAsync(
    string projectPath,
    IEnumerable<string>? files = null
)
files
IEnumerable<string>?
default:"null"
Specific files to discard. If null, discards all changes

Branches

GetBranchesAsync

Retrieves all local and remote branches.
Task<IEnumerable<string>> GetBranchesAsync(string projectPath)
branches
IEnumerable<string>
List of branch names (local branches and remote branches not tracked locally)

GetCurrentBranchAsync

Gets the name of the current branch.
Task<string> GetCurrentBranchAsync(string projectPath)
branch
string
Current branch name, or short SHA if in detached HEAD state

SwitchBranchAsync

Switches to a different branch.
Task<Result> SwitchBranchAsync(
    string projectPath,
    string branchName
)
branchName
string
required
Name of branch to switch to (local or remote)

CreateBranchAsync

Creates a new branch.
Task<Result> CreateBranchAsync(
    string projectPath,
    string branchName,
    string? fromCommitOrBranch = null
)
fromCommitOrBranch
string?
default:"null"
Starting point for the new branch (defaults to current HEAD)

DeleteBranchAsync

Deletes a branch locally and optionally remotely.
Task<Result> DeleteBranchAsync(
    string projectPath,
    string branchName,
    bool force = false,
    bool deleteRemote = false
)
force
bool
default:"false"
Force deletion even if branch has unmerged changes
deleteRemote
bool
default:"false"
Also delete the branch from remote repository

MergeBranchAsync

Merges a source branch into the current branch.
Task<Result> MergeBranchAsync(
    string projectPath,
    string sourceBranch,
    bool fastForward = true
)

SquashMergeBranchAsync

Performs a squash merge.
Task<Result> SquashMergeBranchAsync(
    string projectPath,
    string sourceBranch,
    string? commitMessage = null
)

RebaseBranchAsync

Rebases current branch onto target branch.
Task<Result> RebaseBranchAsync(
    string projectPath,
    string targetBranch
)

CheckMergeConflictsAsync

Checks if merging would cause conflicts.
Task<(bool hasConflicts, string message)> CheckMergeConflictsAsync(
    string projectPath,
    string sourceBranch
)

ResetAsync

Resets current branch to a specific commit.
Task<Result> ResetAsync(
    string projectPath,
    string target,
    ResetMode mode
)
mode
ResetMode
required
Reset mode: Soft (keeps changes staged), Mixed (keeps changes unstaged), or Hard (discards all changes)

Remote Operations

PushAsync

Pushes commits to remote repository.
Task<Result> PushAsync(
    string projectPath,
    string branch,
    bool force = false
)
force
bool
default:"false"
Force push (overwrites remote history)

PullAsync

Pulls changes from remote repository.
Task<Result> PullAsync(
    string projectPath,
    string branch
)

FetchAsync

Fetches changes from remote without merging.
Task<Result> FetchAsync(string projectPath)

GetAheadBehindCountAsync

Gets how many commits ahead/behind the remote branch.
Task<(int Ahead, int Behind)> GetAheadBehindCountAsync(string projectPath)

GetRemoteUrlAsync

Gets the URL of a remote.
Task<string> GetRemoteUrlAsync(
    string projectPath,
    string remoteName = "origin"
)

SetRemoteUrlAsync

Updates the URL of a remote.
Task<Result> SetRemoteUrlAsync(
    string projectPath,
    string remoteName,
    string url
)

Lifecycle

CloneAsync

Clones a remote repository.
Task<Result> CloneAsync(
    string url,
    string destinationPath
)

InitAsync

Initializes a new Git repository.
Task<Result> InitAsync(string projectPath)

AddRemoteAsync

Adds a new remote.
Task<Result> AddRemoteAsync(
    string projectPath,
    string name,
    string url
)

Stash

StashChangesAsync

Stashes current changes.
Task<Result> StashChangesAsync(
    string projectPath,
    string message,
    IEnumerable<string>? files = null
)

ListStashesAsync

Lists all stashes.
Task<IEnumerable<GitStash>> ListStashesAsync(string projectPath)

StashPopAsync

Applies and removes a stash.
Task<Result> StashPopAsync(
    string projectPath,
    int? index = null
)

StashDropAsync

Removes a stash without applying.
Task<Result> StashDropAsync(
    string projectPath,
    int index
)

StashClearAsync

Removes all stashes.
Task<Result> StashClearAsync(string projectPath)

Tags

CreateTagAsync

Creates a new annotated tag.
Task<Result> CreateTagAsync(
    string projectPath,
    string tagName,
    string message,
    string commitHash = null
)

DeleteTagLocalAsync

Deletes a local tag.
Task<Result> DeleteTagLocalAsync(
    string projectPath,
    string tagName
)

PushTagAsync

Pushes a tag to remote.
Task<Result> PushTagAsync(
    string projectPath,
    string tagName
)

GetTagsAsync

Retrieves all tags.
Task<IEnumerable<GitTagItem>> GetTagsAsync(string projectPath)

Configuration

GetConfigAsync

Gets a configuration value.
Task<string> GetConfigAsync(
    string projectPath,
    string key,
    bool isGlobal = false
)

SetConfigAsync

Sets a configuration value.
Task<Result> SetConfigAsync(
    string projectPath,
    string key,
    string value,
    bool isGlobal = false
)

UnsetConfigAsync

Removes a configuration value.
Task<Result> UnsetConfigAsync(
    string projectPath,
    string key,
    bool isGlobal = false
)

Utilities

IsGitInstalled

Checks if Git is available (always returns true with LibGit2Sharp).
bool IsGitInstalled()

HasUpstreamAsync

Checks if a branch has an upstream remote.
Task<bool> HasUpstreamAsync(
    string projectPath,
    string branchName
)

GetDiffAsync

Gets diff for a specific file.
Task<string> GetDiffAsync(
    string projectPath,
    string file,
    string? revision = null
)

GetFileStatsAsync

Gets addition/deletion statistics for a file.
Task<(int additions, int deletions)> GetFileStatsAsync(
    string projectPath,
    string filePath
)

GetMetadataAsync

Gets repository metadata.
Task<Result<GitRepositoryMetadata>> GetMetadataAsync(string projectPath)
metadata
GitRepositoryMetadata
Includes UserName, UserEmail, RemoteUrl, CurrentBranch, IsDetached, Ahead, Behind, HasUpstream

Build docs developers (and LLMs) love