Fix common ancestor finding: iterate backwards to find deepest ancestor

getPath returns root-first, so iterating forward found root as common
ancestor instead of the deepest shared node. Now iterates backwards.
This commit is contained in:
Mario Zechner 2025-12-29 22:26:31 +01:00
parent a602e8aba8
commit 92947a3dc4

View file

@ -91,14 +91,15 @@ export function collectEntriesForBranchSummary(
return { entries: [], commonAncestorId: null }; return { entries: [], commonAncestorId: null };
} }
// Find common ancestor // Find common ancestor (deepest node that's on both paths)
const oldPath = new Set(session.getPath(oldLeafId).map((e) => e.id)); const oldPath = new Set(session.getPath(oldLeafId).map((e) => e.id));
const targetPath = session.getPath(targetId); const targetPath = session.getPath(targetId);
// targetPath is root-first, so iterate backwards to find deepest common ancestor
let commonAncestorId: string | null = null; let commonAncestorId: string | null = null;
for (const entry of targetPath) { for (let i = targetPath.length - 1; i >= 0; i--) {
if (oldPath.has(entry.id)) { if (oldPath.has(targetPath[i].id)) {
commonAncestorId = entry.id; commonAncestorId = targetPath[i].id;
break; break;
} }
} }