Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions apps/origin-src/src/ancestry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,38 @@ impl<I: Copy + Eq + Hash + Ord> Ancestry<I> {
/// recursive strategy does: each from their own merge base, with what
/// [`Store::merge_bases`] leaves where they conflict, so a merge from
/// the result conflicts there, and under it, unless both sides agree. Writes the trees
/// that makes, which merging reads.
/// that makes, which merging reads. Calls `need` with each record
/// before reading its snapshot, so a lazy clone can fetch it.
///
/// # Errors
///
/// Fails if a tree cannot be read or written.
pub fn merge_base<P: Platform>(
/// Fails as `need` does, or if a tree cannot be read or written.
pub fn merge_base<P: Platform, E: From<StoreError>>(
&self,
store: &Store<P>,
tips: &[I],
) -> Result<Option<TreeId>, StoreError> {
mut need: impl FnMut(I) -> Result<(), E>,
) -> Result<Option<TreeId>, E> {
let groups: Vec<_> = tips.iter().map(std::slice::from_ref).collect();
self.merge_base_of(store, &groups)
self.merge_base_of(store, &groups, &mut need)
}

/// The snapshot to merge `groups` from, each standing for all it holds.
fn merge_base_of<P: Platform>(
fn merge_base_of<P: Platform, E: From<StoreError>>(
&self,
store: &Store<P>,
groups: &[&[I]],
) -> Result<Option<TreeId>, StoreError> {
need: &mut impl FnMut(I) -> Result<(), E>,
) -> Result<Option<TreeId>, E> {
let bases = self.best_common(groups);
let Some((&first, others)) = bases.split_first() else {
return Ok(None);
};
let mut merged = vec![first];
let mut tree = self.snapshot(first);
let mut tree = self.snapshot(first, need)?;
for &other in others {
let base = self.merge_base_of(store, &[&merged, &[other]])?;
let merge = store.merge_bases(base, tree, self.snapshot(other))?;
let base = self.merge_base_of(store, &[&merged, &[other]], need)?;
let merge = store.merge_bases(base, tree, self.snapshot(other, need)?)?;
store.write_all(&merge.trees)?;
tree = merge.tree;
merged.push(other);
Expand Down Expand Up @@ -127,8 +130,10 @@ impl<I: Copy + Eq + Hash + Ord> Ancestry<I> {
self.records.get(&id).map_or(&[], |(parents, _)| parents)
}

fn snapshot(&self, id: I) -> TreeId {
self.records[&id].1
/// The snapshot of `id`, once `need` had it.
fn snapshot<E>(&self, id: I, need: &mut impl FnMut(I) -> Result<(), E>) -> Result<TreeId, E> {
need(id)?;
Ok(self.records[&id].1)
}
}

Expand Down
4 changes: 3 additions & 1 deletion apps/origin-src/src/queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ impl<'a, P: Platform> Queue<'a, P> {
let state = self.store.get(tip.acceptance)?.body().state;
let tips = [session.tip.acceptance, tip.acceptance];
let history = session.history(&tips)?;
let base = history.merge_base(self.store, &tips)?;
// Nothing to fetch: the queue reads the bases' states from its
// store, as it does the tips'.
let base = history.merge_base(self.store, &tips, |_| Ok::<_, StoreError>(()))?;
let merge = self.store.merge(base, state, session.state)?;
let proposal = Proposal::Target(source.clone());
if !merge.conflicts.is_empty() {
Expand Down
5 changes: 4 additions & 1 deletion apps/origin-src/src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,12 +1338,15 @@ impl Workspace {
/// The snapshot to merge `heads`, revisions of one change, from: that of
/// the newest revision they all evolve from, or the
/// [merge](Ancestry::merge_base) of several, if each lacks another.
/// Fetches the snapshots it reads that a clone lacks.
fn merge_base(&self, heads: &[RevisionId]) -> Result<Option<TreeId>, WorkspaceError> {
let ancestry = Ancestry::walk(heads, |id| {
let revision: Revision = self.store.get(id)?;
Ok::<_, StoreError>((revision.predecessors, revision.snapshot))
})?;
Ok(ancestry.merge_base(&self.store, heads)?)
ancestry.merge_base(&self.store, heads, |id| {
self.need(&[RecordId::Revision(id)])
})
}

/// `origin log [<target>]`: prints the working copy's stack of changes
Expand Down
29 changes: 29 additions & 0 deletions apps/origin-src/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,32 @@ fn a_move_that_cannot_fetch_moves_nothing() {
clone.ok(&["edit", &own]);
assert_eq!(clone.read("notes"), "mine\n");
}

#[test]
fn a_clone_converges_heads_whose_merge_base_it_never_read() {
let ada = User::new("ada");
let origin = Repo::init(&ada);
origin.write("file", "base\n");
origin.ok(&["describe", "Shared work"]);
let change = origin.full_change();
// Two agents revise the same revision.
let pointer = origin.path(".origin/workspace");
let started = fs::read_to_string(&pointer).unwrap();
origin.write("file", "left\n");
origin.ok(&["describe", "Shared work"]);
fs::write(&pointer, started).unwrap();
origin.write("file", "right\n");
origin.ok(&["describe", "Shared work"]);

// The clone fetches the heads' snapshots, and their merge base's.
let clone = Repo::clone(&ada, origin.root().to_str().unwrap(), &[]);
clone.ok(&["fetch", "--change", &change]);
let converged = clone.ok(&["converge", &change]);
assert!(converged.starts_with("Converged change "), "{converged}");
clone.ok(&["edit", &change]);
let file = clone.read("file");
assert!(
file.contains("left\n") && file.contains("right\n"),
"{file}"
);
}
Loading