feat(git): add ListLocalBranchesAsync
This commit is contained in:
@@ -160,6 +160,19 @@ public sealed class GitService
|
||||
return stdout.Trim();
|
||||
}
|
||||
|
||||
public async Task<List<string>> ListLocalBranchesAsync(string repoDir, CancellationToken ct = default)
|
||||
{
|
||||
var (exitCode, stdout, stderr) = await RunGitAsync(repoDir,
|
||||
["branch", "--format=%(refname:short)"], ct);
|
||||
if (exitCode != 0)
|
||||
throw new InvalidOperationException($"git branch --format failed (exit {exitCode}): {stderr}");
|
||||
|
||||
return stdout
|
||||
.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Where(s => s.Length > 0)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task MergeFfOnlyAsync(string repoDir, string branchName, CancellationToken ct = default)
|
||||
{
|
||||
var (exitCode, _, stderr) = await RunGitAsync(repoDir, ["merge", "--ff-only", branchName], ct);
|
||||
|
||||
@@ -34,4 +34,19 @@ public class GitServiceMergeTests : IDisposable
|
||||
Assert.True(branch == "main" || branch == "master",
|
||||
$"Expected main or master, got '{branch}'");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ListLocalBranchesAsync_AfterCreatingSecondBranch_ReturnsBoth()
|
||||
{
|
||||
if (!GitRepoFixture.IsGitAvailable()) return;
|
||||
|
||||
var repo = NewRepo();
|
||||
GitRepoFixture.RunGit(repo.RepoDir, "branch", "feature/x");
|
||||
|
||||
var git = new GitService();
|
||||
var branches = await git.ListLocalBranchesAsync(repo.RepoDir);
|
||||
|
||||
Assert.Contains("feature/x", branches);
|
||||
Assert.True(branches.Any(b => b == "main" || b == "master"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user