,
claims: &[Claim],
revision: &Revision,
+ ) -> Result {
+ Self::latest(store, claims.iter().map(Claim::statement), revision)
+ }
+
+ /// The unresolved conflicts of `revision`, as [`Conflicts::of`] finds
+ /// them, from `statements` about it, in arrival order.
+ ///
+ /// # Errors
+ ///
+ /// Fails like [`Conflicts::of`].
+ pub fn latest<'s, P: Platform>(
+ store: &Store
,
+ statements: impl IntoIterator,
+ revision: &Revision,
) -> Result {
if matches!(revision.author, Principal::Node(_)) {
return Ok(Self::default());
}
- let Some(claim) = claims.iter().rev().find(|claim| {
- claim.statement().kind == Kind::CONFLICT && claim.statement().issuer == revision.author
- }) else {
+ let latest = statements.into_iter().filter(|statement| {
+ statement.kind == Kind::CONFLICT && statement.issuer == revision.author
+ });
+ let Some(statement) = latest.last() else {
return Ok(Self::default());
};
- Self::listed(store, claim.statement())?.remaining(store, revision.snapshot)
+ Self::listed(store, statement)?.remaining(store, revision.snapshot)
}
/// Those of these conflicts `snapshot` still has: where it has the
diff --git a/apps/origin-src/src/permissions.rs b/apps/origin-src/src/permissions.rs
index a88d313c..32f9513b 100644
--- a/apps/origin-src/src/permissions.rs
+++ b/apps/origin-src/src/permissions.rs
@@ -14,7 +14,21 @@
//! names for every caller; and they must see what it's about, and for a promotion, its original: so no
//! one copies a result they can't read.
//!
-//! A revision evolves only from revisions they see. It may only hold
+//! A revision must extend its change's history: only a new change gets a
+//! root, and a successor evolves only from revisions of its change, which
+//! they see. So no one makes a change diverge with a root of their own,
+//! or becomes one of its creators, who [abandon](crate::annotations::RevisionView::authorizes) it.
+//! Where they may not write, a revision has an *effect*: each file it
+//! changes there, from its base's entry to its own. A root has none. A
+//! successor has exactly each predecessor's, on the same change below if
+//! it's on one, and lists exactly their unresolved conflicts there in its
+//! `core.conflict` (see [`conflict`](crate::conflict)), which only a
+//! `core.conflict` supersedes; a root lists those of the revision it's on.
+//! So whatever base it picks, a successor never reverts, replaces or
+//! resolves someone else's edits where its writer may not, nor adds a
+//! conflict there, nor brings a stack that would land with it; a rebase
+//! onto a newer tip keeps the effect, and one who takes a change over edits
+//! what they write and hands it on. A revision may only hold
//! content they supply or that a snapshot it refers to holds at the same
//! path (see [`Store::unsupplied`]): so no one reads a file by placing its
//! id where they may read. Nor an attachment: a record that carries one
@@ -22,21 +36,24 @@
//! a promotion, which carries its original's. The records of a batch are
//! read before the store's (see [`Batch`]), so a batch may hold a stack of
//! revisions and statements about them; those it brings anew are the
-//! caller's to see, but not one the store had.
+//! caller's to see, but not one the store had. Landings check what they change on the target, in the
+//! [queue](crate::queue).
-use std::collections::{HashMap, HashSet};
+use std::collections::{BTreeSet, HashMap, HashSet};
use std::iter;
use origin_platform::Platform;
use origin_shared::access::{Capability, PathAccess, Rights};
use origin_shared::event::EventKind;
use origin_shared::{
- Base, Kind, Object, ObjectId, Principal, RecordId, RepoPath, Revision, RevisionId, Statement,
- TargetName,
+ Base, ChangeId, Kind, Object, ObjectId, Principal, RecordId, RepoPath, Revision, RevisionId,
+ Statement, StatementId, TargetName, TreeId,
};
-use crate::annotations::RevisionView;
+use crate::annotations::{Annotations, Claim, RevisionView};
+use crate::conflict::Conflicts;
use crate::store::{Store, StoreError};
+use crate::tree::FileChange;
/// Who calls the service.
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -166,6 +183,11 @@ pub struct Batch<'a, P> {
/// Every object of the batch.
supplied: HashSet,
revisions: HashMap,
+ /// How many revisions of each change evolve from none.
+ roots: HashMap,
+ statements: HashMap,
+ /// The statements about each record, in batch order.
+ about: HashMap>,
}
impl<'a, P: Platform> Batch<'a, P> {
@@ -179,10 +201,27 @@ impl<'a, P: Platform> Batch<'a, P> {
store,
supplied,
revisions: HashMap::new(),
+ roots: HashMap::new(),
+ statements: HashMap::new(),
+ about: HashMap::new(),
};
for (id, object) in decoded {
- if let Object::Revision(revision) = object {
- batch.revisions.insert((*id).into(), revision);
+ match object {
+ Object::Revision(revision) => {
+ batch.revisions.insert((*id).into(), revision);
+ if revision.predecessors.is_empty() {
+ *batch.roots.entry(revision.change).or_default() += 1;
+ }
+ }
+ Object::Statement(statement) => {
+ batch.statements.insert((*id).into(), statement);
+ batch
+ .about
+ .entry(statement.subject)
+ .or_default()
+ .push(statement);
+ }
+ _ => {}
}
}
batch
@@ -192,6 +231,14 @@ impl<'a, P: Platform> Batch<'a, P> {
fn holds(&self, record: RecordId) -> bool {
self.supplied.contains(&record.into())
}
+
+ /// The statement `id`.
+ fn statement(&self, id: StatementId) -> Result {
+ match self.statements.get(&id) {
+ Some(&statement) => Ok(statement.clone()),
+ None => self.store.get(id),
+ }
+ }
}
impl RevisionView for Batch<'_, P> {
@@ -207,20 +254,31 @@ impl RevisionView for Batch<'_, P> {
/// module explains.
pub struct Writer<'a, P> {
batch: &'a Batch<'a, P>,
+ /// The statements and revisions of the store, as its index knows them.
+ annotations: &'a Annotations<'a, P>,
principal: &'a Principal,
rights: &'a Rights,
reader: Reader<'a, P>,
+ effects: Effects<'a, P, Batch<'a, P>>,
}
impl<'a, P: Platform> Writer<'a, P> {
- /// Checks `batch` for `principal`, with `rights`.
+ /// Checks `batch` for `principal`, with `rights`, finding what the
+ /// store has through `annotations`.
#[must_use]
- pub fn new(batch: &'a Batch<'a, P>, principal: &'a Principal, rights: &'a Rights) -> Self {
+ pub fn new(
+ batch: &'a Batch<'a, P>,
+ annotations: &'a Annotations<'a, P>,
+ principal: &'a Principal,
+ rights: &'a Rights,
+ ) -> Self {
Self {
batch,
+ annotations,
principal,
rights,
reader: Reader::new(batch.store, &rights.paths),
+ effects: Effects::new(batch.store, batch, &rights.paths),
}
}
@@ -244,13 +302,21 @@ impl<'a, P: Platform> Writer<'a, P> {
Err(Forbidden::UnsuppliedAttachment { record, attachment }.into())
}
+ /// Whether the caller may write every path, so no effect is checked.
+ fn writes_all(&self) -> bool {
+ self.rights.paths.writes_all()
+ }
+
/// Checks a statement put.
///
/// # Errors
///
/// Fails if the caller isn't its issuer, may not issue its kind, or
- /// doesn't see what it's about or the original it promotes, or if a
- /// record cannot be read.
+ /// doesn't see what it's about or the original it promotes; if it
+ /// replaces a `core.conflict` about a revision, by being one or
+ /// superseding one, that doesn't list exactly the conflicts the
+ /// revision carries where they may not write; or if a record cannot be
+ /// read.
pub fn statement(&mut self, statement: &Statement) -> Result<(), PutError> {
if statement.issuer != *self.principal {
return Err(Forbidden::NotYours {
@@ -277,6 +343,40 @@ impl<'a, P: Platform> Writer<'a, P> {
{
self.brings("statement", payload.into())?;
}
+ if !self.writes_all() {
+ self.keeps_listing(statement)?;
+ }
+ Ok(())
+ }
+
+ /// Checks that `statement`, if it replaces its issuer's `core.conflict`
+ /// about a revision, by being one or superseding one, lists exactly the
+ /// conflicts the revision carries where the caller may not write.
+ fn keeps_listing(&self, statement: &Statement) -> Result<(), PutError> {
+ let superseded = match statement.supersedes {
+ Some(id) => match self.batch.statement(id) {
+ Ok(superseded) => Some(superseded),
+ Err(StoreError::NotFound(_)) => None,
+ Err(error) => return Err(error.into()),
+ },
+ None => None,
+ };
+ let replaced = iter::once(statement)
+ .chain(&superseded)
+ .filter(|replaced| replaced.kind == Kind::CONFLICT);
+ for replaced in replaced {
+ let RecordId::Revision(id) = replaced.subject else {
+ continue;
+ };
+ let revision = self.batch.revision(id)?;
+ let listed =
+ if statement.kind == Kind::CONFLICT && statement.subject == replaced.subject {
+ Conflicts::latest(self.batch.store, [statement], &revision)?
+ } else {
+ Conflicts::default()
+ };
+ self.keeps_conflicts(&revision, &listed)?;
+ }
Ok(())
}
@@ -284,9 +384,11 @@ impl<'a, P: Platform> Writer<'a, P> {
///
/// # Errors
///
- /// Fails if the caller isn't its author; they don't see a revision it
- /// evolves from; or it holds content they neither supplied nor share
- /// with a snapshot it refers to; or if a tree cannot be read.
+ /// Fails if the caller isn't its author; it adds a root to a change
+ /// that has revisions, or evolves from a revision of another change;
+ /// they don't see a revision it evolves from; it has an effect they may
+ /// not have; or it holds content they neither supplied nor share with
+ /// a snapshot it refers to; or if a tree cannot be read.
pub fn revision(&mut self, id: RevisionId, revision: &Revision) -> Result<(), PutError> {
if revision.author != *self.principal {
return Err(Forbidden::NotYours {
@@ -296,16 +398,32 @@ impl<'a, P: Platform> Writer<'a, P> {
}
.into());
}
- let base = match revision.base {
- Base::Revision(id) => self.batch.revision(id)?.snapshot,
- Base::Acceptance(_) => self.batch.store.base_snapshot(revision.base)?,
- };
- let mut references = vec![base];
+ let roots = self.batch.roots.get(&revision.change).copied();
+ let stored = !self
+ .annotations
+ .index()
+ .revisions(revision.change)
+ .is_empty();
+ if revision.predecessors.is_empty() && (stored || roots > Some(1)) {
+ return Err(Forbidden::History(revision.change).into());
+ }
+ let mut references = vec![self.effects.base_snapshot(revision.base)?];
for &id in &revision.predecessors {
if !self.sees(RecordId::Revision(id))? {
return Err(PutError::Unseen(RecordId::Revision(id)));
}
- references.push(self.batch.revision(id)?.snapshot);
+ let predecessor = self.batch.revision(id)?;
+ if predecessor.change != revision.change {
+ return Err(Forbidden::History(revision.change).into());
+ }
+ references.push(predecessor.snapshot);
+ }
+ if let Some(forbidden) = self.effects.check(revision)? {
+ return Err(forbidden.into());
+ }
+ if !self.writes_all() {
+ let listed = self.conflicts(id, revision)?;
+ self.keeps_conflicts(revision, &listed)?;
}
self.brings("revision", revision.description.into())?;
if !self.rights.paths.reads_all() {
@@ -318,6 +436,153 @@ impl<'a, P: Platform> Writer<'a, P> {
}
Ok(())
}
+
+ /// The unresolved conflicts of `revision`, `id`, as its author's latest
+ /// `core.conflict` about it lists them, the store's then the batch's.
+ fn conflicts(&self, id: RevisionId, revision: &Revision) -> Result {
+ let subject = RecordId::Revision(id);
+ let stored = self.annotations.claims(subject)?;
+ let batched = self
+ .batch
+ .about
+ .get(&subject)
+ .into_iter()
+ .flatten()
+ .copied();
+ let statements = stored.iter().map(Claim::statement).chain(batched);
+ Conflicts::latest(self.batch.store, statements, revision)
+ }
+
+ /// Checks that `listed`, the unresolved conflicts of `revision`, are
+ /// exactly those it carries where the caller may not write: its
+ /// predecessors', or its base revision's for a root, where `revision`
+ /// still has the entry written. Only a writer of a path resolves its
+ /// conflict, or adds one.
+ fn keeps_conflicts(&self, revision: &Revision, listed: &Conflicts) -> Result<(), PutError> {
+ let from = match (&revision.predecessors[..], revision.base) {
+ ([], Base::Revision(base)) => vec![base],
+ (predecessors, _) => predecessors.to_vec(),
+ };
+ let unwritable = |path: &RepoPath| !self.rights.paths.writes(path);
+ let mut carried = BTreeSet::new();
+ for id in from {
+ let from = self.batch.revision(id)?;
+ let kept = self
+ .conflicts(id, &from)?
+ .only(unwritable)
+ .remaining(self.batch.store, revision.snapshot)?;
+ carried.extend(kept.paths());
+ }
+ let listed: BTreeSet = listed
+ .clone()
+ .only(unwritable)
+ .paths()
+ .into_iter()
+ .collect();
+ if let Some(path) = carried.difference(&listed).next() {
+ return Err(Forbidden::Resolve(path.clone()).into());
+ }
+ if let Some(path) = listed.difference(&carried).next() {
+ return Err(Forbidden::Conflict(path.clone()).into());
+ }
+ Ok(())
+ }
+}
+
+/// What revisions do where their writer may not write, as the module
+/// explains: the service checks it of every revision a signed-in caller
+/// puts.
+pub struct Effects<'a, P, V> {
+ store: &'a Store
,
+ /// Where revisions are read: a batch's, then the store's.
+ revisions: &'a V,
+ /// What the writer may do with each path.
+ paths: &'a PathAccess,
+}
+
+impl<'a, P: Platform, V: RevisionView> Effects<'a, P, V> {
+ /// The effects of revisions of `store`, read from `revisions`, for a
+ /// writer who may do `paths`.
+ pub fn new(store: &'a Store
, revisions: &'a V, paths: &'a PathAccess) -> Self {
+ Self {
+ store,
+ revisions,
+ paths,
+ }
+ }
+
+ /// The snapshot of `base`.
+ fn base_snapshot(&self, base: Base) -> Result {
+ match base {
+ Base::Revision(id) => Ok(self.revisions.revision(id)?.snapshot),
+ Base::Acceptance(_) => self.store.base_snapshot(base),
+ }
+ }
+
+ /// The effect of `revision`: each file it changes where the writer
+ /// may not write, from its base's entry to its own, sorted by path.
+ fn effect(&self, revision: &Revision) -> Result, StoreError> {
+ let base = self.base_snapshot(revision.base)?;
+ let mut changes = self.store.diff(base, revision.snapshot)?;
+ changes.retain(|change| !self.paths.writes(&change.path));
+ Ok(changes)
+ }
+
+ /// What the writer may not do of what `revision` does, if anything: a
+ /// root may have no effect, and a successor has exactly each
+ /// predecessor's, on the same change below, if it's on one.
+ ///
+ /// # Errors
+ ///
+ /// Fails if a revision or tree cannot be read.
+ pub fn check(&self, revision: &Revision) -> Result