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] show files on index
* [x] colorize diff * [x] colorize diff
* [x] only show diff of selected file * [x] only show diff of selected file
* [ ] allow selecting/diff index items
* [ ] allow scrolling diff * [ ] allow scrolling diff
* [ ] support staging/unstaging * [ ] support staging/unstaging
* [ ] support committing * [ ] support committing

View File

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