Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ gemini = { workspace = true, optional = true }
hex = { workspace = true }
imara-diff = "0.1.7"
indicatif = "0.17.8"
infer = "0.16.0"
lazy_static = { workspace = true }
lru-mem = "0.3.0"
mercury = { workspace = true }
Expand Down
36 changes: 26 additions & 10 deletions libra/src/command/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{
collections::{HashMap, HashSet},
fmt,
io::{self, Write},
path::PathBuf,
path::{Path, PathBuf},
};

use clap::Parser;
Expand Down Expand Up @@ -216,15 +216,12 @@ pub async fn diff(
writeln!(w, "deleted file mode 100644").unwrap();
}

let old_index = old_hash.map_or("0000000".to_string(), |h| {
h.to_string()[0..8].to_string()
});
let new_index = new_hash.map_or("0000000".to_string(), |h| {
h.to_string()[0..8].to_string()
});
let old_index = old_hash.map_or("0000000".to_string(), |h| h.to_string()[0..8].to_string());
let new_index = new_hash.map_or("0000000".to_string(), |h| h.to_string()[0..8].to_string());
writeln!(w, "index {}..{}", old_index, new_index).unwrap();

// check is the content is valid utf-8 or maybe binary
let old_type = infer::get(&old_content);
let new_type = infer::get(&new_content);
match (
String::from_utf8(old_content),
String::from_utf8(new_content),
Expand All @@ -237,8 +234,8 @@ pub async fn diff(
writeln!(
w,
"Binary files a/{} and b/{} differ",
file.display(),
file.display()
file_display(&file, old_hash, old_type),
file_display(&file, new_hash, new_type)
)
.unwrap();
}
Expand All @@ -264,6 +261,25 @@ fn get_files_blobs(files: &[PathBuf]) -> Vec<(PathBuf, SHA1)> {
.collect()
}

// display file with type
fn file_display(file: &Path, hash: Option<&SHA1>, file_type: Option<infer::Type>) -> String {
let file_name = match hash {
Some(_) => file.display().to_string(),
None => "dev/null".to_string(),
};

if let Some(file_type) = file_type {
// Check if the file type is displayable in browser, like image, audio, video, etc.
if matches!(
file_type.matcher_type(),
infer::MatcherType::Audio | infer::MatcherType::Video | infer::MatcherType::Image
) {
return format!("{} ({})", file_name, file_type.mime_type()).to_string();
}
}
file_name
}

struct Line(Option<usize>);

impl fmt::Display for Line {
Expand Down