fix(projects): same project text on every OS; compare stored CRLF versions line-end-insensitively

Project text was indented with Environment.NewLine, so the same project
read as CRLF text on Windows and LF text on the Linux server and its
content hash differed. A Windows desktop pushing to a Linux server got
spurious Merge and External versions, Modified/Behind states with no
edits, empty commits, and renders and sends taken for other settings.

Every build now writes the project JSON with LF (ProjectJson and
IndentedJson). Versions already stored keep their CRLF text and hash, so
SameChoices reads CRLF as LF (lossless: raw line ends are JSON
whitespace) and the places that ask whether a text is a given version
use the new ProjectContent.MatchesHash, true for the hash of the LF or
the CRLF form. Integrity checks of stored and pushed versions stay exact.

Tests pin the LF default hash, cover both comparisons both ways, and
push versions with the other OS's line ends to an archive home and into
a workspace: no outside change, in sync, nothing to commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-04 22:48:27 +01:00
1 parent 863c6cf27b
commit 787f030060
15 files changed
+174 -25

No files matched your search

@@ -344,6 +344,39 @@ public sealed class ProjectContentTests
Assert.Equal(portable, ProjectContent.Place(portable, Frames(Photo(2))));
}
[Test]
public void TheTextAndItsHashAreTheSameOnEveryOs()
{
// Built with explicit "\r\n", not Environment.NewLine, so the test fails without the fix on Linux too.
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);
Assert.False(lf.Contains('\r', StringComparison.Ordinal), "portable");
Assert.False(new ProjectSettings().ToJson().Contains('\r', StringComparison.Ordinal), "ToJson");
Assert.Equal(lf, ProjectContent.Canonical(crlf));
Assert.Equal(lf, ProjectContent.ToPortable(crlf, NoFrames()));
// A version stored by a Windows build before (CRLF text, CRLF hash) holds what the LF text holds.
foreach (var photos in new[] { frames, NoFrames() })
{
Assert.True(ProjectContent.SameChoices(crlf, lf, photos));
Assert.True(ProjectContent.SameChoices(lf, crlf, photos));
}
Assert.True(ProjectContent.MatchesHash(lf, ProjectContent.Hash(crlf)));
Assert.True(ProjectContent.MatchesHash(crlf, ProjectContent.Hash(lf)));
Assert.True(ProjectContent.MatchesHash(lf, ProjectContent.Hash(lf).ToUpperInvariant()));
Assert.False(ProjectContent.MatchesHash(lf, ProjectContent.Hash(ProjectContent.ToPortable(Positions([2], 2, 0), frames))));
Assert.False(ProjectContent.MatchesHash(lf, null));
Assert.False(ProjectContent.MatchesHash(lf, "abc"));
// The integrity check stays exact.
Assert.True(ProjectContent.Verifies(crlf, ProjectContent.Hash(crlf)));
Assert.False(ProjectContent.Verifies(lf, ProjectContent.Hash(crlf)));
// Other choices are still other choices.
Assert.False(ProjectContent.SameChoices(crlf, ProjectContent.ToPortable(Positions([2], 2, 0), frames), frames));
}
private static FramePhotos NoFrames() => new([]);
[Test]
public void TextThatIsNoProjectIsRefused()
{
@@ -473,28 +473,38 @@ public sealed class ProjectSettingsTests
/// <summary>
/// SHA-256 of the default project's canonical text, as format-1 builds write it: none of phase 4's members, version 1.
/// The text is indented with the OS's line ends (CRLF on Windows, LF on Linux), so the pin is of the text with CRLF.
/// Builds before wrote it indented with the OS's line ends (CRLF on Windows, LF on Linux); this pin is of the CRLF text,
/// the hash such a Windows build stored.
/// </summary>
private const string DefaultProjectHash = "df0b0f50b113475ece3753a80df0be05cc281cdaa1fa08a73a438ea9161358e0";
/// <summary>SHA-256 of the same text with LF, as every build writes it now on every OS.</summary>
private const string DefaultProjectLfHash = "24e4c2d0379bfda6e42533d4687d69cca95bab72f2a49bd70e2fba267b66a3d1";
[Test]
public void ProjectsWithoutTheNewMembersKeepTheirTextAndHash()
{
var defaults = new ProjectSettings().ToJson();
Assert.Equal(DefaultProjectHash, ProjectContent.Hash(ProjectContent.Canonical(defaults).ReplaceLineEndings("\r\n")));
Assert.Equal(OldBuild.Write(new OldProject()), defaults);
Assert.False(defaults.Contains('\r', StringComparison.Ordinal), "line ends");
Assert.Equal(DefaultProjectLfHash, ProjectContent.Hash(ProjectContent.Canonical(defaults)));
// A version a Windows build stored before still names this text.
Assert.True(ProjectContent.MatchesHash(ProjectContent.Canonical(defaults), DefaultProjectHash));
Assert.Equal(DefaultProjectHash, ProjectContent.Hash(ProjectContent.Canonical(defaults).Replace("\n", "\r\n", StringComparison.Ordinal)));
// The old build wrote the OS's line ends (OldJson leaves NewLine unset): the same text but for those.
Assert.Equal(OldBuild.Write(new OldProject()).ReplaceLineEndings("\n"), defaults);
Assert.Contains("\"version\": 1", defaults);
foreach (var member in new[] { "rotate", "mirror", "flip", "straighten", "\"look\"" })
Assert.False(defaults.Contains(member, StringComparison.Ordinal), member);
// A project written by a format-1 build reads and writes back the same, byte for byte.
// A project written by a format-1 build reads and writes back the same, byte for byte but for line ends.
var older = new ProjectSettings
{
Camera = new CameraSettings { Crop = [0.1, 0.2, 0.5, 0.6], Aspect = new Auto<string>().WithManual("9:16"), Keyframes = [new CameraKey(3, 0.4, 0.5, 1.5, 2)] },
Exposure = new ExposureSettings { ExposureOffsetEv = 0.3 },
}.ToJson();
Assert.Equal(older, ProjectContent.Canonical(older));
Assert.Equal(older, OldBuild.Write(OldBuild.Read(older)));
Assert.Equal(older, OldBuild.Write(OldBuild.Read(older)).ReplaceLineEndings("\n"));
Assert.Equal(older, ProjectContent.Canonical(older.Replace("\n", "\r\n", StringComparison.Ordinal)));
Assert.Equal(ProjectSettings.BaseVersion, ProjectContent.FormatOf(older));
}
@@ -549,8 +559,10 @@ public sealed class ProjectSettingsTests
// A format-1 build keeps them as members it does not know, and writes them where they were.
var old = OldBuild.Read(canonical);
Assert.True(old.Camera.Extra!.ContainsKey("rotate") && old.Extra!.ContainsKey("look"));
Assert.Equal(canonical, OldBuild.Write(old));
Assert.Equal(ProjectContent.Hash(canonical), ProjectContent.Hash(OldBuild.Write(old)));
// In the OS's line ends (CRLF on Windows): the same text and content hash but for those.
Assert.Equal(canonical, OldBuild.Write(old).ReplaceLineEndings("\n"));
Assert.True(ProjectContent.MatchesHash(OldBuild.Write(old), ProjectContent.Hash(canonical)));
Assert.True(ProjectContent.SameChoices(OldBuild.Write(old), canonical, new FramePhotos([])));
var camera = System.Text.Json.Nodes.JsonNode.Parse(canonical)!["camera"]!.AsObject().Select(p => p.Key).ToList();
Assert.SequenceEqual(["crop", "aspect", "offsetX", "offsetY", "keyframes", "rotate", "straighten"], camera);
}