radicle: refactor Canonical
Changes for the `Canonical` type: - Reorder arguments so that `delegates` and `threshold` are grouped after `refname` - Add `Canonical::is_empty` to detect when `Canonical` was constructed with no tips - Consume `Canonical` when calling `Canonical::quorum` to indicate that it should be the final step
This commit is contained in:
parent
b2bcd561cf
commit
ab62dce683
|
|
@ -99,8 +99,8 @@ impl Canonical {
|
||||||
{
|
{
|
||||||
Self::reference(
|
Self::reference(
|
||||||
repo,
|
repo,
|
||||||
delegates,
|
|
||||||
&lit::refs_heads(project.default_branch()).into(),
|
&lit::refs_heads(project.default_branch()).into(),
|
||||||
|
delegates,
|
||||||
threshold,
|
threshold,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -109,8 +109,8 @@ impl Canonical {
|
||||||
/// the reference `name`.
|
/// the reference `name`.
|
||||||
pub fn reference<S>(
|
pub fn reference<S>(
|
||||||
repo: &S,
|
repo: &S,
|
||||||
|
refname: &Qualified,
|
||||||
delegates: &NonEmpty<Did>,
|
delegates: &NonEmpty<Did>,
|
||||||
name: &Qualified,
|
|
||||||
threshold: usize,
|
threshold: usize,
|
||||||
) -> Result<Self, raw::Error>
|
) -> Result<Self, raw::Error>
|
||||||
where
|
where
|
||||||
|
|
@ -118,14 +118,14 @@ impl Canonical {
|
||||||
{
|
{
|
||||||
let mut tips = BTreeMap::new();
|
let mut tips = BTreeMap::new();
|
||||||
for delegate in delegates.iter() {
|
for delegate in delegates.iter() {
|
||||||
match repo.reference_oid(delegate, name) {
|
match repo.reference_oid(delegate, refname) {
|
||||||
Ok(tip) => {
|
Ok(tip) => {
|
||||||
tips.insert(*delegate, tip);
|
tips.insert(*delegate, tip);
|
||||||
}
|
}
|
||||||
Err(e) if super::ext::is_not_found_err(&e) => {
|
Err(e) if super::ext::is_not_found_err(&e) => {
|
||||||
log::warn!(
|
log::warn!(
|
||||||
target: "radicle",
|
target: "radicle",
|
||||||
"Missing `refs/namespaces/{}/{name}` while calculating the canonical reference",
|
"Missing `refs/namespaces/{}/{refname}` while calculating the canonical reference",
|
||||||
delegate.as_key()
|
delegate.as_key()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -139,6 +139,14 @@ impl Canonical {
|
||||||
pub fn tips(&self) -> impl Iterator<Item = (&Did, &Oid)> {
|
pub fn tips(&self) -> impl Iterator<Item = (&Did, &Oid)> {
|
||||||
self.tips.iter()
|
self.tips.iter()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `true` is there were no tips found for any of the delegates for
|
||||||
|
/// the given reference.
|
||||||
|
///
|
||||||
|
/// N.b. this may be the case when a new reference is being created.
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.tips.is_empty()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check that a given `target` converges with any of the provided `tips`.
|
/// Check that a given `target` converges with any of the provided `tips`.
|
||||||
|
|
@ -176,7 +184,7 @@ impl Canonical {
|
||||||
///
|
///
|
||||||
/// Also returns an error if `heads` is empty or `threshold` cannot be
|
/// Also returns an error if `heads` is empty or `threshold` cannot be
|
||||||
/// satisified with the number of heads given.
|
/// satisified with the number of heads given.
|
||||||
pub fn quorum(&self, repo: &raw::Repository) -> Result<Oid, QuorumError> {
|
pub fn quorum(self, repo: &raw::Repository) -> Result<Oid, QuorumError> {
|
||||||
let mut candidates = BTreeMap::<_, usize>::new();
|
let mut candidates = BTreeMap::<_, usize>::new();
|
||||||
|
|
||||||
// Build a list of candidate commits and count how many "votes" each of them has.
|
// Build a list of candidate commits and count how many "votes" each of them has.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue