From a3077d5b1562c26a7f4510e74b19bb485df18a58 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Fri, 2 Jun 2023 12:09:47 +0200 Subject: [PATCH] tui: Improve cob retrieval --- radicle-tui/src/app.rs | 27 +++++----------- radicle-tui/src/cob.rs | 1 + radicle-tui/src/cob/issue.rs | 19 +++++++++++ radicle-tui/src/cob/patch.rs | 52 +++++++------------------------ radicle-tui/src/ui/context.rs | 10 ++++++ radicle-tui/src/ui/widget/home.rs | 30 +++++++----------- 6 files changed, 59 insertions(+), 80 deletions(-) create mode 100644 radicle-tui/src/cob/issue.rs diff --git a/radicle-tui/src/app.rs b/radicle-tui/src/app.rs index 9a32c737..8875a72f 100644 --- a/radicle-tui/src/app.rs +++ b/radicle-tui/src/app.rs @@ -4,19 +4,18 @@ pub mod subscription; use anyhow::Result; -use radicle::cob::issue::{IssueId, Issues}; -use radicle::cob::patch::{PatchId, Patches}; +use radicle::cob::issue::IssueId; +use radicle::cob::patch::PatchId; use radicle::identity::{Id, Project}; use radicle::profile::Profile; -use radicle::storage::ReadStorage; use tuirealm::application::PollStrategy; use tuirealm::{Application, Frame, NoUserEvent}; -use radicle_tui::ui; use radicle_tui::ui::context::Context; use radicle_tui::ui::theme::{self, Theme}; use radicle_tui::Tui; +use radicle_tui::{cob, ui}; use page::{HomeView, PatchView}; @@ -114,15 +113,9 @@ impl App { id: PatchId, theme: &Theme, ) -> Result<()> { - let repo = self - .context - .profile() - .storage - .repository(*self.context.id()) - .unwrap(); - let patches = Patches::open(&repo).unwrap(); + let repo = self.context.repository(); - if let Some(patch) = patches.get(&id)? { + if let Some(patch) = cob::patch::find(repo, &id)? { let view = Box::new(PatchView::new((id, patch))); self.pages.push(view, app, &self.context, theme)?; @@ -140,15 +133,9 @@ impl App { id: IssueId, theme: &Theme, ) -> Result<()> { - let repo = self - .context - .profile() - .storage - .repository(*self.context.id()) - .unwrap(); - let issues = Issues::open(&repo).unwrap(); + let repo = self.context.repository(); - if let Some(issue) = issues.get(&id)? { + if let Some(issue) = cob::issue::find(repo, &id)? { let view = Box::new(IssuePage::new((id, issue))); self.pages.push(view, app, &self.context, theme)?; diff --git a/radicle-tui/src/cob.rs b/radicle-tui/src/cob.rs index e268c5ac..a5dfc54b 100644 --- a/radicle-tui/src/cob.rs +++ b/radicle-tui/src/cob.rs @@ -1 +1,2 @@ +pub mod issue; pub mod patch; diff --git a/radicle-tui/src/cob/issue.rs b/radicle-tui/src/cob/issue.rs new file mode 100644 index 00000000..04234aec --- /dev/null +++ b/radicle-tui/src/cob/issue.rs @@ -0,0 +1,19 @@ +use anyhow::Result; +use radicle::cob::issue::{Issue, IssueId, Issues}; +use radicle::storage::git::Repository; + +pub fn all(repository: &Repository) -> Result> { + let patches = Issues::open(repository)? + .all() + .map(|iter| iter.flatten().collect::>())?; + + Ok(patches + .into_iter() + .map(|(id, issue, _)| (id, issue)) + .collect::>()) +} + +pub fn find(repository: &Repository, id: &IssueId) -> Result> { + let issues = Issues::open(repository)?; + Ok(issues.get(id)?) +} diff --git a/radicle-tui/src/cob/patch.rs b/radicle-tui/src/cob/patch.rs index f29992a0..01dce274 100644 --- a/radicle-tui/src/cob/patch.rs +++ b/radicle-tui/src/cob/patch.rs @@ -1,50 +1,20 @@ use anyhow::Result; -use radicle::git::raw::Oid; - -use radicle::cob::patch::{MergeTarget, Patch, PatchId, Patches}; -use radicle::prelude::*; +use radicle::cob::patch::{Patch, PatchId, Patches}; 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 all(repository: &Repository) -> Result> { + let patches = Patches::open(repository)? + .all() + .map(|iter| iter.flatten().collect::>())?; -pub fn load_proposed(repository: &Repository) -> Result> { - let proposed = Patches::open(repository)? - .proposed()? + Ok(patches + .into_iter() .map(|(id, patch, _)| (id, patch)) - .collect(); - - Ok(proposed) + .collect::>()) } -pub fn sync_status(repository: &Repository, patch: &Patch) -> Result<(usize, usize)> { - let (_, revision) = patch.latest(); - 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 { - 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, - ); - } - } - } +pub fn find(repository: &Repository, id: &PatchId) -> Result> { + let patches = Patches::open(repository)?; + Ok(patches.get(id)?) } diff --git a/radicle-tui/src/ui/context.rs b/radicle-tui/src/ui/context.rs index 0f634224..fbd01ede 100644 --- a/radicle-tui/src/ui/context.rs +++ b/radicle-tui/src/ui/context.rs @@ -1,18 +1,24 @@ use radicle::prelude::{Id, Project}; use radicle::Profile; +use radicle::storage::git::Repository; +use radicle::storage::ReadStorage; + pub struct Context { profile: Profile, id: Id, project: Project, + repository: Repository, } impl Context { pub fn new(profile: Profile, id: Id, project: Project) -> Self { + let repository = profile.storage.repository(id).unwrap(); Self { id, profile, project, + repository, } } @@ -27,4 +33,8 @@ impl Context { pub fn project(&self) -> &Project { &self.project } + + pub fn repository(&self) -> &Repository { + &self.repository + } } diff --git a/radicle-tui/src/ui/widget/home.rs b/radicle-tui/src/ui/widget/home.rs index 1c0fc75c..d9d3ed0e 100644 --- a/radicle-tui/src/ui/widget/home.rs +++ b/radicle-tui/src/ui/widget/home.rs @@ -1,8 +1,3 @@ -use radicle::cob::issue::Issues; -use radicle::storage::ReadStorage; - -use radicle::cob::patch::Patches; - use tuirealm::command::{Cmd, CmdResult}; use tuirealm::tui::layout::Rect; use tuirealm::{AttrValue, Attribute, Frame, MockComponent, Props, State}; @@ -14,6 +9,7 @@ use super::common::list::{ColumnWidth, Table}; use super::{Widget, WidgetComponent}; +use crate::cob; use crate::ui::cob::{IssueItem, PatchItem}; use crate::ui::context::Context; use crate::ui::layout; @@ -59,10 +55,6 @@ pub struct IssueBrowser { impl IssueBrowser { pub fn new(context: &Context, theme: &Theme, shortcuts: Widget) -> Self { - let repo = context.profile().storage.repository(*context.id()).unwrap(); - let issues = Issues::open(&repo) - .and_then(|issues| issues.all().map(|iter| iter.flatten().collect::>())); - let header = [ common::label(" ● "), common::label("ID"), @@ -83,10 +75,12 @@ impl IssueBrowser { ColumnWidth::Fixed(18), ]; + let repo = context.repository(); let mut items = vec![]; - if let Ok(issues) = issues { - for (id, patch, _) in issues { - if let Ok(item) = IssueItem::try_from((context.profile(), &repo, id, patch)) { + + if let Ok(issues) = cob::issue::all(repo) { + for (id, issue) in issues { + if let Ok(item) = IssueItem::try_from((context.profile(), repo, id, issue)) { items.push(item); } } @@ -135,10 +129,6 @@ pub struct PatchBrowser { impl PatchBrowser { pub fn new(context: &Context, theme: &Theme, shortcuts: Widget) -> Self { - let repo = context.profile().storage.repository(*context.id()).unwrap(); - let patches = Patches::open(&repo) - .and_then(|patches| patches.all().map(|iter| iter.flatten().collect::>())); - let header = [ common::label(" ● "), common::label("ID"), @@ -161,10 +151,12 @@ impl PatchBrowser { ColumnWidth::Fixed(18), ]; + let repo = context.repository(); let mut items = vec![]; - if let Ok(patches) = patches { - for (id, patch, _) in patches { - if let Ok(item) = PatchItem::try_from((context.profile(), &repo, id, patch)) { + + if let Ok(patches) = cob::patch::all(repo) { + for (id, patch) in patches { + if let Ok(item) = PatchItem::try_from((context.profile(), repo, id, patch)) { items.push(item); } }