|
3 | 3 | use super::utils;
|
4 | 4 | use crate::hash;
|
5 | 5 | use git2::{
|
6 |
| - Delta, Diff, DiffDelta, DiffFormat, DiffHunk, DiffOptions, Patch, |
7 |
| - Repository, |
| 6 | + Delta, Diff, DiffDelta, DiffFlags, DiffFormat, DiffHunk, |
| 7 | + DiffOptions, Patch, Repository, |
8 | 8 | };
|
| 9 | +use log::debug; |
9 | 10 | use scopetime::scope_time;
|
10 | 11 | use std::{fs, path::Path};
|
11 | 12 |
|
@@ -172,7 +173,13 @@ pub fn get_diff(repo_path: &str, p: String, stage: bool) -> FileDiff {
|
172 | 173 | let newfile_path =
|
173 | 174 | repo_path.join(delta.new_file().path().unwrap());
|
174 | 175 |
|
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 | + }; |
176 | 183 |
|
177 | 184 | let mut patch = Patch::from_buffers(
|
178 | 185 | &[],
|
@@ -216,22 +223,22 @@ pub fn get_diff(repo_path: &str, p: String, stage: bool) -> FileDiff {
|
216 | 223 | res
|
217 | 224 | }
|
218 | 225 |
|
219 |
| -fn new_file_content(path: &Path) -> String { |
| 226 | +fn new_file_content(path: &Path) -> Option<String> { |
220 | 227 | if let Ok(meta) = fs::symlink_metadata(path) {
|
221 | 228 | 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 | + } |
227 | 232 | } else if meta.file_type().is_file() {
|
228 | 233 | if let Ok(content) = fs::read_to_string(path) {
|
229 |
| - return content; |
| 234 | + return Some(content); |
230 | 235 | }
|
231 | 236 | }
|
232 | 237 | }
|
233 | 238 |
|
234 |
| - "file not found".to_string() |
| 239 | + debug!("could not read: {:?}", path); |
| 240 | + |
| 241 | + None |
235 | 242 | }
|
236 | 243 |
|
237 | 244 | #[cfg(test)]
|
|
0 commit comments