Skip to content

Commit

Permalink
fixed bin file size diff on untracked files (closes #171)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Dilly committed Jul 7, 2020
1 parent 02bd22d commit 26f734c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 21 additions & 17 deletions asyncgit/src/sync/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)?;
Expand Down Expand Up @@ -283,14 +283,16 @@ fn raw_diff_to_file_diff<'a>(
Ok(res.into_inner())
}

fn new_file_content(path: &Path) -> Option<String> {
fn new_file_content(path: &Path) -> Option<Vec<u8>> {
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);
}
}
Expand Down Expand Up @@ -460,14 +462,20 @@ mod tests {
}

#[test]
fn test_diff_new_binary_file_using_invalid_utf8() -> Result<()> {
fn test_diff_delta_size() -> 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")?;

stage_add_file(repo_path, file_path).unwrap();

commit(repo_path, "commit").unwrap();

File::create(&root.join(file_path))?
.write_all(b"\xc3\x28")?;
.write_all(b"\x00\x02")?;

let diff = get_diff(
repo_path,
Expand All @@ -476,26 +484,22 @@ mod tests {
)
.unwrap();

assert_eq!(diff.hunks.len(), 0);
dbg!(&diff);
assert_eq!(diff.sizes, (1, 2));
assert_eq!(diff.size_delta, 1);

Ok(())
}

#[test]
fn test_diff_delta_size() -> Result<()> {
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")?;

stage_add_file(repo_path, file_path).unwrap();

commit(repo_path, "commit").unwrap();

File::create(&root.join(file_path))?
.write_all(b"\x00\x02")?;
.write_all(b"\x00\xc7")?;

let diff = get_diff(
repo_path,
Expand All @@ -505,8 +509,8 @@ mod tests {
.unwrap();

dbg!(&diff);
assert_eq!(diff.sizes, (1, 2));
assert_eq!(diff.size_delta, 1);
assert_eq!(diff.sizes, (0, 2));
assert_eq!(diff.size_delta, 2);

Ok(())
}
Expand Down

0 comments on commit 26f734c

Please sign in to comment.