From 5ca712ff80c2e1be4fa3b6ff970fa051d67e4d83 Mon Sep 17 00:00:00 2001 From: Dave Date: Fri, 13 Jan 2023 08:05:51 -0500 Subject: [PATCH] Add no-verify commit command (#1375) * add no-verify option on commit action * make verify a bool flag on commit component --- src/components/commit.rs | 55 ++++++++++++++++++++++++++++++--------- src/keys/key_list.rs | 2 ++ src/keys/key_list_file.rs | 2 ++ src/strings.rs | 16 ++++++++++++ 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/components/commit.rs b/src/components/commit.rs index 74343ec2..ada176f6 100644 --- a/src/components/commit.rs +++ b/src/components/commit.rs @@ -55,6 +55,7 @@ pub struct CommitComponent { theme: SharedTheme, commit_msg_history_idx: usize, options: SharedOptions, + verify: bool, } const FIRST_LINE_LIMIT: usize = 50; @@ -85,6 +86,7 @@ impl CommitComponent { repo, commit_msg_history_idx: 0, options, + verify: true, } } @@ -236,6 +238,11 @@ impl CommitComponent { &mut self, msg: String, ) -> Result { + if !self.verify { + self.do_commit(&msg)?; + self.verify = true; + return Ok(CommitResult::ComitDone); + } if let HookResult::NotOk(e) = sync::hooks_pre_commit(&self.repo.borrow())? { @@ -256,18 +263,7 @@ impl CommitComponent { return Ok(CommitResult::Aborted); } - match &self.mode { - Mode::Normal => sync::commit(&self.repo.borrow(), &msg)?, - Mode::Amend(amend) => { - sync::amend(&self.repo.borrow(), *amend, &msg)? - } - Mode::Merge(ids) => { - sync::merge_commit(&self.repo.borrow(), &msg, ids)? - } - Mode::Revert => { - sync::commit_revert(&self.repo.borrow(), &msg)? - } - }; + self.do_commit(&msg)?; if let HookResult::NotOk(e) = sync::hooks_post_commit(&self.repo.borrow())? @@ -281,6 +277,22 @@ impl CommitComponent { Ok(CommitResult::ComitDone) } + fn do_commit(&self, msg: &str) -> Result<()> { + match &self.mode { + Mode::Normal => sync::commit(&self.repo.borrow(), msg)?, + Mode::Amend(amend) => { + sync::amend(&self.repo.borrow(), *amend, msg)? + } + Mode::Merge(ids) => { + sync::merge_commit(&self.repo.borrow(), msg, ids)? + } + Mode::Revert => { + sync::commit_revert(&self.repo.borrow(), msg)? + } + }; + Ok(()) + } + fn can_commit(&self) -> bool { !self.is_empty() && self.is_changed() } @@ -317,6 +329,9 @@ impl CommitComponent { Ok(()) } + fn toggle_verify(&mut self) { + self.verify = !self.verify + } } impl DrawableComponent for CommitComponent { @@ -350,6 +365,15 @@ impl Component for CommitComponent { true, )); + out.push(CommandInfo::new( + strings::commands::toggle_verify( + &self.key_config, + self.verify, + ), + self.can_commit(), + true, + )); + out.push(CommandInfo::new( strings::commands::commit_amend(&self.key_config), self.can_amend(), @@ -391,6 +415,12 @@ impl Component for CommitComponent { "commit error:", self.commit() ); + } else if key_match( + e, + self.key_config.keys.toggle_verify, + ) && self.can_commit() + { + self.toggle_verify(); } else if key_match( e, self.key_config.keys.commit_amend, @@ -459,6 +489,7 @@ impl Component for CommitComponent { .set_text(sync::merge_msg(&self.repo.borrow())?); Mode::Revert } + _ => { self.commit_template = get_config_string( &self.repo.borrow(), diff --git a/src/keys/key_list.rs b/src/keys/key_list.rs index 24c66e95..bb70db61 100644 --- a/src/keys/key_list.rs +++ b/src/keys/key_list.rs @@ -88,6 +88,7 @@ pub struct KeysList { pub log_tag_commit: GituiKeyEvent, pub log_mark_commit: GituiKeyEvent, pub commit_amend: GituiKeyEvent, + pub toggle_verify: GituiKeyEvent, pub copy: GituiKeyEvent, pub create_branch: GituiKeyEvent, pub rename_branch: GituiKeyEvent, @@ -170,6 +171,7 @@ impl Default for KeysList { log_tag_commit: GituiKeyEvent::new(KeyCode::Char('t'), KeyModifiers::empty()), log_mark_commit: GituiKeyEvent::new(KeyCode::Char(' '), KeyModifiers::empty()), commit_amend: GituiKeyEvent::new(KeyCode::Char('a'), KeyModifiers::CONTROL), + toggle_verify: GituiKeyEvent::new(KeyCode::Char('f'), KeyModifiers::CONTROL), copy: GituiKeyEvent::new(KeyCode::Char('y'), KeyModifiers::empty()), create_branch: GituiKeyEvent::new(KeyCode::Char('c'), KeyModifiers::empty()), rename_branch: GituiKeyEvent::new(KeyCode::Char('r'), KeyModifiers::empty()), diff --git a/src/keys/key_list_file.rs b/src/keys/key_list_file.rs index 9ac19e4f..24238ce6 100644 --- a/src/keys/key_list_file.rs +++ b/src/keys/key_list_file.rs @@ -59,6 +59,7 @@ pub struct KeysListFile { pub log_tag_commit: Option, pub log_mark_commit: Option, pub commit_amend: Option, + pub toggle_verify: Option, pub copy: Option, pub create_branch: Option, pub rename_branch: Option, @@ -150,6 +151,7 @@ impl KeysListFile { log_tag_commit: self.log_tag_commit.unwrap_or(default.log_tag_commit), log_mark_commit: self.log_mark_commit.unwrap_or(default.log_mark_commit), commit_amend: self.commit_amend.unwrap_or(default.commit_amend), + toggle_verify: self.toggle_verify.unwrap_or(default.toggle_verify), copy: self.copy.unwrap_or(default.copy), create_branch: self.create_branch.unwrap_or(default.create_branch), rename_branch: self.rename_branch.unwrap_or(default.rename_branch), diff --git a/src/strings.rs b/src/strings.rs index c7cf5d79..df7cceef 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -883,6 +883,22 @@ pub mod commands { ) .hide_help() } + pub fn toggle_verify( + key_config: &SharedKeyConfig, + current_verify: bool, + ) -> CommandText { + let verb = if current_verify { "disable" } else { "enable" }; + CommandText::new( + format!( + "{} hooks [{}]", + verb, + key_config.get_hint(key_config.keys.toggle_verify), + ), + "toggle running on commit hooks (available in commit popup)", + CMD_GROUP_COMMIT_POPUP, + ) + } + pub fn commit_amend(key_config: &SharedKeyConfig) -> CommandText { CommandText::new( format!(