work on unstaging

This commit is contained in:
Stephan Dilly 2020-03-18 17:25:19 +01:00
parent 034c2533af
commit 9ab8a9b970
2 changed files with 58 additions and 23 deletions

View file

@ -5,7 +5,7 @@ use crate::{
git_utils::{self, Diff, DiffLine, DiffLineType}, git_utils::{self, Diff, DiffLine, DiffLineType},
}; };
use crossterm::event::{Event, KeyCode, MouseEvent}; use crossterm::event::{Event, KeyCode, MouseEvent};
use git2::{IndexAddOption, StatusShow}; use git2::StatusShow;
use itertools::Itertools; use itertools::Itertools;
use std::{borrow::Cow, path::Path}; use std::{borrow::Cow, path::Path};
use tui::{ use tui::{
@ -226,7 +226,7 @@ impl App {
} }
if ev == Event::Key(KeyCode::Enter.into()) { if ev == Event::Key(KeyCode::Enter.into()) {
self.index_add(); self.index_add_remove();
} }
} }
} }
@ -273,31 +273,20 @@ impl App {
self.update_diff(); self.update_diff();
} }
fn index_add(&mut self) { fn index_add_remove(&mut self) {
if self.index_wd.focused() { if self.index_wd.focused() {
if let Some(i) = self.index_wd.selection() { if let Some(i) = self.index_wd.selection() {
let repo = git_utils::repo();
let mut index = repo.index().unwrap();
let path = Path::new(i.path.as_str()); let path = Path::new(i.path.as_str());
let cb = if git_utils::stage_add(path) {
&mut |p: &Path, _matched_spec: &[u8]| -> i32 { self.update();
if p == path { }
0 }
} else { } else {
1 if let Some(i) = self.index.selection() {
} let path = Path::new(i.path.as_str());
};
if let Ok(_) = index.add_all( if git_utils::stage_reset(path) {
path,
IndexAddOption::DISABLE_PATHSPEC_MATCH
| IndexAddOption::CHECK_PATHSPEC,
Some(cb as &mut git2::IndexMatchedPath),
) {
index.write().unwrap();
self.update(); self.update();
} }
} }

View file

@ -1,5 +1,6 @@
use git2::{ use git2::{
DiffFormat, DiffOptions, Repository, StatusOptions, StatusShow, DiffFormat, DiffOptions, IndexAddOption, ObjectType, Repository,
StatusOptions, StatusShow,
}; };
use std::path::Path; use std::path::Path;
@ -134,3 +135,48 @@ pub fn index_empty() -> bool {
statuses.is_empty() statuses.is_empty()
} }
pub fn stage_add(path: &Path) -> bool {
let repo = repo();
let mut index = repo.index().unwrap();
let cb = &mut |p: &Path, _matched_spec: &[u8]| -> i32 {
if p == path {
0
} else {
1
}
};
let cb = Some(cb as &mut git2::IndexMatchedPath);
let flags = IndexAddOption::DISABLE_PATHSPEC_MATCH
| IndexAddOption::CHECK_PATHSPEC;
if let Ok(_) = index.add_all(path, flags, cb) {
index.write().unwrap();
return true;
}
false
}
pub fn stage_reset(path: &Path) -> bool {
let repo = repo();
let mut index = repo.index().unwrap();
let reference = repo.head().unwrap();
let obj = repo
.find_object(
reference.target().unwrap(),
Some(ObjectType::Commit),
)
.unwrap();
if let Ok(_) = repo.reset_default(Some(&obj), &[path]) {
return true;
}
false
}