feat(git): add GetCurrentBranchAsync
This commit is contained in:
@@ -151,6 +151,15 @@ public sealed class GitService
|
|||||||
throw new InvalidOperationException($"git branch {flag} failed (exit {exitCode}): {stderr}");
|
throw new InvalidOperationException($"git branch {flag} failed (exit {exitCode}): {stderr}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<string> GetCurrentBranchAsync(string repoDir, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
var (exitCode, stdout, stderr) = await RunGitAsync(repoDir,
|
||||||
|
["symbolic-ref", "--short", "HEAD"], ct);
|
||||||
|
if (exitCode != 0)
|
||||||
|
throw new InvalidOperationException($"git symbolic-ref --short HEAD failed (exit {exitCode}): {stderr}");
|
||||||
|
return stdout.Trim();
|
||||||
|
}
|
||||||
|
|
||||||
public async Task MergeFfOnlyAsync(string repoDir, string branchName, CancellationToken ct = default)
|
public async Task MergeFfOnlyAsync(string repoDir, string branchName, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
var (exitCode, _, stderr) = await RunGitAsync(repoDir, ["merge", "--ff-only", branchName], ct);
|
var (exitCode, _, stderr) = await RunGitAsync(repoDir, ["merge", "--ff-only", branchName], ct);
|
||||||
|
|||||||
37
tests/ClaudeDo.Worker.Tests/Runner/GitServiceMergeTests.cs
Normal file
37
tests/ClaudeDo.Worker.Tests/Runner/GitServiceMergeTests.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using ClaudeDo.Data.Git;
|
||||||
|
using ClaudeDo.Worker.Tests.Infrastructure;
|
||||||
|
|
||||||
|
namespace ClaudeDo.Worker.Tests.Runner;
|
||||||
|
|
||||||
|
public class GitServiceMergeTests : IDisposable
|
||||||
|
{
|
||||||
|
private readonly List<GitRepoFixture> _repos = new();
|
||||||
|
|
||||||
|
private GitRepoFixture NewRepo()
|
||||||
|
{
|
||||||
|
var r = new GitRepoFixture();
|
||||||
|
_repos.Add(r);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
foreach (var r in _repos) try { r.Dispose(); } catch { }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetCurrentBranchAsync_FreshRepo_ReturnsDefaultBranch()
|
||||||
|
{
|
||||||
|
if (!GitRepoFixture.IsGitAvailable()) return;
|
||||||
|
|
||||||
|
var repo = NewRepo();
|
||||||
|
var git = new GitService();
|
||||||
|
|
||||||
|
var branch = await git.GetCurrentBranchAsync(repo.RepoDir);
|
||||||
|
|
||||||
|
Assert.False(string.IsNullOrWhiteSpace(branch));
|
||||||
|
// Default branch is either "main" or "master" depending on git config.
|
||||||
|
Assert.True(branch == "main" || branch == "master",
|
||||||
|
$"Expected main or master, got '{branch}'");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user