Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
tui: Add module providing access to patch cob
Erik Kundt committed 3 years ago
commit 0b6e32a351302bf69a60fce39c4d66c6293abe60
parent 46e05e84694a01298adbb841ba558e2c6b157695
2 files changed +52 -0
added radicle-tui/src/cob.rs
@@ -0,0 +1 @@
+
pub mod patch;
added radicle-tui/src/cob/patch.rs
@@ -0,0 +1,51 @@
+
use anyhow::Result;
+

+
use radicle::git::raw::Oid;
+

+
use radicle::cob::patch::{MergeTarget, Patch, PatchId, Patches};
+
use radicle::prelude::*;
+
use radicle::storage::git::Repository;
+

+
pub fn load_all(profile: &Profile, id: Id) -> Vec<(PatchId, Patch)> {
+
    if let Ok(repository) = &profile.storage.repository(id) {
+
        if let Ok(proposed) = load_proposed(repository) {
+
            return proposed;
+
        }
+
    }
+
    vec![]
+
}
+

+
pub fn load_proposed(repository: &Repository) -> Result<Vec<(PatchId, Patch)>> {
+
    let proposed = Patches::open(repository)?
+
        .proposed()?
+
        .into_iter()
+
        .map(|(id, patch, _)| (id, patch))
+
        .collect();
+

+
    Ok(proposed)
+
}
+

+
pub fn sync_status(repository: &Repository, patch: &Patch) -> Result<(usize, usize)> {
+
    let (_, revision) = patch.latest().unwrap();
+
    let target_oid = merge_target_oid(patch.target(), repository)?;
+
    let (ahead, behind) = repository
+
        .raw()
+
        .graph_ahead_behind(*revision.head(), target_oid)?;
+

+
    Ok((ahead, behind))
+
}
+

+
pub fn merge_target_oid(target: MergeTarget, repository: &Repository) -> Result<Oid> {
+
    match target {
+
        MergeTarget::Delegates => {
+
            if let Ok((_, target)) = repository.head() {
+
                Ok(*target)
+
            } else {
+
                anyhow::bail!(
+
                    "failed to determine default branch head for project {}",
+
                    repository.id,
+
                );
+
            }
+
        }
+
    }
+
}