If git merge --abort throws, the repo is left mid-merge. Previously the code logged a warning and returned a conflict result, giving the UI a stale file list. Now it returns Blocked with an explicit message so the caller knows manual resolution is required. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
166 lines
6.4 KiB
C#
166 lines
6.4 KiB
C#
using ClaudeDo.Data;
|
|
using ClaudeDo.Data.Git;
|
|
using ClaudeDo.Data.Models;
|
|
using ClaudeDo.Data.Repositories;
|
|
using ClaudeDo.Worker.Hub;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
|
|
|
|
namespace ClaudeDo.Worker.Services;
|
|
|
|
public sealed record MergeResult(
|
|
string Status,
|
|
IReadOnlyList<string> ConflictFiles,
|
|
string? ErrorMessage);
|
|
|
|
public sealed record MergeTargets(
|
|
string DefaultBranch,
|
|
IReadOnlyList<string> LocalBranches);
|
|
|
|
public sealed class TaskMergeService
|
|
{
|
|
public const string StatusMerged = "merged";
|
|
public const string StatusConflict = "conflict";
|
|
public const string StatusBlocked = "blocked";
|
|
|
|
private readonly IDbContextFactory<ClaudeDoDbContext> _dbFactory;
|
|
private readonly GitService _git;
|
|
private readonly HubBroadcaster _broadcaster;
|
|
private readonly ILogger<TaskMergeService> _logger;
|
|
|
|
public TaskMergeService(
|
|
IDbContextFactory<ClaudeDoDbContext> dbFactory,
|
|
GitService git,
|
|
HubBroadcaster broadcaster,
|
|
ILogger<TaskMergeService> logger)
|
|
{
|
|
_dbFactory = dbFactory;
|
|
_git = git;
|
|
_broadcaster = broadcaster;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<MergeResult> MergeAsync(
|
|
string taskId,
|
|
string targetBranch,
|
|
bool removeWorktree,
|
|
string commitMessage,
|
|
CancellationToken ct)
|
|
{
|
|
TaskEntity task;
|
|
ListEntity list;
|
|
WorktreeEntity? wt;
|
|
|
|
using (var ctx = _dbFactory.CreateDbContext())
|
|
{
|
|
task = await new TaskRepository(ctx).GetByIdAsync(taskId, ct)
|
|
?? throw new KeyNotFoundException($"Task '{taskId}' not found.");
|
|
list = await new ListRepository(ctx).GetByIdAsync(task.ListId, ct)
|
|
?? throw new InvalidOperationException("List not found.");
|
|
wt = await new WorktreeRepository(ctx).GetByTaskIdAsync(taskId, ct);
|
|
}
|
|
|
|
if (task.Status == TaskStatus.Running)
|
|
return Blocked("task is running");
|
|
if (wt is null)
|
|
return Blocked("task has no worktree");
|
|
if (wt.State != WorktreeState.Active)
|
|
return Blocked($"worktree state is {wt.State}");
|
|
if (string.IsNullOrWhiteSpace(list.WorkingDir))
|
|
return Blocked("list has no working directory");
|
|
if (!await _git.IsGitRepoAsync(list.WorkingDir, ct))
|
|
return Blocked("working directory is not a git repository");
|
|
if (await _git.IsMidMergeAsync(list.WorkingDir, ct))
|
|
return Blocked("target working directory is mid-merge");
|
|
if (await _git.HasChangesAsync(list.WorkingDir, ct))
|
|
return Blocked("target working tree has uncommitted changes");
|
|
|
|
var currentBranch = await _git.GetCurrentBranchAsync(list.WorkingDir, ct);
|
|
if (!string.Equals(currentBranch, targetBranch, StringComparison.Ordinal))
|
|
{
|
|
try { await _git.CheckoutBranchAsync(list.WorkingDir, targetBranch, ct); }
|
|
catch (Exception ex) { return Blocked($"failed to switch target branch: {ex.Message}"); }
|
|
}
|
|
|
|
var (exitCode, stderr) = await _git.MergeNoFfAsync(list.WorkingDir, wt.BranchName, commitMessage, ct);
|
|
if (exitCode != 0)
|
|
{
|
|
List<string> files;
|
|
try { files = await _git.ListConflictedFilesAsync(list.WorkingDir, ct); }
|
|
catch { files = new(); }
|
|
|
|
// If abort fails the repo is left mid-merge; the caller must resolve manually.
|
|
// Return Blocked (not conflict) so the UI does not offer a stale conflict list.
|
|
try { await _git.MergeAbortAsync(list.WorkingDir, ct); }
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "git merge --abort failed after conflict — repo is mid-merge");
|
|
return Blocked($"merge conflict and abort failed: {ex.Message} — repo is mid-merge, resolve manually");
|
|
}
|
|
|
|
if (files.Count == 0)
|
|
{
|
|
// Non-conflict failure (e.g. unrelated histories).
|
|
return new MergeResult(StatusBlocked, Array.Empty<string>(), $"merge failed: {stderr}");
|
|
}
|
|
|
|
return new MergeResult(StatusConflict, files, null);
|
|
}
|
|
|
|
string? cleanupWarning = null;
|
|
if (removeWorktree)
|
|
{
|
|
try
|
|
{
|
|
await _git.WorktreeRemoveAsync(list.WorkingDir, wt.Path, force: false, ct);
|
|
try { await _git.BranchDeleteAsync(list.WorkingDir, wt.BranchName, force: false, ct); }
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "branch delete failed for {Branch}", wt.BranchName);
|
|
cleanupWarning = $"worktree removed, branch delete failed: {ex.Message}";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "worktree remove failed for {Path}", wt.Path);
|
|
cleanupWarning = $"worktree remove failed: {ex.Message}";
|
|
}
|
|
}
|
|
|
|
using (var ctx = _dbFactory.CreateDbContext())
|
|
{
|
|
await new WorktreeRepository(ctx).SetStateAsync(taskId, WorktreeState.Merged, ct);
|
|
}
|
|
await _broadcaster.WorktreeUpdated(taskId);
|
|
|
|
_logger.LogInformation(
|
|
"Merged task {TaskId} branch {Branch} into {Target} (remove worktree: {Remove})",
|
|
taskId, wt.BranchName, targetBranch, removeWorktree);
|
|
|
|
return new MergeResult(StatusMerged, Array.Empty<string>(), cleanupWarning);
|
|
}
|
|
|
|
public async Task<MergeTargets> GetTargetsAsync(string taskId, CancellationToken ct)
|
|
{
|
|
TaskEntity task;
|
|
ListEntity list;
|
|
using (var ctx = _dbFactory.CreateDbContext())
|
|
{
|
|
task = await new TaskRepository(ctx).GetByIdAsync(taskId, ct)
|
|
?? throw new KeyNotFoundException($"Task '{taskId}' not found.");
|
|
list = await new ListRepository(ctx).GetByIdAsync(task.ListId, ct)
|
|
?? throw new InvalidOperationException("List not found.");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(list.WorkingDir))
|
|
return new MergeTargets("", Array.Empty<string>());
|
|
|
|
var current = await _git.GetCurrentBranchAsync(list.WorkingDir, ct);
|
|
var branches = await _git.ListLocalBranchesAsync(list.WorkingDir, ct);
|
|
return new MergeTargets(current, branches);
|
|
}
|
|
|
|
private static MergeResult Blocked(string reason) =>
|
|
new(StatusBlocked, Array.Empty<string>(), reason);
|
|
}
|