cob: Gracefully handle missing manifest

These changes are motivated by an incompatibility with older operations
of `xyz.radicle.patch`. Before `a46bcb22` (2024-01-18) their head and
base commits were parents of the COB operation drectly, and did not
use `Rad-Related`.

Since `eceb7f29` (released as part of 1.4.0), links to these non-COB
parents removed the operations, breaking these patches.

Parents are filtered now filtered out if they do not carry a COB
manifest.

Co-authored-by: Lorenz Leutgeb <lorenz.leutgeb@radicle.dev>
This commit is contained in:
Yorgos Saslis
2026-11-06 20:32:03 +01:00
committed by Lorenz Leutgeb
co-authored by Lorenz Leutgeb
parent df62b66d9d
commit 8b053320dc
3 changed files with 196 additions and 3 deletions
@@ -180,6 +180,12 @@ impl change::Storage for git2::Repository {
.parents()
.map(Oid::from)
.filter(|p| !resources.contains(p) && !related.contains(p))
.filter(|p| {
// Before `a46bcb22`, operations on `xyz.radicle.patch` had parents
// that were not COB operations but commits in the underlying
// repository, i.e. "code commits".
!matches!(self.manifest_of(p), Err(error::Load::NoManifest(_)))
})
.collect();
let mut signatures = Signatures::try_from(&*commit)?
.into_iter()
+133 -3
View File
@@ -87,7 +87,9 @@ impl ChangeGraph {
// The Dag::dependency() function implicitly assumes that both nodes already exist in the graph.
// Therefore, we add the edges only after successfully processing all nodes.
for (child_id, parent_id) in edges_to_add {
graph.dependency(child_id, parent_id);
if graph.contains(&parent_id) {
graph.dependency(child_id, parent_id);
}
}
graph.roots().next()?;
@@ -113,7 +115,7 @@ impl ChangeGraph {
if !root.valid_signatures() {
return Err(EvaluateError::Signature(root.id));
}
if !root.dependencies.is_empty() {
if !root.parents.is_empty() {
return Err(EvaluateError::RootHasParents(root.id));
}
// Evaluate the root separately, since we can't have a COB without a valid root.
@@ -130,7 +132,7 @@ impl ChangeGraph {
&children,
|id, entry, siblings| {
// Every COB parent must have been applied before the entry.
if !entry.dependencies.iter().all(|p| applied.contains(p)) {
if !entry.parents.iter().all(|p| applied.contains(p)) {
log::debug!("Entry {} has a parent that was not applied.", entry.id);
return ControlFlow::Break(());
}
@@ -174,3 +176,131 @@ impl ChangeGraph {
x.1.timestamp.cmp(&y.1.timestamp).then(x.0.cmp(y.0))
}
}
#[cfg(all(test, feature = "git2"))]
#[allow(clippy::unwrap_used)]
mod test {
use nonempty::NonEmpty;
use super::*;
use crate::change::{Storage as _, Template};
use crate::object::{Commit, Objects, Reference, collaboration::get_from_objects};
fn store(repo: &git2::Repository, parents: Vec<Oid>, message: &str) -> Entry {
repo.store(
None,
vec![],
&crypto::SigningKey::mock(1),
Template {
type_name: "xyz.radicle.issue".parse().unwrap(),
tips: parents,
message: message.to_owned(),
embeds: vec![],
contents: NonEmpty::new(message.as_bytes().to_vec()),
},
)
.unwrap()
}
// Write a parent link without requiring the parent or its tree to be readable.
// COB signatures cover the tree, so adding a parent preserves the signature.
fn with_parent(repo: &git2::Repository, entry: &Entry, parent: Oid) -> Oid {
let odb = repo.odb().unwrap();
let object = odb.read(entry.id.into()).unwrap();
let commit = std::str::from_utf8(object.data()).unwrap();
let (tree, rest) = commit.split_once('\n').unwrap();
odb.write(
git2::ObjectType::Commit,
format!("{tree}\nparent {parent}\n{rest}").as_bytes(),
)
.unwrap()
.into()
}
fn unreadable_parents(repo: &git2::Repository, root: &Entry) -> Vec<Oid> {
let missing = "1111111111111111111111111111111111111111".parse().unwrap();
let sig = git2::Signature::now("test", "test@example.com").unwrap();
let tree = repo.find_tree(root.revision.into()).unwrap();
// A COB tree without the required signature is not a plain code commit.
let unsigned = repo
.commit(None, &sig, &sig, "unsigned", &tree, &[])
.unwrap();
let odb = repo.odb().unwrap();
let object = odb.read(unsigned).unwrap();
let commit = std::str::from_utf8(object.data()).unwrap();
let (_, rest) = commit.split_once('\n').unwrap();
let missing_tree = odb
.write(
git2::ObjectType::Commit,
format!("tree {missing}\n{rest}").as_bytes(),
)
.unwrap();
let mut builder = repo.treebuilder(Some(&tree)).unwrap();
let invalid = repo.blob(b"invalid manifest").unwrap();
builder.insert("manifest", invalid, 0o100644).unwrap();
let tree = repo.find_tree(builder.write().unwrap()).unwrap();
let malformed = repo
.commit(None, &sig, &sig, "malformed", &tree, &[])
.unwrap();
vec![
missing,
missing_tree.into(),
unsigned.into(),
malformed.into(),
]
}
fn setup() -> (tempfile::TempDir, git2::Repository) {
let dir = tempfile::tempdir().unwrap();
let repo = git2::Repository::init(dir.path()).unwrap();
let mut config = repo.config().unwrap();
config.set_str("user.name", "test").unwrap();
config.set_str("user.email", "test@example.com").unwrap();
(dir, repo)
}
fn objects(tip: Oid) -> Objects {
Objects::new(Reference {
name: "refs/cobs/test".to_owned().try_into().unwrap(),
target: Commit { id: tip },
})
}
#[test]
fn unreadable_parents_block_entries_and_their_descendants() {
let (_dir, repo) = setup();
let root = store(&repo, vec![], "root");
for parent in unreadable_parents(&repo, &root) {
let entry = store(&repo, vec![root.id], "child");
let child = with_parent(&repo, &entry, parent);
let descendant = store(&repo, vec![child], "descendant");
let object = get_from_objects::<NonEmpty<Entry>, _>(
&repo,
root.type_name(),
&root.id.into(),
&objects(descendant.id),
)
.unwrap()
.unwrap();
assert_eq!(object.history.len(), 1, "parent {parent}");
assert_eq!(object.history.tips(), BTreeSet::from([root.id]));
}
}
#[test]
fn unreadable_parents_cannot_turn_an_entry_into_a_root() {
let (_dir, repo) = setup();
let entry = store(&repo, vec![], "root");
for parent in unreadable_parents(&repo, &entry) {
let root = with_parent(&repo, &entry, parent);
let result = get_from_objects::<NonEmpty<Entry>, _>(
&repo,
entry.type_name(),
&root.into(),
&objects(root),
);
assert!(result.is_err(), "parent {parent}");
}
}
}
+57
View File
@@ -1771,6 +1771,63 @@ mod test {
assert!(issues.get(&id).unwrap().is_none());
}
/// Entries written before the `Rad-Related` trailer have code commits as parents.
/// Such a parent is not a COB parent, and the entry applies.
#[test]
fn entry_with_an_unloadable_parent_is_applied() {
let test::setup::NodeWithRepo { node, repo, .. } = test::setup::NodeWithRepo::default();
let mut issues = Cache::no_cache(&*repo, &node.signer).unwrap();
let a = issues
.create(cob::Title::new("A").unwrap(), "A", &[], &[], [])
.unwrap()
.id;
// Maya's comment on A, with a parent that is not a COB entry.
let comment = store_entry(
&repo,
&node.signer,
vec![*a, plain_commit(&repo)],
Action::Comment {
body: String::from("Hello"),
reply_to: Some(*a),
embeds: vec![],
},
);
write_cob_ref(&repo, &node.signer, &a, comment);
let issue = issues.get(&a).unwrap().unwrap();
assert_eq!(issue.comments().count(), 2);
}
/// A root written before the `Rad-Related` trailer has code commits as parents.
#[test]
fn root_with_unloadable_parents_loads() {
let test::setup::NodeWithRepo { node, repo, .. } = test::setup::NodeWithRepo::default();
let mut issues = Cache::no_cache(&*repo, &node.signer).unwrap();
let root = store_entry(
&repo,
&node.signer,
vec![plain_commit(&repo), plain_commit(&repo)],
Action::Comment {
body: String::from("Hello"),
reply_to: None,
embeds: vec![],
},
);
let id = IssueId::from(root);
write_cob_ref(&repo, &node.signer, &id, root);
let issue = issues.get(&id).unwrap().unwrap();
assert_eq!(issue.comments().count(), 1);
let reply = issues
.get_mut(&id)
.unwrap()
.comment("Reply", root, vec![])
.unwrap();
assert_eq!(repo.load(reply).unwrap().parents, vec![root]);
}
#[test]
fn embeds() {
let test::setup::NodeWithRepo { node, repo, .. } = test::setup::NodeWithRepo::default();