Repository navigation
Compare branches by commit identity instead of t - #1905
Conversation
bplatz
left a comment
There was a problem hiding this comment.
Approving: comparing branches by commit identity is the right fix, and it catches the conflicts the old t cutoff missed. Please address the inline comments before merging.
The first three have one cause. A sync merge commit (the parent merged into its child) gets folded or replayed whole, so it carries the other branch's old values back. The comment on merge.rs has a repro and a suggested fix. The fourth is a doc line that no longer matches the code.
| let commit = load_commit_by_id(store, cid).await?; | ||
| if own.contains(cid) { | ||
| keys.extend( | ||
| commit.flakes.iter().map(|flake| { | ||
| ConflictKey::new(flake.s.clone(), flake.p.clone(), flake.g.clone()) | ||
| }), | ||
| ); | ||
| } | ||
| commits.push(commit); |
There was a problem hiding this comment.
Folding a sync merge in whole brings back the target's old values.
Say dev syncs main in through a real merge commit S, and main then changes one of those keys again. Merging dev back re-asserts main's old value. It reports no conflict, and the key ends up with both values.
S is in diff.source.commits but not in own, so this loop folds all of its flakes. On keys dev never touched, S's flakes are just copies of main's earlier change, and applying them to main's newer state brings that change back. Only S's flakes on keys that dev's own earlier commits changed are the merge's resolution.
Repro, using the helpers this PR adds to it_merge.rs:
#[tokio::test]
async fn merge_back_after_sync_keeps_the_targets_later_value() {
let fluree = FlureeBuilder::memory().build_memory();
fluree.create_ledger("mydb").await.unwrap();
insert_named(&fluree, "mydb:main", "ex:alice", "Alice").await;
fluree.create_branch("mydb", "dev", None, None).await.unwrap();
// A dev commit first, so the sync writes a real merge commit.
insert_named(&fluree, "mydb:dev", "ex:d1", "d1").await;
rename_alice(&fluree, "mydb:main", "v1").await;
fluree
.merge_branch("mydb", "main", Some("dev"), ConflictStrategy::default())
.await
.unwrap();
rename_alice(&fluree, "mydb:main", "v2").await;
fluree
.merge_branch("mydb", "dev", None, ConflictStrategy::Abort)
.await
.unwrap(); // 0 conflicts
// Actual: ["d1", "v1", "v2"]
assert_eq!(query_all_names(&fluree, "mydb:main").await, ["d1", "v2"]);
}One possible fix: keep a non-own commit's flakes only on keys that own commits earlier in the list touched. keys already builds up oldest-first, so:
let mut commit = load_commit_by_id(store, cid).await?;
if own.contains(cid) {
keys.extend(/* as now */);
} else {
// A merge of history the other side holds. Only its flakes on
// keys this side changed are its resolution; the rest copy the
// other side's changes.
commit.flakes.retain(|flake| {
keys.contains(&ConflictKey::new(
flake.s.clone(),
flake.p.clone(),
flake.g.clone(),
))
});
}With that change the repro passes, and the rest of it_merge still passes, including merge_back_keeps_a_resolution_the_sync_made. Rebase and preview have the same gap; see the comments on rebase.rs and merge_preview.rs.
| // Replay every commit on the branch's line, merges included: a | ||
| // merge the branch made carries how it resolved that merge. | ||
| let summaries = | ||
| scan_branch_commits(&branch_store, &diff.source.commits, &source_delta).await?; |
There was a problem hiding this comment.
Rebase has the same gap: it replays the sync merge whole.
Use the same setup as the merge-back repro on merge.rs, up to main's rename to v2, then rebase dev instead of merging it back:
// Fails with "Rebase aborted: 1 conflict(s)", although dev never touched alice.
fluree.rebase_branch("mydb", "dev", ConflictStrategy::Abort).await.unwrap();
// With TakeBranch it succeeds, but dev ends on ["d1", "v1"]: main's v2 is lost.S's flakes on alice are main's old rename. They conflict with source_delta (main's later rename), and under take-branch they win. Filtering the merge's flakes down to keys that dev's own earlier commits touched, as suggested on merge.rs, fixes both cases.
rebase_after_sync_detects_conflict doesn't cover this. dev has no commits of its own when it syncs, so the sync is a fast-forward and no merge commit is replayed. Adding a dev commit before the sync makes the test replay S.
| match (&diff, &source_head) { | ||
| (Some(diff), _) => { | ||
| let (_, net, ns_delta) = if need_changes || need_validation { | ||
| delta_keys_and_changes_of(&source_store, &diff.source.commits, true).await? |
There was a problem hiding this comment.
After a sync, the change list shows the target's own changes.
Same cause again: folding all of diff.source.commits includes the sync merge. After dev syncs main in, previewing dev → main lists main's own changes as dev's:
insert_named(&fluree, "mydb:main", "ex:a", "a").await;
fluree.create_branch("mydb", "dev", None, None).await.unwrap();
insert_named(&fluree, "mydb:dev", "ex:d1", "d1").await;
for n in 2..=4 {
insert_named(&fluree, "mydb:main", &format!("ex:m{n}"), &format!("m{n}")).await;
}
fluree
.merge_branch("mydb", "main", Some("dev"), ConflictStrategy::default())
.await
.unwrap();
let opts = MergePreviewOpts { include_changes: true, ..Default::default() };
let preview = fluree.merge_preview_with("mydb", "dev", None, opts).await.unwrap();
// changes.entries subjects: ex:d1, ex:m2, ex:m3, ex:m4. Expected: ex:d1 only.In a merge-request view, every "update branch" would pull upstream's changes into the branch's diff. Validation stages this same change set, so fixing it together with merge keeps preview and merge in agreement.
The same filter would also let this call and the delta_keys_of(.., own) call below share one pass, as merge's collect_commit_data does. Today a default preview reads each source commit twice.
| Replays a branch's unique commits on top of the source branch's current HEAD. This brings the branch up to date with upstream changes. The `main` branch cannot be rebased. | ||
| Replays a branch's unique commits on top of the source branch's current HEAD. This brings the branch up to date with upstream changes, and rewrites the branch's commits to do it. To keep the branch's commits as they are, merge the source into the branch instead. The `main` branch cannot be rebased. | ||
|
|
||
| A branch that already merged its source in is rebased on its own commits only. The merge that brought the source in is left out, because its changes came from the source. |
There was a problem hiding this comment.
This no longer matches the code. Since cc3fec0, prepare_rebase replays diff.source.commits, and that list includes the merge that brought the source in (rebase.rs around line 413). Once the rebase comment is settled, three places should say the same thing: this line, the "Pass 1: read the branch's own commits" comment in rebase.rs, and the rebase bullet in docs/cli/server-integration.md ("that merge is not one of them").
|
This PR fixes #1912 (the status section of that issue says so), but the PR description has no |
Fixes #1912
Summary
Merging a branch into its parent a second time silently missed conflicts. Both sides could change the same fact and the merge would report none, so
abortwent through andtake-branchleft both values in place.Every branch numbers its commits from its own fork point, so a
tfrom one branch means nothing on another. Merge found the common ancestor and then cut each side's commit walk atancestor.t. After an earlier merge, that ancestor sits on the other branch's clock, and the cutoff drops commits that came after it. Rebase and merge preview cut the same way.The two sides are now compared by commit identity. Along the way this fixes merging in directions that never worked: a parent into its child, one branch into a sibling, and
maininto a branch.What was broken
xis merged intodev, then both change the same fact, thenxis merged again. The base is thexcommit merged the first time, whosetis above every commitdevmade since, sodev's side came out empty.mainwas refused outright, by a guard that exists only to infer a default target. Any other parent failed with a storage error, because merge read both heads through the source's store. The same error hit two branches offmainonce both had commits. Merge preview read both stores, so it reported these merges as fine.tfalls.The fix
diff_branchesinfluree-db-corecompares two branches by identity. It follows each branch's line of first parents, and follows merge parents into the branches they brought in, so a commit an earlier merge brought in counts as one the branch already holds. It returns, per side:commits, every commit on that branch's line above the divergence. Folding them gives the branch's state, so a merge applies them and a rebase replays them.own, the subset carrying the branch's own changes. A merge whose merged-in history the other side already holds is left out, because its changes came from that side. This is what conflict detection compares.The split matters in both directions. Comparing everything reports conflicts on keys the other side never touched. Folding only
owndrops a merge commit's flakes, which are the strategy's resolution rather than a copy of what it merged: syncing withtake-sourceand merging back then resurrected the value the sync had resolved away.It also returns the commits the target lacks, parents first, and a
fast_forwardflag that is true only when the target's head is on the source's line.Merge, rebase and merge preview all use it, so a preview and the merge it previews now see the same divergence.
Merging in any direction
--targetis enough. The source needs a parent branch only when the target is inferred from it.Tests
abortand undertake-branch, a resolution surviving a merge back, a key only the source changed not conflicting with the target's earlier merge of it, a key the sync settled not conflicting again, a real conflict in each new direction resolved withtake-sourceandtake-branch, the new fast-forward case, and the round trip.abortandtake-branch.Each fix was confirmed by restoring the old behavior and watching the matching test fail.