Skip to main content

Overview

The Git Operations use cases provide comprehensive Git functionality including commits, branches, stashing, and remote operations.

CommitChangesUseCase

Commits staged changes to the Git repository.

Request Model

public class CommitRequest
{
    public string ProjectPath { get; set; } = string.Empty;
    public string Message { get; set; } = string.Empty;
    public IEnumerable<string> Files { get; set; } = Enumerable.Empty<string>();
}

Method Signature

public async Task<Result<GitCommit>> ExecuteAsync(CommitRequest request)
request
CommitRequest
required
The commit request parameters
Result<GitCommit>
Result<GitCommit>
Result containing the created commit

Example Usage

var useCase = new CommitChangesUseCase(gitRepo, notifications);
var request = new CommitRequest
{
    ProjectPath = "C:\\Projects\\MyApp",
    Message = "Add user authentication feature",
    Files = new[] { "src/Auth/Login.cs", "src/Auth/Register.cs" }
};

var result = await useCase.ExecuteAsync(request);

if (result.IsSuccess)
{
    Console.WriteLine($"Committed: {result.Data.Sha}");
}

CreateBranchUseCase

Creates a new Git branch.

Method Signature

public async Task<Result> ExecuteAsync(
    string projectPath, 
    string branchName, 
    string? fromCommitOrBranch = null
)
projectPath
string
required
Path to the project repository
branchName
string
required
Name for the new branch
fromCommitOrBranch
string
Optional commit SHA or branch name to create from (defaults to HEAD)
Result
Result
Result indicating success or failure

Example Usage

var useCase = new CreateBranchUseCase(gitRepo, notificationService);

// Create from HEAD
var result = await useCase.ExecuteAsync(
    projectPath: "C:\\Projects\\MyApp",
    branchName: "feature/new-feature"
);

// Create from specific branch
var result2 = await useCase.ExecuteAsync(
    projectPath: "C:\\Projects\\MyApp",
    branchName: "hotfix/bug-fix",
    fromCommitOrBranch: "develop"
);

SwitchBranchUseCase

Switches to a different Git branch.

Method Signature

public async Task<Result> ExecuteAsync(
    string projectPath, 
    string branchName, 
    bool stashChanges = false
)
projectPath
string
required
Path to the project repository
branchName
string
required
Name of the branch to switch to
stashChanges
bool
default:"false"
Whether to automatically stash uncommitted changes before switching
Result
Result
Result indicating success or failure

Example Usage

var useCase = new SwitchBranchUseCase(gitRepo, notifications);

// Switch without stashing
var result = await useCase.ExecuteAsync(
    projectPath: "C:\\Projects\\MyApp",
    branchName: "develop"
);

// Switch with automatic stashing
var result2 = await useCase.ExecuteAsync(
    projectPath: "C:\\Projects\\MyApp",
    branchName: "main",
    stashChanges: true
);

PushChangesUseCase

Pushes local commits to the remote repository.

Method Signature

public async Task<Result> ExecuteAsync(string projectPath, string branch)
projectPath
string
required
Path to the project repository
branch
string
required
Name of the branch to push
Result
Result
Result indicating success or failure

Example Usage

var useCase = new PushChangesUseCase(gitRepo, notifications);
var result = await useCase.ExecuteAsync(
    projectPath: "C:\\Projects\\MyApp",
    branch: "main"
);

if (result.IsSuccess)
{
    Console.WriteLine("Changes pushed successfully");
}

PullChangesUseCase

Pulls changes from the remote repository.

Method Signature

public async Task<Result> ExecuteAsync(string projectPath)
projectPath
string
required
Path to the project repository
Result
Result
Result indicating success or failure

LoadChangesUseCase

Loads the current working directory changes.

Method Signature

public async Task<IEnumerable<FileChange>> ExecuteAsync(string projectPath)
projectPath
string
required
Path to the project repository
IEnumerable<FileChange>
IEnumerable<FileChange>
Collection of file changes

FileChange Entity

public class FileChange
{
    public string FilePath { get; set; }
    public ChangeStatus Status { get; set; }
    public int Additions { get; set; }
    public int Deletions { get; set; }
}

public enum ChangeStatus
{
    Modified,
    Added,
    Deleted,
    Renamed,
    Untracked,
    Conflict
}

Example Usage

var useCase = new LoadChangesUseCase(gitRepo);
var changes = await useCase.ExecuteAsync("C:\\Projects\\MyApp");

foreach (var change in changes)
{
    Console.WriteLine($"{change.Status}: {change.FilePath}");
    Console.WriteLine($"  +{change.Additions} -{change.Deletions}");
}

StashChangesUseCase

Stashes uncommitted changes.

Method Signature

public async Task<Result> ExecuteAsync(
    string projectPath, 
    string message, 
    IEnumerable<string>? files = null
)
projectPath
string
required
Path to the project repository
message
string
required
Message describing the stash
files
IEnumerable<string>
Optional list of specific files to stash (null = all changes)
Result
Result
Result indicating success or failure

Example Usage

var useCase = new StashChangesUseCase(gitRepo, notificationService);

// Stash all changes
var result = await useCase.ExecuteAsync(
    projectPath: "C:\\Projects\\MyApp",
    message: "WIP: Working on authentication"
);

// Stash specific files
var result2 = await useCase.ExecuteAsync(
    projectPath: "C:\\Projects\\MyApp",
    message: "Save config changes",
    files: new[] { "appsettings.json" }
);

StashPopUseCase

Applies and removes the most recent stash.

Method Signature

public async Task<Result> ExecuteAsync(string projectPath, int index = 0)
projectPath
string
required
Path to the project repository
index
int
default:"0"
Index of the stash to pop (0 = most recent)

CreateTagUseCase

Creates a Git tag.

Method Signature

public async Task<Result> ExecuteAsync(
    string projectPath, 
    string tagName, 
    string message = null
)
projectPath
string
required
Path to the project repository
tagName
string
required
Name of the tag (e.g., “v1.0.0”)
message
string
Optional tag message (creates annotated tag if provided)

Example Usage

var useCase = new CreateTagUseCase(gitRepo);
var result = await useCase.ExecuteAsync(
    projectPath: "C:\\Projects\\MyApp",
    tagName: "v1.0.0",
    message: "Release version 1.0.0"
);

LoadHistoryUseCase

Loads the commit history of the repository.

Method Signature

public async Task<IEnumerable<GitCommit>> ExecuteAsync(
    string projectPath, 
    int maxCount = 100
)
projectPath
string
required
Path to the project repository
maxCount
int
default:"100"
Maximum number of commits to retrieve

Example Usage

var useCase = new LoadHistoryUseCase(gitRepo);
var commits = await useCase.ExecuteAsync(
    projectPath: "C:\\Projects\\MyApp",
    maxCount: 50
);

foreach (var commit in commits)
{
    Console.WriteLine($"{commit.Sha.Substring(0, 7)} - {commit.Message}");
}

FetchChangesUseCase

Fetches changes from the remote repository without merging.

Method Signature

public async Task<Result> ExecuteAsync(string projectPath, bool isSilent = false)
projectPath
string
required
Path to the project repository
isSilent
bool
default:"false"
If true, suppresses notifications

Example Usage

var useCase = new FetchChangesUseCase(gitRepo, notifications);
var result = await useCase.ExecuteAsync("C:\\Projects\\MyApp");

if (result.IsSuccess)
{
    Console.WriteLine("Fetch completed successfully");
}
Source: FetchChangesUseCase.cs:21-42

DiscardChangesUseCase

Discards uncommitted changes in specific files or all files.

Method Signature

public async Task<Result> ExecuteAsync(
    string projectPath, 
    IEnumerable<string>? files = null
)
projectPath
string
required
Path to the project repository
files
IEnumerable<string>
Specific files to discard changes from. If null or empty, discards all changes.

Example Usage

// Discard changes in specific files
var useCase = new DiscardChangesUseCase(gitRepo, notifications);
var files = new[] { "src/Program.cs", "README.md" };
await useCase.ExecuteAsync("C:\\Projects\\MyApp", files);

// Discard all changes
await useCase.ExecuteAsync("C:\\Projects\\MyApp", null);
This operation is irreversible. Discarded changes cannot be recovered.

ResetCommitUseCase

Resets (undoes) the last commit with different reset modes.

Method Signature

public async Task<Result> ExecuteAsync(
    string projectPath, 
    ResetMode mode = ResetMode.Soft
)
projectPath
string
required
Path to the project repository
mode
ResetMode
default:"Soft"
Reset mode determining what happens to changes

Example Usage

var useCase = new ResetCommitUseCase(gitRepo, notifications);

// Soft reset - keep changes staged
await useCase.ExecuteAsync("C:\\Projects\\MyApp", ResetMode.Soft);

// Hard reset - discard all changes
await useCase.ExecuteAsync("C:\\Projects\\MyApp", ResetMode.Hard);
Hard reset is destructive: Using ResetMode.Hard permanently discards all changes from the last commit. Use with caution.

StashClearUseCase

Clears all stashed changes from the stash list.

Method Signature

public async Task<Result> ExecuteAsync(string projectPath)
projectPath
string
required
Path to the project repository
This operation permanently removes all stashed changes. This cannot be undone.

StashDropUseCase

Drops (removes) a specific stash entry.

Method Signature

public async Task<Result> ExecuteAsync(string projectPath, int stashIndex = 0)
projectPath
string
required
Path to the project repository
stashIndex
int
default:"0"
Index of the stash to drop (0 is the most recent)

Example Usage

var useCase = new StashDropUseCase(gitRepo, notifications);

// Drop the most recent stash
await useCase.ExecuteAsync("C:\\Projects\\MyApp", 0);

// Drop a specific stash by index
await useCase.ExecuteAsync("C:\\Projects\\MyApp", 2);

DeleteTagUseCase

Deletes a Git tag from the repository.

Method Signature

public async Task<Result> ExecuteAsync(string projectPath, string tagName)
projectPath
string
required
Path to the project repository
tagName
string
required
Name of the tag to delete

Example Usage

var useCase = new DeleteTagUseCase(gitRepo, notifications);
await useCase.ExecuteAsync("C:\\Projects\\MyApp", "v1.0.0-beta");

GetBranchesUseCase

Retrieves all branches in the repository.

Method Signature

public async Task<IEnumerable<string>> ExecuteAsync(string projectPath)
projectPath
string
required
Path to the project repository

Example Usage

var useCase = new GetBranchesUseCase(gitRepo);
var branches = await useCase.ExecuteAsync("C:\\Projects\\MyApp");

foreach (var branch in branches)
{
    Console.WriteLine(branch);
}

Build docs developers (and LLMs) love