From d2332687da1d12f6139ef8a04b48416d46d9daff Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Wed, 4 Nov 2026 23:09:30 +0100 Subject: [PATCH] fix(projects): address the line-end review StillRestores compared the restored version with the new version's parent as raw text, so a version an older Windows build stored with CRLF never matched the same choices written as LF, and a commit on a parent already holding the restored version was labelled a restore. It now compares them with SameChoices, like every other project text comparison. The LF line ends of the project writers were only checked through Environment.NewLine, which is LF on Linux CI already; the tests now assert the NewLine option of ProjectJson and IndentedJson directly, so reverting either fails on every OS. Co-Authored-By: Claude Opus 5.5 --- src/Chronos.Core/Project/ProjectSettings.cs | 2 +- src/Chronos.Data/Workspaces/WorkspaceSync.cs | 3 +- .../Project/ProjectContentTests.cs | 3 ++ .../Workspaces/WorkspaceMergeTests.cs | 28 +++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Chronos.Core/Project/ProjectSettings.cs b/src/Chronos.Core/Project/ProjectSettings.cs index 17ebc3f5..ccabb23f 100644 --- a/src/Chronos.Core/Project/ProjectSettings.cs +++ b/src/Chronos.Core/Project/ProjectSettings.cs @@ -329,7 +329,7 @@ public sealed record ProjectSettings public const int BaseVersion = 1; /// Layout of a project edited as JSON (): indented like , with LF line ends on every OS. - private static readonly JsonSerializerOptions IndentedJson = new() { WriteIndented = true, NewLine = "\n" }; + internal static readonly JsonSerializerOptions IndentedJson = new() { WriteIndented = true, NewLine = "\n" }; /// Member names as a project edited as JSON reads them (an older or hand-written file may use any casing). private static readonly JsonNodeOptions CaseInsensitive = new() { PropertyNameCaseInsensitive = true }; diff --git a/src/Chronos.Data/Workspaces/WorkspaceSync.cs b/src/Chronos.Data/Workspaces/WorkspaceSync.cs index 04e9e9ff..9cae9613 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceSync.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceSync.cs @@ -1479,7 +1479,8 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) 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; + // The same choices are no different, whatever line ends either was stored with (a CRLF version of an older Windows build). + if (ProjectContent.SameChoices(then, before, photos)) 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); diff --git a/tests/Chronos.Core.Tests/Project/ProjectContentTests.cs b/tests/Chronos.Core.Tests/Project/ProjectContentTests.cs index 5fbe2a3d..9de87d3b 100644 --- a/tests/Chronos.Core.Tests/Project/ProjectContentTests.cs +++ b/tests/Chronos.Core.Tests/Project/ProjectContentTests.cs @@ -351,6 +351,9 @@ public sealed class ProjectContentTests var frames = Frames(Photo(1), Photo(2), Photo(3)); var lf = ProjectContent.ToPortable(Positions([1], 2, 0), frames); var crlf = lf.Replace("\n", "\r\n", StringComparison.Ordinal); + // Every writer of project text names LF itself: on Linux Environment.NewLine is LF already, so only these fail there. + Assert.Equal("\n", ProjectJson.Default.Options.NewLine); + Assert.Equal("\n", ProjectSettings.IndentedJson.NewLine); Assert.False(lf.Contains('\r', StringComparison.Ordinal), "portable"); Assert.False(new ProjectSettings().ToJson().Contains('\r', StringComparison.Ordinal), "ToJson"); Assert.Equal(lf, ProjectContent.Canonical(crlf)); diff --git a/tests/Chronos.Data.Tests/Workspaces/WorkspaceMergeTests.cs b/tests/Chronos.Data.Tests/Workspaces/WorkspaceMergeTests.cs index 1df4b486..988740f8 100644 --- a/tests/Chronos.Data.Tests/Workspaces/WorkspaceMergeTests.cs +++ b/tests/Chronos.Data.Tests/Workspaces/WorkspaceMergeTests.cs @@ -142,6 +142,34 @@ public abstract class WorkspaceMergeTests(TestContext context) : DatabaseTest(co Assert.Equal(foreign.Id, (await fx.Home.HeadAsync(id, fx.Ct)).Id); } + [Test] + public async Task ARestoreOfAVersionTheBaseHoldsWithOtherLineEndsIsNoRestore() + { + var fx = await FixtureAsync(); + var projects = fx.Archive.Service.Projects; + var main = await projects.MainAsync(fx.Sequence.Id, fx.Ct); + var choices = new ProjectSettings { ExcludedFrames = [1], HeroFrame = 3 }; + // A version an older build stored on the other OS, then the same choices as this build writes them (the base). + var foreign = ProjectServiceTests.Foreign(ProjectServiceTests.Draft(await projects.HeadAsync(main.Id, fx.Ct), choices, fx.Frames)); + Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(main.Id, foreign, fx.Ct)).Outcome); + var same = ProjectServiceTests.Draft(foreign, choices, fx.Frames); + Assert.NotEqual(foreign.SettingsJson, same.SettingsJson); + Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(main.Id, same, fx.Ct)).Outcome); + + var (_, sync) = fx.Workspace(); + var project = await fx.CheckoutAsync(sync); + var id = project.ProjectId; + Assert.Equal(same.Id, project.ReadBundle().Workspace!.Base!.VersionId); + // The foreign version restored before a pull moved the base onto the same choices: the restore stays recorded. + await sync.RestoreAsync(id, foreign.Id, fx.Home, fx.Ct); + project.WriteSync(project.ReadSync() with { RestoredFrom = foreign.Id }); + + // Edited further and saved: the parent already holds the restored version, so the new version is a plain one. + await EditAsync(sync, id, s => s with { Exposure = s.Exposure with { ExposureOffsetEv = 0.4 } }, fx.Ct); + var next = Assert.NotNull(await sync.CommitAsync(id, cancellationToken: fx.Ct)); + Assert.Equal((ProjectVersionKind.Commit, (Guid?)null), (next.Kind, next.RestoredFrom)); + } + [Test] public async Task ABaseNamingAPhotoByItsHashReadsAsTheWorkingStateOnMediaThatHaveNotLearntIt() {