Skip to content

Commit

Permalink
view: add support for merging git ref targets
Browse files Browse the repository at this point in the history
When there are two concurrent operations, we would resolve conflicting
updates of git refs quite arbitrarily before this change. This change
introduces a new `refs` module with a function for doing a 3-way merge
of ref targets. For example, if both sides moved a ref forward but by
different amounts, we pick the descendant-most target. If we can't
resolve it, we leave it as a conflict. That's fine to do for git refs
because they can be resolved by simply running `jj git refresh` to
import refs again (the underlying git repo is the source of truth).

As with the previous change, I'm doing this now because mostly because
it is a good stepping stone towards branch support (issue libfuse#21). We'll
soon use the same 3-way merging for updating the local branch
definition (once we add that) when a branch changes in the git repo or
on a remote.
  • Loading branch information
martinvonz committed Jul 25, 2021
1 parent 0aa738a commit 6b1ccd4
Show file tree
Hide file tree
Showing 8 changed files with 528 additions and 98 deletions.
6 changes: 5 additions & 1 deletion lib/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl IndexPosition {
pub const MAX: Self = IndexPosition(u32::MAX);
}

#[derive(Clone)]
#[derive(Clone, Copy)]
pub enum IndexRef<'a> {
Readonly(&'a ReadonlyIndex),
Mutable(&'a MutableIndex),
Expand Down Expand Up @@ -1470,6 +1470,10 @@ impl ReadonlyIndex {
}))
}

pub fn as_index_ref(self: &ReadonlyIndex) -> IndexRef {
IndexRef::Readonly(self)
}

pub fn num_commits(&self) -> u32 {
CompositeIndex(self).num_commits()
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub mod op_heads_store;
pub mod op_store;
pub mod operation;
pub mod protos;
pub mod refs;
pub mod repo;
pub mod repo_path;
pub mod revset;
Expand Down
9 changes: 9 additions & 0 deletions lib/src/op_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ impl RefTarget {
RefTarget::Conflict { removes: _, adds } => adds.contains(needle),
}
}

pub fn removes(&self) -> Vec<CommitId> {
match self {
RefTarget::Normal(_) => {
vec![]
}
RefTarget::Conflict { removes, adds: _ } => removes.clone(),
}
}
}

/// Represents the way the repo looks at a given time, just like how a Tree
Expand Down
109 changes: 109 additions & 0 deletions lib/src/refs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::index::IndexRef;
use crate::op_store::RefTarget;
use crate::store::CommitId;

pub fn merge_ref_targets(
index: IndexRef,
left: Option<&RefTarget>,
base: Option<&RefTarget>,
right: Option<&RefTarget>,
) -> Option<RefTarget> {
if left == base || left == right {
right.cloned()
} else if base == right {
left.cloned()
} else {
let mut adds = vec![];
let mut removes = vec![];
if let Some(left) = left {
adds.extend(left.adds());
removes.extend(left.removes());
}
if let Some(base) = base {
// Note that these are backwards (because the base is subtracted).
adds.extend(base.removes());
removes.extend(base.adds());
}
if let Some(right) = right {
adds.extend(right.adds());
removes.extend(right.removes());
}

while let Some((maybe_remove_index, add_index)) =
find_pair_to_remove(index, &adds, &removes)
{
if let Some(remove_index) = maybe_remove_index {
removes.remove(remove_index);
}
adds.remove(add_index);
}

if adds.is_empty() {
None
} else if adds.len() == 1 && removes.is_empty() {
Some(RefTarget::Normal(adds[0].clone()))
} else {
Some(RefTarget::Conflict { removes, adds })
}
}
}

fn find_pair_to_remove(
index: IndexRef,
adds: &[CommitId],
removes: &[CommitId],
) -> Option<(Option<usize>, usize)> {
// Removes pairs of matching adds and removes.
for (add_index, add) in adds.iter().enumerate() {
for (remove_index, remove) in removes.iter().enumerate() {
if add == remove {
return Some((Some(remove_index), add_index));
}
}
}

// If a "remove" is an ancestor of two different "adds" and one of the
// "adds" is an ancestor of the other, then pick the descendant.
for (add_index1, add1) in adds.iter().enumerate() {
for (add_index2, add2) in adds.iter().enumerate().skip(add_index1 + 1) {
let first_add_is_ancestor;
if add1 == add2 || index.is_ancestor(add1, add2) {
first_add_is_ancestor = true;
} else if index.is_ancestor(add2, add1) {
first_add_is_ancestor = false;
} else {
continue;
}
if removes.is_empty() {
if first_add_is_ancestor {
return Some((None, add_index1));
} else {
return Some((None, add_index2));
}
}
for (remove_index, remove) in removes.iter().enumerate() {
if first_add_is_ancestor && index.is_ancestor(remove, add1) {
return Some((Some(remove_index), add_index1));
} else if !first_add_is_ancestor && index.is_ancestor(remove, add2) {
return Some((Some(remove_index), add_index2));
}
}
}
}

None
}
3 changes: 2 additions & 1 deletion lib/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,8 @@ impl MutableRepo {
self.index.merge_in(base_repo.index());
self.index.merge_in(other_repo.index());

self.view.merge(&base_repo.view, &other_repo.view);
self.view
.merge(self.index.as_index_ref(), &base_repo.view, &other_repo.view);
self.enforce_view_invariants();

self.invalidate_evolution();
Expand Down
39 changes: 18 additions & 21 deletions lib/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

use std::collections::{BTreeMap, HashSet};

use crate::index::IndexRef;
use crate::op_store;
use crate::op_store::RefTarget;
use crate::refs::merge_ref_targets;
use crate::store::CommitId;

pub struct View {
Expand Down Expand Up @@ -92,7 +94,7 @@ impl View {
&mut self.data
}

pub fn merge(&mut self, base: &View, other: &View) {
pub fn merge(&mut self, index: IndexRef, base: &View, other: &View) {
if other.checkout() == base.checkout() || other.checkout() == self.checkout() {
// Keep the self side
} else if self.checkout() == base.checkout() {
Expand Down Expand Up @@ -120,29 +122,24 @@ impl View {
// warning?

// Merge git refs
let base_git_ref_names: HashSet<_> = base.git_refs().keys().clone().collect();
let other_git_ref_names: HashSet<_> = other.git_refs().keys().clone().collect();
for maybe_modified_git_ref_name in other_git_ref_names.intersection(&base_git_ref_names) {
let base_target = base.git_refs().get(*maybe_modified_git_ref_name).unwrap();
let other_target = other.git_refs().get(*maybe_modified_git_ref_name).unwrap();
let base_git_ref_names: HashSet<_> = base.git_refs().keys().cloned().collect();
let other_git_ref_names: HashSet<_> = other.git_refs().keys().cloned().collect();
for maybe_modified_git_ref_name in other_git_ref_names.union(&base_git_ref_names) {
let base_target = base.git_refs().get(maybe_modified_git_ref_name);
let other_target = other.git_refs().get(maybe_modified_git_ref_name);
if base_target == other_target {
continue;
}
// TODO: Handle modify/modify conflict (i.e. if self and base are different
// here)
self.insert_git_ref((*maybe_modified_git_ref_name).clone(), other_target.clone());
}
for added_git_ref_name in other_git_ref_names.difference(&base_git_ref_names) {
// TODO: Handle add/add conflict (i.e. if self also has the ref here)
self.insert_git_ref(
(*added_git_ref_name).clone(),
other.git_refs().get(*added_git_ref_name).unwrap().clone(),
);
}
for removed_git_ref_name in base_git_ref_names.difference(&other_git_ref_names) {
// TODO: Handle modify/remove conflict (i.e. if self and base are different
// here)
self.remove_git_ref(*removed_git_ref_name);
let self_target = self.git_refs().get(maybe_modified_git_ref_name);
let merged_target = merge_ref_targets(index, self_target, base_target, other_target);
match merged_target {
None => {
self.remove_git_ref(maybe_modified_git_ref_name);
}
Some(target) => {
self.insert_git_ref(maybe_modified_git_ref_name.clone(), target);
}
}
}
}
}
89 changes: 14 additions & 75 deletions lib/tests/test_git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,104 +168,43 @@ fn test_import_refs_merge() {
let (_temp_dir, repo) = testutils::init_repo(&settings, true);
let git_repo = repo.store().git_repo().unwrap();

// Set up the following refs and update them as follows:
// sideways-unchanged: one operation rewrites the ref
// unchanged-sideways: the other operation rewrites the ref
// remove-unchanged: one operation removes the ref
// unchanged-remove: the other operation removes the ref
// forward-forward: two operations move forward by different amounts
// sideways-sideways: two operations rewrite the ref
// forward-remove: one operation moves forward, the other operation removes
// remove-forward: one operation removes, the other operation moves
// add-add: two operations add the ref with different target
//
// The above cases distinguish between refs moving forward and sideways (and
// there are no tests for refs moving backward) because we may want to treat
// the cases differently, although that's still unclear.
//
// TODO: Consider adding more systematic testing to cover
// all state transitions. For example, the above does not include a case
// where a ref is added on both sides and one is an ancestor of the other
// (we should probably resolve that in favor of the descendant).
let commit1 = empty_git_commit(&git_repo, "refs/heads/main", &[]);
let commit2 = empty_git_commit(&git_repo, "refs/heads/main", &[&commit1]);
let commit3 = empty_git_commit(&git_repo, "refs/heads/main", &[&commit2]);
let commit4 = empty_git_commit(&git_repo, "refs/heads/feature1", &[&commit2]);
let commit5 = empty_git_commit(&git_repo, "refs/heads/feature2", &[&commit2]);
git_ref(&git_repo, "refs/heads/sideways-unchanged", commit3.id());
git_ref(&git_repo, "refs/heads/unchanged-sideways", commit3.id());
git_ref(&git_repo, "refs/heads/remove-unchanged", commit3.id());
git_ref(&git_repo, "refs/heads/unchanged-remove", commit3.id());
git_ref(&git_repo, "refs/heads/sideways-sideways", commit3.id());
git_ref(&git_repo, "refs/heads/forward-forward", commit1.id());
git_ref(&git_repo, "refs/heads/forward-remove", commit1.id());
git_ref(&git_repo, "refs/heads/remove-forward", commit1.id());
let mut tx = repo.start_transaction("initial import");
jujutsu_lib::git::import_refs(tx.mut_repo(), &git_repo).unwrap_or_default();
jujutsu_lib::git::import_refs(tx.mut_repo(), &git_repo).unwrap();
let repo = tx.commit();

// One of the concurrent operations:
git_ref(&git_repo, "refs/heads/sideways-unchanged", commit4.id());
delete_git_ref(&git_repo, "refs/heads/remove-unchanged");
git_ref(&git_repo, "refs/heads/sideways-sideways", commit4.id());
git_ref(&git_repo, "refs/heads/forward-forward", commit2.id());
git_ref(&git_repo, "refs/heads/forward-remove", commit2.id());
delete_git_ref(&git_repo, "refs/heads/remove-forward");
git_ref(&git_repo, "refs/heads/add-add", commit3.id());
delete_git_ref(&git_repo, "refs/heads/feature1");
git_ref(&git_repo, "refs/heads/main", commit4.id());
let mut tx1 = repo.start_transaction("concurrent import 1");
jujutsu_lib::git::import_refs(tx1.mut_repo(), &git_repo).unwrap_or_default();
jujutsu_lib::git::import_refs(tx1.mut_repo(), &git_repo).unwrap();
tx1.commit();

// The other concurrent operation:
git_ref(&git_repo, "refs/heads/unchanged-sideways", commit4.id());
delete_git_ref(&git_repo, "refs/heads/unchanged-remove");
git_ref(&git_repo, "refs/heads/sideways-sideways", commit5.id());
git_ref(&git_repo, "refs/heads/forward-forward", commit3.id());
delete_git_ref(&git_repo, "refs/heads/forward-remove");
git_ref(&git_repo, "refs/heads/remove-forward", commit2.id());
git_ref(&git_repo, "refs/heads/add-add", commit4.id());
let mut tx2 = repo.start_transaction("concurrent import 2");
jujutsu_lib::git::import_refs(tx2.mut_repo(), &git_repo).unwrap_or_default();
git_ref(&git_repo, "refs/heads/main", commit5.id());
jujutsu_lib::git::import_refs(tx2.mut_repo(), &git_repo).unwrap();
tx2.commit();

// Reload the repo, causing the operations to be merged.
let repo = repo.reload();

let view = repo.view();
let git_refs = view.git_refs();
assert_eq!(git_refs.len(), 9);
assert_eq!(
git_refs.get("refs/heads/sideways-unchanged"),
Some(RefTarget::Normal(commit_id(&commit4))).as_ref()
);
assert_eq!(
git_refs.get("refs/heads/unchanged-sideways"),
Some(RefTarget::Normal(commit_id(&commit4))).as_ref()
);
assert_eq!(git_refs.get("refs/heads/remove-unchanged"), None);
assert_eq!(git_refs.get("refs/heads/unchanged-remove"), None);
// TODO: Perhaps we should automatically resolve this to the descendant-most
// commit? (We currently do get the descendant-most, but that's only because we
// let the later operation overwrite.)
assert_eq!(
git_refs.get("refs/heads/forward-forward"),
Some(RefTarget::Normal(commit_id(&commit3))).as_ref()
);
// TODO: The rest of these should be conflicts (however we decide to represent
// that).
assert_eq!(
git_refs.get("refs/heads/sideways-sideways"),
Some(RefTarget::Normal(commit_id(&commit5))).as_ref()
);
assert_eq!(git_refs.get("refs/heads/forward-remove"), None);
assert_eq!(git_refs.len(), 2);
assert_eq!(
git_refs.get("refs/heads/remove-forward"),
Some(RefTarget::Normal(commit_id(&commit2))).as_ref()
);
assert_eq!(
git_refs.get("refs/heads/add-add"),
Some(RefTarget::Normal(commit_id(&commit4))).as_ref()
git_refs.get("refs/heads/main"),
Some(RefTarget::Conflict {
removes: vec![commit_id(&commit3)],
adds: vec![commit_id(&commit4), commit_id(&commit5)]
})
.as_ref()
);
assert_eq!(git_refs.get("refs/heads/feature1"), None);
}

#[test]
Expand Down
Loading

0 comments on commit 6b1ccd4

Please sign in to comment.