Fix Git diff rendering
This commit is contained in:
176
src/app/git.rs
176
src/app/git.rs
@@ -1,6 +1,5 @@
|
||||
use super::*;
|
||||
use std::collections::BTreeMap;
|
||||
use std::ffi::OsStr;
|
||||
use std::process::{Command, Output};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
@@ -483,43 +482,65 @@ fn worktree_kind(summary: gix::status::index_worktree::iter::Summary) -> GitChan
|
||||
}
|
||||
|
||||
fn load_diff(root: &Path, file: &GitFile) -> Result<GitDiff, String> {
|
||||
let mut sections = Vec::new();
|
||||
let repository = gix::discover(root).map_err(|error| error.to_string())?;
|
||||
let index = repository
|
||||
.index_or_empty()
|
||||
.map_err(|error| error.to_string())?;
|
||||
let index_path =
|
||||
gix::path::to_unix_separators_on_windows(gix::path::into_bstr(file.path.as_path()));
|
||||
let index_blob = index
|
||||
.entry_by_path(index_path.as_ref())
|
||||
.filter(|entry| {
|
||||
entry
|
||||
.mode
|
||||
.to_tree_entry_mode()
|
||||
.is_some_and(|mode| mode.is_blob_or_symlink())
|
||||
})
|
||||
.map(|entry| {
|
||||
repository
|
||||
.find_blob(entry.id)
|
||||
.map(|mut blob| blob.take_data())
|
||||
.map_err(|error| error.to_string())
|
||||
})
|
||||
.transpose()?;
|
||||
let mut lines = Vec::new();
|
||||
if file.staged_kind.is_some() {
|
||||
let output = run_git_path(
|
||||
root,
|
||||
&["diff", "--cached", "--no-ext-diff", "--no-color", "--"],
|
||||
&file.path,
|
||||
)?;
|
||||
sections.push(("Staged changes", output));
|
||||
let tree = repository
|
||||
.find_tree(
|
||||
repository
|
||||
.head_tree_id_or_empty()
|
||||
.map_err(|error| error.to_string())?,
|
||||
)
|
||||
.map_err(|error| error.to_string())?;
|
||||
let head_blob = tree
|
||||
.lookup_entry_by_path(&file.path)
|
||||
.map_err(|error| error.to_string())?
|
||||
.filter(|entry| entry.mode().is_blob_or_symlink())
|
||||
.map(|entry| {
|
||||
repository
|
||||
.find_blob(entry.object_id())
|
||||
.map(|mut blob| blob.take_data())
|
||||
.map_err(|error| error.to_string())
|
||||
})
|
||||
.transpose()?;
|
||||
lines.push(diff_section("Staged changes"));
|
||||
lines.extend(diff_blobs(
|
||||
head_blob.as_deref(),
|
||||
index_blob.as_deref(),
|
||||
&file.display_path,
|
||||
)?);
|
||||
}
|
||||
if file.worktree_kind.is_some() {
|
||||
let mut output = run_git_path(
|
||||
root,
|
||||
&["diff", "--no-ext-diff", "--no-color", "--"],
|
||||
&file.path,
|
||||
)?;
|
||||
if output.is_empty() && file.worktree_kind == Some(GitChangeKind::Added) {
|
||||
output = run_no_index_diff(root, &file.path)?;
|
||||
}
|
||||
sections.push(("Worktree changes", output));
|
||||
}
|
||||
let mut lines = Vec::new();
|
||||
for (label, content) in sections {
|
||||
lines.push(GitDiffLine {
|
||||
kind: GitDiffLineKind::Section,
|
||||
old_number: None,
|
||||
new_number: None,
|
||||
text: label.into(),
|
||||
});
|
||||
lines.extend(parse_unified_diff(&content));
|
||||
let worktree_blob = read_worktree_blob(&root.join(&file.path))?;
|
||||
lines.push(diff_section("Worktree changes"));
|
||||
lines.extend(diff_blobs(
|
||||
index_blob.as_deref(),
|
||||
worktree_blob.as_deref(),
|
||||
&file.display_path,
|
||||
)?);
|
||||
}
|
||||
if lines.is_empty() {
|
||||
lines.push(GitDiffLine {
|
||||
kind: GitDiffLineKind::Header,
|
||||
old_number: None,
|
||||
new_number: None,
|
||||
text: "No textual diff is available.".into(),
|
||||
});
|
||||
lines.push(diff_message("No textual diff is available."));
|
||||
}
|
||||
Ok(GitDiff {
|
||||
path: file.display_path.clone(),
|
||||
@@ -527,6 +548,76 @@ fn load_diff(root: &Path, file: &GitFile) -> Result<GitDiff, String> {
|
||||
})
|
||||
}
|
||||
|
||||
fn diff_section(text: &str) -> GitDiffLine {
|
||||
GitDiffLine {
|
||||
kind: GitDiffLineKind::Section,
|
||||
old_number: None,
|
||||
new_number: None,
|
||||
text: text.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn diff_message(text: &str) -> GitDiffLine {
|
||||
GitDiffLine {
|
||||
kind: GitDiffLineKind::Header,
|
||||
old_number: None,
|
||||
new_number: None,
|
||||
text: text.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn read_worktree_blob(path: &Path) -> Result<Option<Vec<u8>>, String> {
|
||||
match fs::symlink_metadata(path) {
|
||||
Ok(metadata) if metadata.file_type().is_symlink() => fs::read_link(path)
|
||||
.map(|target| Some(target.as_os_str().as_encoded_bytes().to_vec()))
|
||||
.map_err(|error| error.to_string()),
|
||||
Ok(metadata) if metadata.is_file() => {
|
||||
fs::read(path).map(Some).map_err(|error| error.to_string())
|
||||
}
|
||||
Ok(_) => Ok(None),
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
|
||||
Err(error) => Err(error.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn diff_blobs(
|
||||
before: Option<&[u8]>,
|
||||
after: Option<&[u8]>,
|
||||
path: &str,
|
||||
) -> Result<Vec<GitDiffLine>, String> {
|
||||
let before_label = before.map_or_else(|| "/dev/null".into(), |_| format!("a/{path}"));
|
||||
let after_label = after.map_or_else(|| "/dev/null".into(), |_| format!("b/{path}"));
|
||||
let before = before.unwrap_or_default();
|
||||
let after = after.unwrap_or_default();
|
||||
let mut lines = vec![
|
||||
diff_message(&format!("--- {before_label}")),
|
||||
diff_message(&format!("+++ {after_label}")),
|
||||
];
|
||||
if before == after {
|
||||
lines.push(diff_message("No textual diff is available."));
|
||||
return Ok(lines);
|
||||
}
|
||||
if before.iter().take(8_000).any(|byte| *byte == 0)
|
||||
|| after.iter().take(8_000).any(|byte| *byte == 0)
|
||||
{
|
||||
lines.push(diff_message("Binary file changed."));
|
||||
return Ok(lines);
|
||||
}
|
||||
let input = gix::diff::blob::InternedInput::new(before, after);
|
||||
let diff =
|
||||
gix::diff::blob::diff_with_slider_heuristics(gix::diff::blob::Algorithm::Histogram, &input);
|
||||
let output = gix::diff::blob::UnifiedDiff::new(
|
||||
&diff,
|
||||
&input,
|
||||
gix::diff::blob::unified_diff::ConsumeBinaryHunk::new(Vec::new(), "\n"),
|
||||
Default::default(),
|
||||
)
|
||||
.consume()
|
||||
.map_err(|error| error.to_string())?;
|
||||
lines.extend(parse_unified_diff(&String::from_utf8_lossy(&output)));
|
||||
Ok(lines)
|
||||
}
|
||||
|
||||
fn parse_unified_diff(diff: &str) -> Vec<GitDiffLine> {
|
||||
let mut old_number = None;
|
||||
let mut new_number = None;
|
||||
@@ -619,10 +710,6 @@ fn run_git_paths(root: &Path, arguments: &[&str], paths: &[PathBuf]) -> Result<S
|
||||
)
|
||||
}
|
||||
|
||||
fn run_git_path(root: &Path, arguments: &[&str], path: &Path) -> Result<String, String> {
|
||||
run_git_paths(root, arguments, &[path.to_path_buf()])
|
||||
}
|
||||
|
||||
fn run_git_args(root: &Path, arguments: &[&str]) -> Result<String, String> {
|
||||
let mut command = git_command(root);
|
||||
command.args(arguments);
|
||||
@@ -661,21 +748,6 @@ fn run_git_remote(root: &Path, action: &str, branch: Option<&str>) -> Result<(),
|
||||
finish_git(command.output(), action).map(|_| ())
|
||||
}
|
||||
|
||||
fn run_no_index_diff(root: &Path, path: &Path) -> Result<String, String> {
|
||||
let mut command = git_command(root);
|
||||
command.args(["diff", "--no-index", "--no-color", "--"]);
|
||||
command.arg(OsStr::new("/dev/null"));
|
||||
command.arg(root.join(path));
|
||||
let output = command
|
||||
.output()
|
||||
.map_err(|error| format!("Could not run Git diff: {error}"))?;
|
||||
if output.status.success() || output.status.code() == Some(1) {
|
||||
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
|
||||
} else {
|
||||
finish_git(Ok(output), "diff")
|
||||
}
|
||||
}
|
||||
|
||||
fn git_command(root: &Path) -> Command {
|
||||
let mut command = Command::new("git");
|
||||
command.arg("-C").arg(root).env("GIT_TERMINAL_PROMPT", "0");
|
||||
|
||||
Reference in New Issue
Block a user