loss non-utf8 files lossily (fix #726)

This commit is contained in:
Stephan Dilly 2021-05-22 13:36:16 +02:00
parent bf148c54d5
commit a31f185154
3 changed files with 24 additions and 13 deletions

View File

@ -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),

View File

@ -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)
}

View File

@ -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