faster diff filtering

This commit is contained in:
Stephan Dilly 2020-03-16 20:30:19 +01:00
parent b9ce02e79a
commit dd64532814
4 changed files with 23 additions and 17 deletions

View File

@ -7,6 +7,7 @@ terminal ui (tui) frontend for git written in rust
* [x] show files on index
* [x] colorize diff
* [x] only show diff of selected file
* [ ] allow selecting/diff index items
* [ ] allow scrolling diff
* [ ] support staging/unstaging
* [ ] support committing

View File

@ -1,5 +1,6 @@
use crate::git_utils;
use crossterm::event::{Event, KeyCode};
use git2::{DiffFormat, Repository, Status};
use git2::{DiffFormat, DiffOptions, Repository, Status};
use std::cmp;
use std::path::Path;
use tui::{
@ -10,7 +11,7 @@ use tui::{
Frame,
};
#[derive(Copy, Clone,PartialEq)]
#[derive(Copy, Clone, PartialEq)]
pub enum DiffLineType {
None,
Header,
@ -24,13 +25,13 @@ impl Default for DiffLineType {
}
}
#[derive(Default,PartialEq)]
#[derive(Default, PartialEq)]
pub struct DiffLine {
content: String,
line_type: DiffLineType,
}
#[derive(Default,PartialEq)]
#[derive(Default, PartialEq)]
pub struct Diff(Vec<DiffLine>);
#[derive(Default)]
@ -73,12 +74,10 @@ impl App {
continue;
}
if status.is_index_new() || status.is_index_modified() {
if git_utils::on_index(&status) {
self.index_items
.push(format!("{} ({:?})", e.path().unwrap().to_string(), status))
}
if status.is_wt_new() || status.is_wt_modified() {
} else {
self.status_items.push(e.path().unwrap().to_string())
}
}
@ -94,7 +93,7 @@ impl App {
///
fn update_diff(&mut self) {
let new_diff=match self.status_select {
let new_diff = match self.status_select {
Some(i) => get_diff(Path::new(self.status_items[i].as_str())),
None => Diff::default(),
};
@ -229,17 +228,16 @@ fn get_diff(p: &Path) -> Diff {
panic!("bare repo")
}
let diff = repo.diff_index_to_workdir(None, None).unwrap();
let mut opt = DiffOptions::new();
opt.pathspec(p);
let diff = repo
.diff_index_to_workdir(None, Some(&mut opt))
.unwrap();
let mut res = Vec::new();
diff.print(DiffFormat::Patch, |delta, _hunk, line| {
if p != delta.old_file().path().unwrap() {
return true;
}
if p != delta.new_file().path().unwrap() {
return true;
}
diff.print(DiffFormat::Patch, |_delta, _hunk, line| {
let line_type = match line.origin() {
'H' => DiffLineType::Header,

6
src/git_utils.rs Normal file
View File

@ -0,0 +1,6 @@
use git2::Status;
///
pub fn on_index(s:&Status)->bool{
s.is_index_new() || s.is_index_modified()
}

View File

@ -1,5 +1,6 @@
mod app;
mod poll;
mod git_utils;
use app::App;
use crossterm::{