BRIEF §14.5 asks for an explicit conflict prompt when both sides changed. A save of a server sequence that meets settings someone changed to other values on the server now stops (the change stays pending) and the editor asks: Keep mine saves the editor's values over the server's, Take server (the safe default) shows the server's values as one undoable step, so Ctrl+Z brings the editor's back. Both first take in what only the server changed. Settings changed on one side only still merge silently; a reload after an analysis never merges over a conflicting pending change. The Export "Sync" box moves under Save to so neither is cut in longer languages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
83 lines
4.2 KiB
C#
83 lines
4.2 KiB
C#
using System.Text.Json.Nodes;
|
|
using Titano.Core.Project;
|
|
using Titano.Data.Sync;
|
|
|
|
namespace Titano.App.Editor;
|
|
|
|
/// <summary>
|
|
/// Three-way merge of projects: the editor's edit (<c>ours</c>) and a change stored meanwhile by someone else
|
|
/// (<c>theirs</c>: an analysis filling Auto values, another client through the server), both made from <c>basis</c>.
|
|
/// Values are compared member by member down the JSON tree; a member only one side changed takes that side's value, a
|
|
/// member both changed takes the editor's (the user is looking at it). <see cref="Conflicts"/> lists those members for
|
|
/// a source that asks the user instead (a server sequence, BRIEF §14.5).
|
|
/// </summary>
|
|
public static class ProjectMerge
|
|
{
|
|
public static ProjectSettings Merge(ProjectSettings basis, ProjectSettings ours, ProjectSettings theirs)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(basis);
|
|
ArgumentNullException.ThrowIfNull(ours);
|
|
ArgumentNullException.ThrowIfNull(theirs);
|
|
return new Rebase(basis, theirs).Apply(ours);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The members both sides changed to different values, in document order: the path below the project
|
|
/// ("exposure.deflickerWindow.manual"), the editor's value and theirs as JSON (null when absent).
|
|
/// </summary>
|
|
public static IReadOnlyList<SettingsDifference> Conflicts(ProjectSettings basis, ProjectSettings ours, ProjectSettings theirs)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(basis);
|
|
ArgumentNullException.ThrowIfNull(ours);
|
|
ArgumentNullException.ThrowIfNull(theirs);
|
|
var conflicts = new List<SettingsDifference>();
|
|
Collect(JsonNode.Parse(basis.ToJson()), JsonNode.Parse(ours.ToJson()), JsonNode.Parse(theirs.ToJson()), string.Empty, conflicts);
|
|
return conflicts;
|
|
}
|
|
|
|
/// <summary>True when both projects store the same values.</summary>
|
|
public static bool SameValues(ProjectSettings a, ProjectSettings b) => a.ToJson() == b.ToJson();
|
|
|
|
private static JsonNode? Merge(JsonNode? basis, JsonNode? ours, JsonNode? theirs)
|
|
{
|
|
if (JsonNode.DeepEquals(ours, basis)) return theirs?.DeepClone();
|
|
if (JsonNode.DeepEquals(theirs, basis) || JsonNode.DeepEquals(theirs, ours)) return ours?.DeepClone();
|
|
if (ours is not JsonObject mine || theirs is not JsonObject other) return ours?.DeepClone();
|
|
var common = basis as JsonObject;
|
|
var merged = new JsonObject();
|
|
foreach (var name in mine.Select(p => p.Key).Union(other.Select(p => p.Key)))
|
|
{
|
|
var value = Merge(common?[name], mine[name], other[name]);
|
|
if (value is not null || mine.ContainsKey(name)) merged[name] = value;
|
|
}
|
|
return merged;
|
|
}
|
|
|
|
/// <summary>The members <see cref="Merge(JsonNode?, JsonNode?, JsonNode?)"/> settles for the editor although both sides changed them.</summary>
|
|
private static void Collect(JsonNode? basis, JsonNode? ours, JsonNode? theirs, string path, List<SettingsDifference> conflicts)
|
|
{
|
|
if (JsonNode.DeepEquals(ours, basis) || JsonNode.DeepEquals(theirs, basis) || JsonNode.DeepEquals(theirs, ours)) return;
|
|
if (ours is JsonObject mine && theirs is JsonObject other)
|
|
{
|
|
var common = basis as JsonObject;
|
|
foreach (var name in mine.Select(p => p.Key).Union(other.Select(p => p.Key)))
|
|
Collect(common?[name], mine[name], other[name], path.Length == 0 ? name : path + "." + name, conflicts);
|
|
return;
|
|
}
|
|
conflicts.Add(new SettingsDifference(path.Length == 0 ? "settings" : path, ours?.ToJsonString(), theirs?.ToJsonString()));
|
|
}
|
|
|
|
/// <summary>One outside change (<paramref name="basis"/> → <paramref name="theirs"/>) applied to any number of projects made from the basis.</summary>
|
|
public sealed class Rebase(ProjectSettings basis, ProjectSettings theirs)
|
|
{
|
|
private readonly JsonNode? _basis = JsonNode.Parse(basis.ToJson());
|
|
private readonly JsonNode? _theirs = JsonNode.Parse(theirs.ToJson());
|
|
|
|
public ProjectSettings Apply(ProjectSettings ours)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(ours);
|
|
return ProjectSettings.FromJson(Merge(_basis, JsonNode.Parse(ours.ToJson()), _theirs)?.ToJsonString() ?? "{}");
|
|
}
|
|
}
|
|
}
|