fix(projects): one photo merges as one, newer settings are never merged

A workspace copied before the home hashed or converted a photo named it
differently from the versions the home made since, so a pull merged the
same photo as two: frame edits were silently undone and Keep mine placed
the library's value. Before replaying, the workspace media now learn the
home's hashes (same frame id, size and capture time) and conversion
aliases, so every side names each photo alike. Rebind also gives a
renamed entry the occurrence a placement looks for, so positions on
burst photos keep applying once hashed, and folds entries of one photo
(in Rebind and in the merge's keyed form) instead of keeping one.

Settings of a newer Chronos are no longer merged member by member with
this one's: ProjectMerge refuses when both sides changed. A workspace
pull keeps its local versions and working state untouched and records a
conflict of format that only Save both settles (into a new project of
this format); with nothing local it takes the newer head read-only.
Main's catch-up leaves a newer head and settings written meanwhile as
they are.

When Main's catch-up merge overrides values another writer stored, that
writer's settings are kept as an External version before the Merge one,
so nothing is lost from the history.

Unknown members inside Auto values, camera keyframes and curve points,
and on pending entries once placed, are kept like ProjectSettings' Extra.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 10:05:27 +01:00
1 parent 50e28fd63d
commit eac4a36a7b
12 files changed
+905 -38

No files matched your search

+78 -4
View File
@@ -57,9 +57,12 @@ public static class ProjectContent
/// <summary>
/// The portable text with each pending frame whose photo <paramref name="frames"/> hold named as they hold it now: an
/// encoding a conversion replaced by the one the frame holds (<see cref="PhotoAlias"/>, also its undo), a photo parked
/// before it was hashed by its SHA-256, size and capture time. The same choices on the same photos then read as the same
/// text (<see cref="SameChoices"/>), whichever encoding or hashing state they were saved against; a placement finds the
/// same frames either way. Canonical when anything is renamed; the text as it is otherwise, and for a newer format.
/// before it was hashed by its SHA-256, size and capture time, and which copy of that photo it is among the frames
/// holding it under that name (a photo of a burst told apart by its order while unhashed is unique once hashed). Entries
/// that name one photo once renamed become one (<see cref="Fold"/>). The same choices on the same photos then read as
/// the same text (<see cref="SameChoices"/>), whichever encoding or hashing state they were saved against; a placement
/// finds the same frames either way. Canonical when anything is renamed or folded; the text as it is otherwise, and for
/// a newer format.
/// </summary>
/// <exception cref="JsonException">The text is not a project object.</exception>
public static string Rebind(string portableJson, FramePhotos frames)
@@ -80,19 +83,90 @@ public static class ProjectContent
var occurrence = entry["occurrence"] is JsonValue o && o.TryGetValue<int>(out var n) && n > 0 ? n : 0;
// Only a frame that is hashed names a photo better than the entry does.
if (frames.IndexOf(new FramePhoto(sha, size, captured), occurrence) is not { } at || frames[at] is not { IsHashed: true } held) continue;
// The copy a placement looks for under the new name: among the frames holding that SHA-256, not those of that size and time.
var copy = frames.Occurrence(at);
var sameTime = held.CapturedAt is { } heldAt
? captured is { } entryAt && FramePhoto.Millisecond(heldAt) == FramePhoto.Millisecond(entryAt)
: captured is null;
if (string.Equals(held.Sha256, sha, StringComparison.OrdinalIgnoreCase) && held.Size == size && sameTime) continue;
if (string.Equals(held.Sha256, sha, StringComparison.OrdinalIgnoreCase) && held.Size == size && sameTime && copy == occurrence) continue;
entry["sha256"] = held.Sha256!.ToLowerInvariant();
entry["size"] = held.Size;
if (held.CapturedAt is { } heldTime) entry["capturedAt"] = heldTime;
else entry.Remove("capturedAt");
if (copy > 0) entry["occurrence"] = copy;
else entry.Remove("occurrence");
renamed = true;
}
renamed |= FoldAlike(pending);
return renamed ? Canonical(project.ToJsonString()) : portableJson;
}
/// <summary>
/// The photo a pending entry names and which copy of it: "sha:&lt;sha-256&gt;#&lt;occurrence&gt;", or
/// "photo:&lt;size&gt;@&lt;capture ticks&gt;#&lt;occurrence&gt;" for a photo not hashed; null for an entry naming no photo.
/// Entries of one key are one photo's positions (<see cref="Fold"/>).
/// </summary>
public static string? PhotoKey(JsonNode? entry)
{
if (entry is not JsonObject e) return null;
var occurrence = e["occurrence"] is JsonValue o && o.TryGetValue<int>(out var n) ? n : 0;
if (e["sha256"] is JsonValue s && s.TryGetValue<string>(out var sha) && sha.Length > 0)
return string.Create(CultureInfo.InvariantCulture, $"sha:{sha.ToLowerInvariant()}#{occurrence}");
var size = e["size"] is JsonValue z && z.TryGetValue<long>(out var bytes) ? bytes : 0;
return e["capturedAt"] is JsonValue c && c.TryGetValue<DateTime>(out var at)
? string.Create(CultureInfo.InvariantCulture, $"photo:{size}@{at.Ticks}#{occurrence}")
: null;
}
/// <summary>
/// Folds <paramref name="entry"/> into <paramref name="into"/>, two pending entries of one photo (saved under two names,
/// alike since): excluded or hero when either is, the keyframes of both (each once), and the members this Chronos does not
/// know of both (those of <paramref name="into"/> where both have one). Nothing either held is dropped.
/// </summary>
public static void Fold(JsonObject into, JsonObject entry)
{
ArgumentNullException.ThrowIfNull(into);
ArgumentNullException.ThrowIfNull(entry);
if (Flag(entry, "excluded")) into["excluded"] = true;
if (Flag(entry, "hero")) into["hero"] = true;
if (entry["keyframes"] is JsonArray { Count: > 0 } keys)
{
if (into["keyframes"] is not JsonArray mine) into["keyframes"] = mine = new JsonArray();
foreach (var key in keys.ToList())
{
if (!mine.Any(k => JsonNode.DeepEquals(k, key))) mine.Add(key?.DeepClone());
}
}
foreach (var (name, value) in entry.ToList())
{
if (!ProjectSettings.PendingMembers.Contains(name) && !into.ContainsKey(name)) into[name] = value?.DeepClone();
}
}
/// <summary>Folds the pending entries naming one photo into the first of them (<see cref="Fold"/>); whether any were.</summary>
private static bool FoldAlike(JsonArray pending)
{
var first = new Dictionary<string, JsonObject>(StringComparer.Ordinal);
var folded = new List<JsonNode>();
foreach (var entry in pending.OfType<JsonObject>())
{
if (PhotoKey(entry) is not { } key) continue;
if (first.TryGetValue(key, out var into))
{
Fold(into, entry);
folded.Add(entry);
}
else
{
first[key] = entry;
}
}
foreach (var entry in folded) pending.Remove(entry);
return folded.Count > 0;
}
private static bool Flag(JsonObject entry, string name) => entry[name] is JsonValue value && value.TryGetValue<bool>(out var set) && set;
/// <summary>
/// Whether two portable texts hold the same choices on the frames of <paramref name="frames"/>: the same text, or the
/// same once their pending frames are named as the frames hold their photos now (<see cref="Rebind"/>).