From 06248cef814dbe9895f777f77fa3baf94fc57f76 Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Thu, 5 Dec 2024 15:56:03 +0800 Subject: [PATCH] fix(libra): diff handle non-utf8 content Signed-off-by: HouXiaoxuan --- libra/src/command/diff.rs | 41 +++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/libra/src/command/diff.rs b/libra/src/command/diff.rs index 67629a01d..1656505af 100644 --- a/libra/src/command/diff.rs +++ b/libra/src/command/diff.rs @@ -123,13 +123,7 @@ pub async fn execute(args: DiffArgs) { }; // use pathspec to filter files - let paths: Vec = args - .pathspec - .iter() - .map(|s| { - util::to_workdir_path(s) - }) - .collect(); + let paths: Vec = args.pathspec.iter().map(util::to_workdir_path).collect(); let mut buf: Vec = Vec::new(); // filter files, cross old and new files, and pathspec @@ -174,10 +168,10 @@ pub async fn diff( let read_content = |file: &PathBuf, hash: &SHA1| { // read content from blob or file match load_object::(hash) { - Ok(blob) => String::from_utf8(blob.data).unwrap(), + Ok(blob) => blob.data, Err(_) => { let file = util::workdir_to_absolute(file); - std::fs::read_to_string(&file) + std::fs::read(&file) .map_err(|e| { eprintln!("fatal: could not read file '{}': {}", file.display(), e); }) @@ -199,13 +193,13 @@ pub async fn diff( continue; } - let old_content = match &old_hash.as_ref() { + let old_content = match old_hash.as_ref() { Some(hash) => read_content(&file, hash), - None => String::new(), + None => Vec::new(), }; - let new_content = match &new_hash.as_ref() { + let new_content = match new_hash.as_ref() { Some(hash) => read_content(&file, hash), - None => String::new(), + None => Vec::new(), }; writeln!( @@ -230,8 +224,25 @@ pub async fn diff( }); writeln!(w, "index {}..{}", old_index, new_index).unwrap(); - // diff_result(&old_content, &new_content, w); - imara_diff_result(&old_content, &new_content, w); + // check is the content is valid utf-8 or maybe binary + match ( + String::from_utf8(old_content), + String::from_utf8(new_content), + ) { + (Ok(old_text), Ok(new_text)) => { + imara_diff_result(&old_text, &new_text, w); + } + _ => { + // TODO: Handle non-UTF-8 data as binary for now; consider optimization in the future. + writeln!( + w, + "Binary files a/{} and b/{} differ", + file.display(), + file.display() + ) + .unwrap(); + } + } } }