From b22d87c9f3012d6dd5bcbd971e446a3222cf2069 Mon Sep 17 00:00:00 2001 From: chiheb ben cheikh Date: Fri, 17 Apr 2026 15:13:31 +0100 Subject: [PATCH] fix: run pre-commit hook before opening commit dialog (#1711) The pre-commit hook was running after the user typed their commit message, wasting time on messages that would get rejected. Now it runs when the commit dialog opens, matching Git CLI behavior where pre-commit runs before the user types a message. The hook is extracted into a run_pre_commit_hook() helper to keep open() under the clippy::too_many_lines threshold. --- src/popups/commit.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/popups/commit.rs b/src/popups/commit.rs index b5dff7677c..efb0a5d7ec 100644 --- a/src/popups/commit.rs +++ b/src/popups/commit.rs @@ -235,19 +235,6 @@ impl CommitPopup { let verify = self.verify; self.verify = true; - if verify { - // run pre commit hook - can reject commit - if let HookResult::NotOk(e) = - sync::hooks_pre_commit(&self.repo.borrow())? - { - log::error!("pre-commit hook error: {e}"); - self.queue.push(InternalEvent::ShowErrorMsg( - format!("pre-commit hook error:\n{e}"), - )); - return Ok(CommitResult::Aborted); - } - } - let mut msg = commit_message_prettify(&self.repo.borrow(), msg)?; @@ -349,6 +336,21 @@ impl CommitPopup { self.verify = !self.verify; } + fn run_pre_commit_hook(&self) -> Result { + if self.verify { + if let HookResult::NotOk(e) = + sync::hooks_pre_commit(&self.repo.borrow())? + { + log::error!("pre-commit hook error: {e}"); + self.queue.push(InternalEvent::ShowErrorMsg( + format!("pre-commit hook error:\n{e}"), + )); + return Ok(false); + } + } + Ok(true) + } + pub fn open(&mut self, reword: Option) -> Result<()> { //only clear text if it was not a normal commit dlg before, so to preserve old commit msg that was edited if !matches!(self.mode, Mode::Normal) { @@ -442,6 +444,10 @@ impl CommitPopup { self.mode = mode; + if !self.run_pre_commit_hook()? { + return Ok(()); + } + let mut msg = self.input.get_text().to_string(); if let HookResult::NotOk(e) = sync::hooks_prepare_commit_msg( &self.repo.borrow(),