Skip to content

Commit c0b48b8

Browse files
author
Stephan Dilly
committed
fix "file not found" diff on binary files
1 parent aa83096 commit c0b48b8

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

asyncgit/src/sync/diff.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
use super::utils;
44
use crate::hash;
55
use git2::{
6-
Delta, Diff, DiffDelta, DiffFormat, DiffHunk, DiffOptions, Patch,
7-
Repository,
6+
Delta, Diff, DiffDelta, DiffFlags, DiffFormat, DiffHunk,
7+
DiffOptions, Patch, Repository,
88
};
9+
use log::debug;
910
use scopetime::scope_time;
1011
use std::{fs, path::Path};
1112

@@ -172,7 +173,13 @@ pub fn get_diff(repo_path: &str, p: String, stage: bool) -> FileDiff {
172173
let newfile_path =
173174
repo_path.join(delta.new_file().path().unwrap());
174175

175-
let newfile_content = new_file_content(&newfile_path);
176+
let newfile_content =
177+
if delta.flags().contains(DiffFlags::NOT_BINARY) {
178+
new_file_content(&newfile_path)
179+
.unwrap_or(String::from("file not found"))
180+
} else {
181+
String::from("binary file")
182+
};
176183

177184
let mut patch = Patch::from_buffers(
178185
&[],
@@ -216,22 +223,22 @@ pub fn get_diff(repo_path: &str, p: String, stage: bool) -> FileDiff {
216223
res
217224
}
218225

219-
fn new_file_content(path: &Path) -> String {
226+
fn new_file_content(path: &Path) -> Option<String> {
220227
if let Ok(meta) = fs::symlink_metadata(path) {
221228
if meta.file_type().is_symlink() {
222-
return fs::read_link(path)
223-
.unwrap()
224-
.to_str()
225-
.unwrap()
226-
.to_string();
229+
if let Ok(path) = fs::read_link(path) {
230+
return Some(path.to_str()?.to_string());
231+
}
227232
} else if meta.file_type().is_file() {
228233
if let Ok(content) = fs::read_to_string(path) {
229-
return content;
234+
return Some(content);
230235
}
231236
}
232237
}
233238

234-
"file not found".to_string()
239+
debug!("could not read: {:?}", path);
240+
241+
None
235242
}
236243

237244
#[cfg(test)]

0 commit comments

Comments
 (0)