merge: review2-p5a-fix-desktop into phase 5a fix round 1 integration
# Conflicts: # src/Chronos.Data/Workspaces/WorkspaceSync.cs
This commit is contained in:
commit
1a35fcdb27
28 files changed
+1328
-139
No files matched your search
@@ -31,6 +31,12 @@ public enum WorkspaceState
|
||||
|
||||
/// <summary>The workspace holds no media for the project (never copied, or removed): copy them again to edit.</summary>
|
||||
MediaRemoved,
|
||||
|
||||
/// <summary>
|
||||
/// The home answered that it no longer has the project or its sequence (the sequence left its index): nothing more can be
|
||||
/// sent, and the local copy can only be removed, its work kept aside. Told by the desktop, which asks the home.
|
||||
/// </summary>
|
||||
Gone,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -45,7 +51,15 @@ public sealed record WorkspaceStatus(WorkspaceState State, bool Modified, int Ah
|
||||
/// <summary>What a push, pull or resolve did: the versions it stored at the home and those it took from there, and where it left the project.</summary>
|
||||
/// <param name="SavedAs">After <see cref="ConflictChoice.SaveBoth"/>: the new project at the home holding this workspace's versions.</param>
|
||||
public sealed record WorkspaceSyncResult(WorkspaceStatus Status, IReadOnlyList<ProjectVersion> Pushed, IReadOnlyList<ProjectVersion> Pulled,
|
||||
SequenceProject? SavedAs = null);
|
||||
SequenceProject? SavedAs = null)
|
||||
{
|
||||
/// <summary>
|
||||
/// After <see cref="ConflictChoice.TakeServer"/> settled a conflict: the working state as keeping this side's values on
|
||||
/// the settings both changed would have left it (positions on the media's frames), so the editor takes the server's
|
||||
/// values as one step its undo takes back, and only them; null otherwise.
|
||||
/// </summary>
|
||||
public ProjectSettings? Kept { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>How a conflict is settled (owner review 2, R8: only on real conflicts).</summary>
|
||||
public enum ConflictChoice
|
||||
@@ -82,13 +96,14 @@ public sealed class WorkspaceSyncException(PushOutcome outcome, string message)
|
||||
/// When the home moved meanwhile, its new versions are pulled and each local commit is replayed on top of them
|
||||
/// (<see cref="ProjectMerge.Replay"/>, new ids, the old files kept in <c>versions/.replaced</c>), and the working state is
|
||||
/// merged too (<see cref="ProjectMerge.MergeWithConflicts"/>); settings both sides changed to different values stop
|
||||
/// there as a conflict (<see cref="WorkspaceConflict"/>) for the user to settle (<see cref="ResolveAsync"/>). Versions
|
||||
/// travel in the portable form (<see cref="ProjectContent"/>), so frames inserted at the home never move a position; before
|
||||
/// a merge the media learn how the home names its photos now (hashed or converted there since the copy), so every side
|
||||
/// names one photo alike. Settings of a newer Chronos are never merged with this one's: a project with nothing of its own
|
||||
/// moves onto them read-only, any other waits on a conflict of format that only Save both settles
|
||||
/// (<see cref="WorkspaceConflict.NewerFormat"/>). Every operation holds the project's lease; nothing is ever deleted: a
|
||||
/// restore or save-both first saves uncommitted changes as a version.
|
||||
/// there as a conflict (<see cref="WorkspaceConflict"/>) for the user to settle (<see cref="ResolveAsync"/>), a choice
|
||||
/// applying only to the conflicts the user was shown. Versions travel in the portable form (<see cref="ProjectContent"/>),
|
||||
/// so frames inserted at the home never move a position; before a merge the media learn how the home names its photos now
|
||||
/// (hashed or converted there since the copy), so every side names one photo alike. Settings of a newer Chronos are never
|
||||
/// merged with this one's: a project with nothing of its own moves onto them read-only, any other waits on a conflict of
|
||||
/// format that only Save both settles (<see cref="WorkspaceConflict.NewerFormat"/>). Every operation holds the project's
|
||||
/// lease; nothing is ever deleted: a restore, a save-both or taking the server's values first saves uncommitted changes as
|
||||
/// a version.
|
||||
/// Every write sequence survives a cut at any step: the working state's base is the commit point. A push moves the base
|
||||
/// before its local file reads as sent; a replay journals itself in <c>sync.json</c> (<see cref="WorkspaceReplay"/>), writes
|
||||
/// the replayed versions, then the base, and only then sets the old ones aside; a save-both journals its copy and the ids
|
||||
@@ -261,7 +276,9 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
|
||||
|
||||
/// <summary>
|
||||
/// Takes the home's versions newer than the base: the working state (and any local versions, replayed) moves onto the
|
||||
/// home's head; a real conflict stops it (<see cref="WorkspaceState.Conflict"/>).
|
||||
/// home's head; a real conflict stops it (<see cref="WorkspaceState.Conflict"/>). A conflict recorded before is worked
|
||||
/// out again (the home may have moved on, this side may have been edited since), so what is asked is what differs now;
|
||||
/// one that no longer conflicts is settled by the merge.
|
||||
/// </summary>
|
||||
/// <exception cref="HomeUnavailableException">The home cannot be reached.</exception>
|
||||
public async Task<WorkspaceSyncResult> PullAsync(Guid projectId, IProjectHome home, CancellationToken cancellationToken = default)
|
||||
@@ -272,9 +289,14 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
|
||||
{
|
||||
await SettleAsync(project, home, cancellationToken).ConfigureAwait(false);
|
||||
IReadOnlyList<ProjectVersion> pulled = [];
|
||||
if (project.ReadSync().Conflict is null)
|
||||
var bundle = project.ReadBundle();
|
||||
if (project.ReadSync().Conflict is not null)
|
||||
{
|
||||
(_, var conflict, pulled) = await ReplayAsync(project, bundle, home, null, cancellationToken).ConfigureAwait(false);
|
||||
if (conflict is null) project.WriteSync(project.ReadSync() with { Conflict = null });
|
||||
}
|
||||
else
|
||||
{
|
||||
var bundle = project.ReadBundle();
|
||||
var head = await home.HeadAsync(projectId, cancellationToken).ConfigureAwait(false);
|
||||
if (head.Id != bundle.Workspace!.Base?.VersionId) (_, _, pulled) = await ReplayAsync(project, bundle, home, null, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
@@ -283,16 +305,20 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Settles the conflict a pull or push stopped at. <see cref="ConflictChoice.KeepMine"/> keeps this workspace's values
|
||||
/// (but for the members in <paramref name="takeServer"/>), <see cref="ConflictChoice.TakeServer"/> takes the server's
|
||||
/// wherever both changed; either way the local versions are replayed on the home's head and wait to be pushed.
|
||||
/// Settles the conflict a pull or push stopped at, as the user was shown it (<paramref name="shown"/>, else the one
|
||||
/// recorded). <see cref="ConflictChoice.KeepMine"/> keeps this workspace's values (but for the members in
|
||||
/// <paramref name="takeServer"/>), <see cref="ConflictChoice.TakeServer"/> takes the server's on the settings listed;
|
||||
/// either way the local versions are replayed on the home's head and wait to be pushed. A choice applies only to the
|
||||
/// conflicts listed: when what differs now is not that (the home moved on, or this side was edited meanwhile), nothing
|
||||
/// is settled, and the conflict found now is recorded and returned to be asked again. Taking the server's first saves
|
||||
/// uncommitted changes as a version, so the values it replaces are kept (its file in <c>versions/.replaced</c>).
|
||||
/// <see cref="ConflictChoice.SaveBoth"/> saves the working state as a version, sends this workspace's versions to a new
|
||||
/// project at the home (<paramref name="saveBothName"/>, else this project's name with a number) made from the base,
|
||||
/// and moves this project onto the home's head.
|
||||
/// </summary>
|
||||
/// <exception cref="HomeUnavailableException">The home cannot be reached; the conflict stays.</exception>
|
||||
public async Task<WorkspaceSyncResult> ResolveAsync(Guid projectId, IProjectHome home, ConflictChoice choice, IReadOnlySet<string>? takeServer = null,
|
||||
string? saveBothName = null, CancellationToken cancellationToken = default)
|
||||
string? saveBothName = null, WorkspaceConflict? shown = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(home);
|
||||
var project = Existing(projectId);
|
||||
@@ -301,20 +327,26 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
|
||||
await SettleAsync(project, home, cancellationToken).ConfigureAwait(false);
|
||||
var bundle = project.ReadBundle();
|
||||
var sync = project.ReadSync();
|
||||
if (sync.Conflict is null) return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], []);
|
||||
if (sync.Conflict is not { } recorded) return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], []);
|
||||
if (choice == ConflictChoice.SaveBoth)
|
||||
return await SaveBothAsync(project, bundle, home, saveBothName, cancellationToken).ConfigureAwait(false);
|
||||
if (choice == ConflictChoice.TakeServer && !bundle.IsReadOnly) CommitLocked(project, bundle, null, null);
|
||||
IReadOnlySet<string> takeTheirs = choice == ConflictChoice.TakeServer ? AllPaths : takeServer ?? new HashSet<string>();
|
||||
var (_, conflict, pulled) = await ReplayAsync(project, bundle, home, takeTheirs, cancellationToken).ConfigureAwait(false);
|
||||
ProjectSettings? kept = null;
|
||||
var (_, conflict, pulled) = await ReplayAsync(project, bundle, home, takeTheirs, cancellationToken, shown ?? recorded, k => kept = k).ConfigureAwait(false);
|
||||
if (conflict is null) project.WriteSync(project.ReadSync() with { Conflict = null });
|
||||
return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], pulled);
|
||||
return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], pulled)
|
||||
{
|
||||
Kept = conflict is null ? kept : null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Brings version <paramref name="versionId"/> back as the working state (from this workspace, else from
|
||||
/// <paramref name="home"/>); the next version saved is a "restore" of it. Uncommitted changes are saved as a version
|
||||
/// first, so a restore loses nothing.
|
||||
/// <paramref name="home"/>); the next version saved is a "restore" of it while the working state still holds it.
|
||||
/// Uncommitted changes are saved as a version first, also while a conflict waits (settling it replays that version
|
||||
/// too), so a restore loses nothing.
|
||||
/// </summary>
|
||||
/// <exception cref="ProjectException">Neither this workspace nor the home has the version.</exception>
|
||||
public async Task<ProjectSettings> RestoreAsync(Guid projectId, Guid versionId, IProjectHome? home = null, CancellationToken cancellationToken = default)
|
||||
@@ -332,11 +364,15 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
|
||||
if (version is null) throw new ProjectException(ProjectError.NotFound, $"Version {versionId} of project {projectId} is not known here.");
|
||||
var bundle = project.ReadBundle();
|
||||
if (bundle.IsReadOnly) throw ReadOnly(projectId);
|
||||
if (project.ReadSync().Conflict is null) CommitLocked(project, bundle, null, null);
|
||||
CommitLocked(project, bundle, null, null);
|
||||
var media = project.ReadMedia();
|
||||
var working = ProjectSettings.FromJson(ProjectContent.Place(version.SettingsJson, media?.Photos ?? NoFrames), allowNewer: true);
|
||||
project.WriteBundle(project.ReadBundle() with { Project = working, Saved = DateTimeOffset.UtcNow });
|
||||
project.WriteSync(project.ReadSync() with { RestoredFrom = versionId });
|
||||
var photos = media?.Photos ?? NoFrames;
|
||||
var working = ProjectSettings.FromJson(ProjectContent.Place(version.SettingsJson, photos), allowNewer: true);
|
||||
bundle = project.ReadBundle();
|
||||
project.WriteBundle(bundle with { Project = working, Saved = DateTimeOffset.UtcNow });
|
||||
// Restoring what the project already is changes nothing: the next version is no restore.
|
||||
if (Tip(project, bundle, Unpushed(project, bundle)) is not { } tip || !ProjectContent.SameChoices(tip.SettingsJson, version.SettingsJson, photos))
|
||||
project.WriteSync(project.ReadSync() with { RestoredFrom = versionId });
|
||||
return working;
|
||||
}
|
||||
}
|
||||
@@ -360,12 +396,16 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
|
||||
/// <summary>
|
||||
/// Pulls the home's versions after the base and replays the local ones and the working state on its head. With
|
||||
/// <paramref name="takeTheirs"/> null a conflict is recorded and nothing else moves (the versions pulled are kept as
|
||||
/// files; the base only moves onto local commits the home already holds); with a choice the conflicts are settled by it.
|
||||
/// Written in steps a cut can stop at any point (<see cref="WorkspaceReplay"/>): the journal, the replayed versions, the
|
||||
/// base with the merged working state (the commit point), then the old versions set aside and the held ones adopted.
|
||||
/// files; the base only moves onto local commits the home already holds); with a choice the conflicts are settled by it,
|
||||
/// only when they are those <paramref name="shown"/> lists (others are recorded and asked about first, as without a
|
||||
/// choice). Taking the server's everywhere tells <paramref name="kept"/> the working state as keeping this side's values
|
||||
/// would have left it (placed). Written in steps a cut can stop at any point (<see cref="WorkspaceReplay"/>): the journal,
|
||||
/// the replayed versions, the base with the merged working state (the commit point), then the old versions set aside and
|
||||
/// the held ones adopted.
|
||||
/// </summary>
|
||||
private async Task<(ProjectBundle Bundle, WorkspaceConflict? Conflict, IReadOnlyList<ProjectVersion> Pulled)> ReplayAsync(WorkspaceProject project,
|
||||
ProjectBundle bundle, IProjectHome home, IReadOnlySet<string>? takeTheirs, CancellationToken cancellationToken)
|
||||
ProjectBundle bundle, IProjectHome home, IReadOnlySet<string>? takeTheirs, CancellationToken cancellationToken, WorkspaceConflict? shown = null,
|
||||
Action<ProjectSettings>? kept = null)
|
||||
{
|
||||
var basis = await BaseVersionAsync(project, bundle, home, cancellationToken).ConfigureAwait(false);
|
||||
// A local commit the home stored although its answer was lost comes back among the home's versions, and the commits
|
||||
@@ -415,12 +455,15 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
|
||||
var choice = takeTheirs is EveryPath ? null : takeTheirs;
|
||||
List<ProjectSettings> mine = [.. locals.Select(v => ProjectMerge.Read(ProjectContent.Rebind(v.SettingsJson, photos)))];
|
||||
var replay = ProjectMerge.Replay(basisSettings, mine, headSettings, choice);
|
||||
// This side's values wherever both changed: what taking the server's everywhere replaces (the editor's undo of it).
|
||||
var keptTip = replay.Commits.Count > 0 ? replay.Commits[^1] : headSettings;
|
||||
if (takeTheirs is EveryPath) replay = ProjectMerge.Replay(basisSettings, mine, headSettings, Paths(replay.Conflicts));
|
||||
var newTip = replay.Commits.Count > 0 ? replay.Commits[^1] : headSettings;
|
||||
var merge = ProjectMerge.MergeWithConflicts(tip, working, newTip, choice);
|
||||
var keptMine = takeTheirs is EveryPath ? ProjectMerge.MergeWithConflicts(tip, working, keptTip).Merged : merge.Merged;
|
||||
if (takeTheirs is EveryPath) merge = ProjectMerge.MergeWithConflicts(tip, working, newTip, Paths(merge.Conflicts));
|
||||
var differences = replay.Conflicts.Concat(merge.Conflicts).DistinctBy(d => d.Path).ToList();
|
||||
if (differences.Count > 0 && takeTheirs is null)
|
||||
if (differences.Count > 0 && (takeTheirs is null || (shown is not null && !SameDifferences(shown.Differences, differences))))
|
||||
{
|
||||
var conflict = new WorkspaceConflict(head.Id, head.Number, basis?.Id ?? Guid.Empty, [.. locals.Select(v => v.Id)], differences, DateTimeOffset.UtcNow);
|
||||
if (ours.Count > 0)
|
||||
@@ -472,6 +515,8 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
|
||||
Step("replay.base");
|
||||
await FinishReplayAsync(project, journal, held, home, cancellationToken).ConfigureAwait(false);
|
||||
project.WriteSync(project.ReadSync() with { Replay = null });
|
||||
if (takeTheirs is EveryPath && kept is not null)
|
||||
kept(ProjectSettings.FromJson(ProjectContent.Place(ProjectContent.Canonical(keptMine.ToJson()), photos), allowNewer: true));
|
||||
return (moved, null, theirs);
|
||||
}
|
||||
|
||||
@@ -695,13 +740,17 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
|
||||
: (workspace.Base?.VersionId, workspace.Base?.Number ?? 0, workspace.Base?.ContentHash);
|
||||
if (hash == tipHash || SameAsTip(project, bundle, locals, portable, media)) return null;
|
||||
var sync = project.ReadSync();
|
||||
// A restore taken back since (Undo, Ctrl+Z) no longer shows in the working state: that version is no restore.
|
||||
var restoredFrom = sync.RestoredFrom is { } restored && StillRestores(project, restored, Tip(project, bundle, locals), portable, media?.Photos ?? NoFrames)
|
||||
? sync.RestoredFrom
|
||||
: null;
|
||||
var version = new ProjectVersion
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
ProjectId = project.ProjectId,
|
||||
ParentId = tipId,
|
||||
Number = tipNumber + 1,
|
||||
Kind = sync.RestoredFrom is null ? ProjectVersionKind.Commit : ProjectVersionKind.Restore,
|
||||
Kind = restoredFrom is null ? ProjectVersionKind.Commit : ProjectVersionKind.Restore,
|
||||
Author = author,
|
||||
ClientName = clientName,
|
||||
Note = string.IsNullOrWhiteSpace(note) ? null : note.Trim(),
|
||||
@@ -709,7 +758,7 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
|
||||
ContentHash = hash,
|
||||
SettingsFormat = ProjectContent.FormatOf(portable),
|
||||
FramesVersion = media?.FramesVersion ?? workspace.Media.FramesVersion,
|
||||
RestoredFrom = sync.RestoredFrom,
|
||||
RestoredFrom = restoredFrom,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
project.WriteVersion(version);
|
||||
@@ -769,10 +818,34 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName)
|
||||
private static bool SameAsTip(WorkspaceProject project, ProjectBundle bundle, List<ProjectVersion> locals, string portable, WorkspaceMedia? media)
|
||||
{
|
||||
if (media is null) return false;
|
||||
var tip = locals.Count > 0 ? locals[^1] : bundle.Workspace!.Base is { } basis ? project.Versions().FirstOrDefault(v => v.Id == basis.VersionId) : null;
|
||||
return tip is not null && ProjectContent.SameChoices(tip.SettingsJson, portable, media.Photos);
|
||||
return Tip(project, bundle, locals) is { } tip && ProjectContent.SameChoices(tip.SettingsJson, portable, media.Photos);
|
||||
}
|
||||
|
||||
/// <summary>The last version: the newest local one, else the base (null when this workspace keeps neither).</summary>
|
||||
private static ProjectVersion? Tip(WorkspaceProject project, ProjectBundle bundle, List<ProjectVersion> locals) =>
|
||||
locals.Count > 0 ? locals[^1] : bundle.Workspace!.Base is { } basis ? project.Versions().FirstOrDefault(v => v.Id == basis.VersionId) : null;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the working state <paramref name="portable"/>, saved as a version on <paramref name="parent"/>, still restores
|
||||
/// version <paramref name="restoredId"/>: that version differs from the parent, and the working state holds its values
|
||||
/// wherever it does. A restore taken back since (the toast's Undo, Ctrl+Z) is no restore; one whose version this
|
||||
/// workspace no longer keeps counts as recorded.
|
||||
/// </summary>
|
||||
private static bool StillRestores(WorkspaceProject project, Guid restoredId, ProjectVersion? parent, string portable, FramePhotos photos)
|
||||
{
|
||||
if (project.Versions().FirstOrDefault(v => v.Id == restoredId) is not { } restored) return true;
|
||||
var then = ProjectContent.Rebind(restored.SettingsJson, photos);
|
||||
var before = parent is null ? ProjectContent.ToPortable(new ProjectSettings(), photos) : ProjectContent.Rebind(parent.SettingsJson, photos);
|
||||
if (then == before) return false;
|
||||
var now = ProjectContent.Rebind(portable, photos);
|
||||
var merge = ProjectMerge.MergeWithConflicts(ProjectMerge.Read(before), ProjectMerge.Read(now), ProjectMerge.Read(then));
|
||||
return merge.Conflicts.Count == 0 && ProjectContent.Canonical(merge.Merged.ToJson()) == ProjectContent.Canonical(now);
|
||||
}
|
||||
|
||||
/// <summary>Whether two conflicts list the same settings with the same values on each side (in any order).</summary>
|
||||
private static bool SameDifferences(IReadOnlyList<SettingsDifference> shown, IReadOnlyList<SettingsDifference> found) =>
|
||||
shown.Count == found.Count && shown.ToHashSet().SetEquals(found);
|
||||
|
||||
/// <summary>The local versions after the base, oldest first, each the child of the one before (the base's for the first).</summary>
|
||||
private static List<ProjectVersion> Unpushed(WorkspaceProject project, ProjectBundle bundle)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user