diff --git a/README.md b/README.md index 49a431b2..ebfeeb62 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,6 @@ gitui * [x] commit * [x] [input polling in thread](assets/perf_compare.jpg) * [x] async git API for fluid control -* [ ] show content of new unstaged files * [ ] discard untracked files (remove) * [ ] (un)staging selected hunks diff --git a/asyncgit/src/sync/diff.rs b/asyncgit/src/sync/diff.rs index f01b09b4..93001b7a 100644 --- a/asyncgit/src/sync/diff.rs +++ b/asyncgit/src/sync/diff.rs @@ -1,6 +1,9 @@ use super::utils; -use git2::{DiffFormat, DiffOptions}; +use git2::{ + Delta, DiffDelta, DiffFormat, DiffHunk, DiffOptions, Patch, +}; use scopetime::scope_time; +use std::fs; /// #[derive(Copy, Clone, PartialEq)] @@ -38,7 +41,7 @@ pub fn get_diff(p: String, stage: bool) -> Diff { opt.pathspec(p); let diff = if !stage { - // diff against stage + opt.include_untracked(true); repo.diff_index_to_workdir(None, Some(&mut opt)).unwrap() } else { // diff against head @@ -56,7 +59,7 @@ pub fn get_diff(p: String, stage: bool) -> Diff { let mut res = Vec::new(); - diff.print(DiffFormat::Patch, |_delta, _hunk, line| { + let mut put = |line: git2::DiffLine| { let origin = line.origin(); if origin != 'F' { @@ -82,9 +85,51 @@ pub fn get_diff(p: String, stage: bool) -> Diff { res.push(diff_line); } - true - }) - .unwrap(); + }; + + let new_file_diff = if diff.deltas().len() == 1 { + let delta: DiffDelta = diff.deltas().next().unwrap(); + + if delta.status() == Delta::Untracked { + let newfile_path = delta.new_file().path().unwrap(); + + let newfile_content = + fs::read_to_string(newfile_path).unwrap(); + + let mut patch = Patch::from_buffers( + &[], + None, + newfile_content.as_bytes(), + Some(newfile_path), + Some(&mut opt), + ) + .unwrap(); + + patch + .print(&mut |_delta, _hunk, line: git2::DiffLine| { + put(line); + true + }) + .unwrap(); + + true + } else { + false + } + } else { + false + }; + + if !new_file_diff { + diff.print( + DiffFormat::Patch, + |_, _, line: git2::DiffLine| { + put(line); + true + }, + ) + .unwrap(); + } Diff(res) }