show diff of new untracked files

This commit is contained in:
Stephan Dilly 2020-03-23 13:55:59 +01:00
parent c410b8e4b1
commit 4730a8f498
2 changed files with 51 additions and 7 deletions

View file

@ -38,7 +38,6 @@ gitui
* [x] commit * [x] commit
* [x] [input polling in thread](assets/perf_compare.jpg) * [x] [input polling in thread](assets/perf_compare.jpg)
* [x] async git API for fluid control * [x] async git API for fluid control
* [ ] show content of new unstaged files
* [ ] discard untracked files (remove) * [ ] discard untracked files (remove)
* [ ] (un)staging selected hunks * [ ] (un)staging selected hunks

View file

@ -1,6 +1,9 @@
use super::utils; use super::utils;
use git2::{DiffFormat, DiffOptions}; use git2::{
Delta, DiffDelta, DiffFormat, DiffHunk, DiffOptions, Patch,
};
use scopetime::scope_time; use scopetime::scope_time;
use std::fs;
/// ///
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
@ -38,7 +41,7 @@ pub fn get_diff(p: String, stage: bool) -> Diff {
opt.pathspec(p); opt.pathspec(p);
let diff = if !stage { let diff = if !stage {
// diff against stage opt.include_untracked(true);
repo.diff_index_to_workdir(None, Some(&mut opt)).unwrap() repo.diff_index_to_workdir(None, Some(&mut opt)).unwrap()
} else { } else {
// diff against head // diff against head
@ -56,7 +59,7 @@ pub fn get_diff(p: String, stage: bool) -> Diff {
let mut res = Vec::new(); let mut res = Vec::new();
diff.print(DiffFormat::Patch, |_delta, _hunk, line| { let mut put = |line: git2::DiffLine| {
let origin = line.origin(); let origin = line.origin();
if origin != 'F' { if origin != 'F' {
@ -82,9 +85,51 @@ pub fn get_diff(p: String, stage: bool) -> Diff {
res.push(diff_line); 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) Diff(res)
} }