From 26f734c57dde24abd6714b1dda3d906765712073 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Tue, 7 Jul 2020 09:42:34 +0200 Subject: [PATCH] fixed bin file size diff on untracked files (closes #171) --- CHANGELOG.md | 1 + asyncgit/src/sync/diff.rs | 56 +++++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27afb35c..7604ef8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - switch deprecated transitive dependency `net2`->`socket2` [in `crossterm`->`mio`] ([#66](https://github.com/extrawurst/gitui/issues/66)) - crash diffing stash created on command line ([#178](https://github.com/extrawurst/gitui/issues/178)) +- delta file size diff on untracked binary files ([#171](https://github.com/extrawurst/gitui/issues/171)) ## [0.8.0] - 2020-07-06 diff --git a/asyncgit/src/sync/diff.rs b/asyncgit/src/sync/diff.rs index 8965b750..75c75af2 100644 --- a/asyncgit/src/sync/diff.rs +++ b/asyncgit/src/sync/diff.rs @@ -239,7 +239,7 @@ fn raw_diff_to_file_diff<'a>( let mut patch = Patch::from_buffers( &[], None, - newfile_content.as_bytes(), + newfile_content.as_slice(), Some(&newfile_path), None, )?; @@ -283,14 +283,16 @@ fn raw_diff_to_file_diff<'a>( Ok(res.into_inner()) } -fn new_file_content(path: &Path) -> Option { +fn new_file_content(path: &Path) -> Option> { if let Ok(meta) = fs::symlink_metadata(path) { if meta.file_type().is_symlink() { if let Ok(path) = fs::read_link(path) { - return Some(path.to_str()?.to_string()); + return Some( + path.to_str()?.to_string().as_bytes().into(), + ); } } else if meta.file_type().is_file() { - if let Ok(content) = fs::read_to_string(path) { + if let Ok(content) = fs::read(path) { return Some(content); } } @@ -459,28 +461,6 @@ mod tests { assert_eq!(diff.hunks[0].lines[1].content, "test"); } - #[test] - fn test_diff_new_binary_file_using_invalid_utf8() -> Result<()> { - let file_path = Path::new("bar"); - let (_td, repo) = repo_init_empty().unwrap(); - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); - - File::create(&root.join(file_path))? - .write_all(b"\xc3\x28")?; - - let diff = get_diff( - repo_path, - String::from(file_path.to_str().unwrap()), - false, - ) - .unwrap(); - - assert_eq!(diff.hunks.len(), 0); - - Ok(()) - } - #[test] fn test_diff_delta_size() -> Result<()> { let file_path = Path::new("bar"); @@ -511,6 +491,30 @@ mod tests { Ok(()) } + #[test] + fn test_binary_diff_delta_size_untracked() -> Result<()> { + let file_path = Path::new("bar"); + let (_td, repo) = repo_init_empty().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); + + File::create(&root.join(file_path))? + .write_all(b"\x00\xc7")?; + + let diff = get_diff( + repo_path, + String::from(file_path.to_str().unwrap()), + false, + ) + .unwrap(); + + dbg!(&diff); + assert_eq!(diff.sizes, (0, 2)); + assert_eq!(diff.size_delta, 2); + + Ok(()) + } + #[test] fn test_diff_delta_size_commit() -> Result<()> { let file_path = Path::new("bar");