172 lines
4.3 KiB
C#
172 lines
4.3 KiB
C#
using MarketAlly.GitCommitEditor.Resources;
|
|
|
|
namespace MarketAlly.GitCommitEditor.Models;
|
|
|
|
/// <summary>
|
|
/// Safety information for a batch rewrite operation.
|
|
/// </summary>
|
|
public sealed class RewriteSafetyInfo
|
|
{
|
|
/// <summary>
|
|
/// Whether the repository has uncommitted changes.
|
|
/// </summary>
|
|
public bool HasUncommittedChanges { get; init; }
|
|
|
|
/// <summary>
|
|
/// Whether any commits to be rewritten have been pushed to remote.
|
|
/// </summary>
|
|
public bool HasPushedCommits { get; init; }
|
|
|
|
/// <summary>
|
|
/// Number of commits that have been pushed to remote.
|
|
/// </summary>
|
|
public int PushedCommitCount { get; init; }
|
|
|
|
/// <summary>
|
|
/// Number of commits that are local only.
|
|
/// </summary>
|
|
public int LocalOnlyCommitCount { get; init; }
|
|
|
|
/// <summary>
|
|
/// Total commits to be rewritten.
|
|
/// </summary>
|
|
public int TotalCommitCount { get; init; }
|
|
|
|
/// <summary>
|
|
/// Whether the current branch tracks a remote branch.
|
|
/// </summary>
|
|
public bool HasRemoteTracking { get; init; }
|
|
|
|
/// <summary>
|
|
/// The remote tracking branch name, if any.
|
|
/// </summary>
|
|
public string? RemoteTrackingBranch { get; init; }
|
|
|
|
/// <summary>
|
|
/// Number of commits ahead of remote.
|
|
/// </summary>
|
|
public int? AheadOfRemote { get; init; }
|
|
|
|
/// <summary>
|
|
/// Number of commits behind remote.
|
|
/// </summary>
|
|
public int? BehindRemote { get; init; }
|
|
|
|
/// <summary>
|
|
/// Whether a backup branch was created.
|
|
/// </summary>
|
|
public bool BackupBranchCreated { get; init; }
|
|
|
|
/// <summary>
|
|
/// Name of the backup branch, if created.
|
|
/// </summary>
|
|
public string? BackupBranchName { get; init; }
|
|
|
|
/// <summary>
|
|
/// Whether the operation can proceed safely.
|
|
/// </summary>
|
|
public bool CanProceedSafely => !HasUncommittedChanges && !HasPushedCommits;
|
|
|
|
/// <summary>
|
|
/// Whether the operation can proceed with warnings.
|
|
/// </summary>
|
|
public bool CanProceedWithWarnings => !HasUncommittedChanges;
|
|
|
|
/// <summary>
|
|
/// Gets warning messages based on the safety info.
|
|
/// </summary>
|
|
public IReadOnlyList<string> GetWarnings()
|
|
{
|
|
var warnings = new List<string>();
|
|
|
|
if (HasUncommittedChanges)
|
|
{
|
|
warnings.Add(Str.Safety_UncommittedChanges);
|
|
}
|
|
|
|
if (HasPushedCommits)
|
|
{
|
|
warnings.Add(Str.Safety_PushedCommits(PushedCommitCount));
|
|
}
|
|
|
|
if (BehindRemote > 0)
|
|
{
|
|
warnings.Add(Str.Safety_BehindRemote(BehindRemote.Value));
|
|
}
|
|
|
|
return warnings;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a summary description of the operation.
|
|
/// </summary>
|
|
public string GetSummary()
|
|
{
|
|
var parts = new List<string>();
|
|
|
|
if (LocalOnlyCommitCount > 0)
|
|
{
|
|
parts.Add($"{LocalOnlyCommitCount} local commit(s)");
|
|
}
|
|
|
|
if (PushedCommitCount > 0)
|
|
{
|
|
parts.Add($"{PushedCommitCount} pushed commit(s)");
|
|
}
|
|
|
|
return string.Join(" and ", parts) + " will be rewritten.";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Result of a batch rewrite execute operation.
|
|
/// </summary>
|
|
public sealed class BatchRewriteResult
|
|
{
|
|
/// <summary>
|
|
/// Whether the overall operation succeeded.
|
|
/// </summary>
|
|
public bool Success { get; init; }
|
|
|
|
/// <summary>
|
|
/// Number of commits successfully rewritten.
|
|
/// </summary>
|
|
public int SuccessCount { get; init; }
|
|
|
|
/// <summary>
|
|
/// Number of commits that failed to rewrite.
|
|
/// </summary>
|
|
public int FailedCount { get; init; }
|
|
|
|
/// <summary>
|
|
/// Number of commits skipped.
|
|
/// </summary>
|
|
public int SkippedCount { get; init; }
|
|
|
|
/// <summary>
|
|
/// Error message if the operation failed.
|
|
/// </summary>
|
|
public string? ErrorMessage { get; init; }
|
|
|
|
/// <summary>
|
|
/// Whether a force push is required.
|
|
/// </summary>
|
|
public bool RequiresForcePush { get; init; }
|
|
|
|
/// <summary>
|
|
/// The backup branch name if one was created.
|
|
/// </summary>
|
|
public string? BackupBranchName { get; init; }
|
|
|
|
/// <summary>
|
|
/// Individual operation results.
|
|
/// </summary>
|
|
public IReadOnlyList<RewriteOperation> Operations { get; init; } = [];
|
|
|
|
public static BatchRewriteResult Failure(string error) => new()
|
|
{
|
|
Success = false,
|
|
ErrorMessage = error
|
|
};
|
|
}
|