Skip to content

fix: patch-hub crashes when loading patches after b4 fails #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cover_renderer::render_cover;
use logging::Logger;
use patch_hub::lore::{
lore_api_client::BlockingLoreAPIClient,
lore_session,
lore_session::{self, B4Result},
patch::{Author, Patch},
};
use patch_renderer::{render_patch_preview, PatchRenderer};
Expand Down Expand Up @@ -132,7 +132,7 @@ impl App {
/// Initializes field [App::details_actions], from currently selected
/// patchset in [App::bookmarked_patchsets] or [App::latest_patchsets],
/// depending on the value of [App::current_screen].
pub fn init_details_actions(&mut self) -> color_eyre::Result<()> {
pub fn init_details_actions(&mut self) -> color_eyre::Result<B4Result> {
let representative_patch: Patch;
let mut is_patchset_bookmarked = true;
let mut reviewed_by = Vec::new();
Expand Down Expand Up @@ -160,12 +160,14 @@ impl App {
screen => bail!(format!("Invalid screen passed as argument {screen:?}")),
};

let patchset_path: String = match log_on_error!(lore_session::download_patchset(
let patchset_path: String = match lore_session::download_patchset(
self.config.patchsets_cache_dir(),
&representative_patch,
)) {
Ok(result) => result,
Err(io_error) => bail!("{io_error}"),
) {
lore_session::B4Result::PatchFound(path) => path,
lore_session::B4Result::PatchNotFound(error) => {
return Ok(lore_session::B4Result::PatchNotFound(error))
}
};

match log_on_error!(lore_session::split_patchset(&patchset_path)) {
Expand Down Expand Up @@ -250,7 +252,7 @@ impl App {
lore_api_client: self.lore_api_client.clone(),
patchset_path,
});
Ok(())
Ok(B4Result::PatchFound("Patch found".to_string()))
}
Err(message) => bail!(message),
}
Expand Down
18 changes: 15 additions & 3 deletions src/handler/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ use std::ops::ControlFlow;
use crate::{
app::{screens::CurrentScreen, App},
loading_screen,
ui::popup::{help::HelpPopUpBuilder, PopUp},
ui::popup::{help::HelpPopUpBuilder, info_popup::InfoPopUp, PopUp},
};
use color_eyre::eyre::Ok;
use ratatui::{
crossterm::event::{KeyCode, KeyEvent},
prelude::Backend,
Terminal,
};

use patch_hub::lore::lore_session::B4Result;

pub fn handle_latest_patchsets<B>(
app: &mut App,
key: KeyEvent,
Expand Down Expand Up @@ -55,9 +58,18 @@ where
"Loading patchset" => {
let result = app.init_details_actions();
if result.is_ok() {
app.set_current_screen(CurrentScreen::PatchsetDetails);
match result.unwrap() {
B4Result::PatchFound(_) => {
app.set_current_screen(CurrentScreen::PatchsetDetails);
}

B4Result::PatchNotFound(err_cause) => {
app.popup = Some(InfoPopUp::generate_info_popup("Error",&format!("The selected patchset couldn't be retrieved.\nReason: {err_cause}\nPlease choose another patchset.")));
app.set_current_screen(CurrentScreen::LatestPatchsets);
}
}
}
result
Ok(())
}
};
}
Expand Down
29 changes: 24 additions & 5 deletions src/lore/lore_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use derive_getters::Getters;
use regex::Regex;
use serde_xml_rs::from_str;
use std::collections::{HashMap, HashSet};
use std::ffi::OsStr;
use std::io::{BufRead, BufReader};
use std::mem::swap;
use std::path::Path;
Expand All @@ -18,6 +19,11 @@ use std::{
};
use thiserror::Error;

pub enum B4Result {
PatchFound(String),
PatchNotFound(String),
}

#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -152,17 +158,20 @@ impl LoreSession {
}
}

pub fn download_patchset(output_dir: &str, patch: &Patch) -> io::Result<String> {
pub fn download_patchset(output_dir: &str, patch: &Patch) -> B4Result {
let message_id: &str = &patch.message_id().href;
let mbox_name: String = extract_mbox_name_from_message_id(message_id);

if !Path::new(output_dir).exists() {
fs::create_dir_all(output_dir)?;
match fs::create_dir_all(output_dir) {
Ok(_) => {}
Err(_) => return B4Result::PatchNotFound("Couldn't create patches dir.".to_string()),
};
}

let filepath: String = format!("{output_dir}/{mbox_name}");
if !Path::new(&filepath).exists() {
Command::new("b4")
match Command::new("b4")
.arg("--quiet")
.arg("am")
.arg("--use-version")
Expand All @@ -174,10 +183,20 @@ pub fn download_patchset(output_dir: &str, patch: &Patch) -> io::Result<String>
.arg(&mbox_name)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()?;
.status()
{
Ok(_) => {}
Err(_) => return B4Result::PatchNotFound("Couldn't create patch file.".to_string()),
};
}

let path = Path::new(OsStr::new(&filepath));

if !path.exists() {
return B4Result::PatchNotFound("Couldn't create patch file.".to_string());
}

Ok(filepath)
B4Result::PatchFound(filepath)
}

fn extract_mbox_name_from_message_id(message_id: &str) -> String {
Expand Down