From 63785873e4edc777ed7bafc33dc83bb48a9ea7a8 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Thu, 19 Mar 2020 01:25:41 +0100 Subject: [PATCH] can rest files on index (revert) --- README.md | 1 - src/app.rs | 49 ++++++++++++++++++++++++++++++++++++----- src/components/index.rs | 14 ++++-------- src/git_utils.rs | 17 ++++++++++++-- 4 files changed, 62 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 032f340a..a52a8e08 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ Over the last 2 years my go to GUI tool for this was [fork](https://git-fork.com * [x] (un)stage files * [x] inspect diffs * [x] commit -* [ ] reset changes in status * [ ] input polling in thread * [ ] file watcher instead of polling git * [ ] log view diff --git a/src/app.rs b/src/app.rs index 4533cd6e..959cc4be 100644 --- a/src/app.rs +++ b/src/app.rs @@ -173,14 +173,33 @@ impl App { } fn commands(&self) -> Vec { + let mut res = Vec::new(); if !self.commit.is_visible() { - vec![CommandInfo { + if self.index_wd.focused() { + let some_selection = + self.index_wd.selection().is_some(); + res.push(CommandInfo { + name: "Stage File [enter]".to_string(), + enabled: some_selection, + }); + res.push(CommandInfo { + name: "Reset File [D]".to_string(), + enabled: some_selection, + }); + } else if self.index.focused() { + res.push(CommandInfo { + name: "Unstage File [enter]".to_string(), + enabled: self.index.selection().is_some(), + }); + } + + res.push(CommandInfo { name: "Quit [esc,q]".to_string(), enabled: true, - }] - } else { - Vec::new() + }); } + + res } /// @@ -225,8 +244,14 @@ impl App { self.scroll(false); } - if ev == Event::Key(KeyCode::Enter.into()) { - self.index_add_remove(); + if let Event::Key(e) = ev { + if e.code == KeyCode::Enter { + self.index_add_remove(); + } + } + + if ev == Event::Key(KeyCode::Char('D').into()) { + self.index_reset(); } } } @@ -293,6 +318,18 @@ impl App { } } + fn index_reset(&mut self) { + if self.index_wd.focused() { + if let Some(i) = self.index_wd.selection() { + let path = Path::new(i.path.as_str()); + + if git_utils::index_reset(path) { + self.update(); + } + } + } + } + fn scroll(&mut self, inc: bool) { if inc { self.offset = diff --git a/src/components/index.rs b/src/components/index.rs index 12f95063..8cfef0a4 100644 --- a/src/components/index.rs +++ b/src/components/index.rs @@ -86,16 +86,10 @@ impl Component for IndexComponent { fn commands(&self) -> Vec { if self.focused { - return vec![ - CommandInfo { - name: "Scroll [↑↓]".to_string(), - enabled: self.items.len() > 0, - }, - CommandInfo { - name: "Stage File [enter]".to_string(), - enabled: self.selection.is_some(), - }, - ]; + return vec![CommandInfo { + name: "Scroll [↑↓]".to_string(), + enabled: self.items.len() > 0, + }]; } Vec::new() diff --git a/src/git_utils.rs b/src/git_utils.rs index a6a5a9f0..441348ec 100644 --- a/src/git_utils.rs +++ b/src/git_utils.rs @@ -1,6 +1,6 @@ use git2::{ - DiffFormat, DiffOptions, IndexAddOption, ObjectType, Repository, - StatusOptions, StatusShow, + build::CheckoutBuilder, DiffFormat, DiffOptions, IndexAddOption, + ObjectType, Repository, StatusOptions, StatusShow, }; use std::path::Path; @@ -178,3 +178,16 @@ pub fn stage_reset(path: &Path) -> bool { false } + +pub fn index_reset(path: &Path) -> bool { + let repo = repo(); + + let mut checkout_opts = CheckoutBuilder::new(); + checkout_opts.path(&path).force(); + + if let Ok(_) = repo.checkout_head(Some(&mut checkout_opts)) { + return true; + } + + false +}