Use operation bundle for patch creation

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-12-11 16:56:18 +01:00
parent e5e1ba583f
commit 036442af87
No known key found for this signature in database
1 changed files with 34 additions and 21 deletions

View File

@ -544,6 +544,20 @@ impl Transaction {
} }
} }
/// Create a new transaction to be used as the initial set of operations for a COB.
pub fn initial(actor: ActorId) -> Self {
Self {
actions: Vec::new(),
clock: clock::Lamport::default(),
actor,
}
}
/// Consume this transaction, returning the underlying actions.
pub fn actions(self) -> Vec<Action> {
self.actions
}
/// Add an operation to this transaction. /// Add an operation to this transaction.
pub fn push(&mut self, action: Action) -> OpId { pub fn push(&mut self, action: Action) -> OpId {
self.actions.push(action); self.actions.push(action);
@ -553,10 +567,15 @@ impl Transaction {
} }
/// Edit patch metadata. /// Edit patch metadata.
pub fn edit(&mut self, title: String, description: String, target: MergeTarget) -> OpId { pub fn edit(
&mut self,
title: impl ToString,
description: impl ToString,
target: MergeTarget,
) -> OpId {
self.push(Action::Edit { self.push(Action::Edit {
title, title: title.to_string(),
description, description: description.to_string(),
target, target,
}) })
} }
@ -813,33 +832,25 @@ impl<'a> Patches<'a> {
/// Create a patch. /// Create a patch.
pub fn create<'g, G: Signer>( pub fn create<'g, G: Signer>(
&'g mut self, &'g mut self,
title: impl Into<String>, title: impl ToString,
description: impl Into<String>, description: impl ToString,
target: MergeTarget, target: MergeTarget,
base: impl Into<git::Oid>, base: impl Into<git::Oid>,
oid: impl Into<git::Oid>, oid: impl Into<git::Oid>,
tags: &[Tag], tags: &[Tag],
signer: &G, signer: &G,
) -> Result<PatchMut<'a, 'g>, Error> { ) -> Result<PatchMut<'a, 'g>, Error> {
let title = title.into(); let mut tx = Transaction::initial(*self.public_key());
let description = description.into();
let action = Action::Revision {
base: base.into(),
oid: oid.into(),
};
let (id, patch, clock) = self
.raw
.create("Create patch", NonEmpty::new(action), signer)?;
let mut patch = PatchMut::new(id, patch, clock, self);
assert_eq!(patch.clock, clock::Lamport::from(0));
patch.edit(title, description, target, signer)?; tx.revision(base, oid);
assert_eq!(patch.clock, clock::Lamport::from(1)); tx.edit(title, description, target);
tx.tag(tags.to_owned(), []);
patch.tag(tags.to_owned(), [], signer)?; #[allow(clippy::unwrap_used)]
assert_eq!(patch.clock, clock::Lamport::from(2)); let actions: NonEmpty<_> = tx.actions().try_into().unwrap(); // SAFETY: The transaction is not empty.
let (id, patch, clock) = self.raw.create("Create patch", actions, signer)?;
Ok(patch) Ok(PatchMut::new(id, patch, clock, self))
} }
/// Get an issue. /// Get an issue.
@ -1081,6 +1092,8 @@ mod test {
) )
.unwrap(); .unwrap();
assert_eq!(patch.clock, clock::Lamport::from(2));
let id = patch.id; let id = patch.id;
let patch = patches.get(&id).unwrap().unwrap(); let patch = patches.get(&id).unwrap().unwrap();