diff --git a/asyncgit/src/error.rs b/asyncgit/src/error.rs index 1ba229fe..a22c9dbd 100644 --- a/asyncgit/src/error.rs +++ b/asyncgit/src/error.rs @@ -24,6 +24,9 @@ pub enum Error { #[error("git: can\u{2019}t run blame on a binary file")] NoBlameOnBinaryFile, + #[error("binary file")] + BinaryFile, + #[error("io error:{0}")] Io(#[from] std::io::Error), diff --git a/asyncgit/src/sync/tree.rs b/asyncgit/src/sync/tree.rs index 38fa634c..68c49b59 100644 --- a/asyncgit/src/sync/tree.rs +++ b/asyncgit/src/sync/tree.rs @@ -1,5 +1,8 @@ use super::{utils::bytes2string, CommitId}; -use crate::{error::Result, sync::utils::repo}; +use crate::{ + error::{Error, Result}, + sync::utils::repo, +}; use git2::{Oid, Repository, Tree}; use scopetime::scope_time; use std::{ @@ -80,10 +83,10 @@ pub fn tree_file_content( let blob = repo.find_blob(file.id)?; if blob.is_binary() { - return Ok(String::new()); + return Err(Error::BinaryFile); } - let content = String::from_utf8(blob.content().into())?; + let content = String::from_utf8_lossy(blob.content()).to_string(); Ok(content) } diff --git a/src/components/revision_files.rs b/src/components/revision_files.rs index cffea8d5..6a5cf11b 100644 --- a/src/components/revision_files.rs +++ b/src/components/revision_files.rs @@ -132,7 +132,7 @@ impl RevisionFilesComponent { }) } - fn selection_changed(&mut self) -> Result<()> { + fn selection_changed(&mut self) { if let Some(file) = self.tree.selected_file().map(|file| { file.full_path() .strip_prefix("./") @@ -146,26 +146,31 @@ impl RevisionFilesComponent { .unwrap_or_default(); if !already_loaded { - self.load_file(file)?; + self.load_file(file); } } else { self.current_file = None; } - - Ok(()) } - fn load_file(&mut self, path: String) -> Result<()> { + fn load_file(&mut self, path: String) { if let Some(item) = self .files .iter() .find(|f| f.path.ends_with(Path::new(&path))) { - let content = sync::tree_file_content(CWD, item)?; - self.current_file = Some((path, content)); + match sync::tree_file_content(CWD, item) { + Ok(content) => { + self.current_file = Some((path, content)) + } + Err(e) => { + self.current_file = Some(( + path, + format!("error loading file: {}", e), + )) + } + } } - - Ok(()) } } @@ -300,7 +305,7 @@ impl Component for RevisionFilesComponent { &self.key_config, key, ) { - self.selection_changed()?; + self.selection_changed(); true } else { false