From 6249491484ea6392cd35997c27a7722ef117d0ee Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Thu, 9 Dec 2021 21:12:31 +0100 Subject: [PATCH] keep commit msg on hook-fail (#1036) fixes #1035 --- CHANGELOG.md | 3 +++ README.md | 2 +- asyncgit/src/sync/commit.rs | 2 +- src/components/commit.rs | 50 ++++++++++++++++++++----------------- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39fae0ad..5b553db4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Fixed +- Keep commit message when pre-commit hook fails ([#1035](https://github.com/extrawurst/gitui/issues/1035)) + ## [0.19] - 2021-12-08 - Bare Repo Support **finder highlighting matches** diff --git a/README.md b/README.md index a1aeadb1..68a3cc8e 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ - Fast and intuitive **keyboard only** control - Context based help (**no need to memorize** tons of hot-keys) -- Inspect, commit, and amend changes (incl. hooks: _commit-msg_/_post-commit_) +- Inspect, commit, and amend changes (incl. hooks: _pre-commit_,_commit-msg_,_post-commit_) - Stage, unstage, revert and reset files, hunks and lines - Stashing (save, pop, apply, drop, and inspect) - Push/Fetch to/from remote diff --git a/asyncgit/src/sync/commit.rs b/asyncgit/src/sync/commit.rs index 25d733f1..558df516 100644 --- a/asyncgit/src/sync/commit.rs +++ b/asyncgit/src/sync/commit.rs @@ -60,7 +60,7 @@ pub(crate) fn signature_allow_undefined_name( signature } -/// this does not run any git hooks +/// this does not run any git hooks, git-hooks have to be executed manually, checkout `hooks_commit_msg` for example pub fn commit(repo_path: &RepoPath, msg: &str) -> Result { scope_time!("commit"); diff --git a/src/components/commit.rs b/src/components/commit.rs index 670c52b0..fd58db28 100644 --- a/src/components/commit.rs +++ b/src/components/commit.rs @@ -30,6 +30,11 @@ use tui::{ Frame, }; +enum CommitResult { + ComitDone, + Aborted, +} + enum Mode { Normal, Amend(CommitId), @@ -175,11 +180,23 @@ impl CommitComponent { } let msg = self.input.get_text().to_string(); - self.input.clear(); - self.commit_with_msg(msg) + + if matches!( + self.commit_with_msg(msg)?, + CommitResult::ComitDone + ) { + self.hide(); + self.queue.push(InternalEvent::Update(NeedsUpdate::ALL)); + self.input.clear(); + } + + Ok(()) } - fn commit_with_msg(&mut self, msg: String) -> Result<()> { + fn commit_with_msg( + &mut self, + msg: String, + ) -> Result { if let HookResult::NotOk(e) = sync::hooks_pre_commit(&self.repo.borrow())? { @@ -188,7 +205,7 @@ impl CommitComponent { "pre-commit hook error:\n{}", e ))); - return Ok(()); + return Ok(CommitResult::Aborted); } let mut msg = message_prettify(msg, Some(b'#'))?; if let HookResult::NotOk(e) = @@ -199,28 +216,19 @@ impl CommitComponent { "commit-msg hook error:\n{}", e ))); - return Ok(()); + return Ok(CommitResult::Aborted); } - let res = match &self.mode { - Mode::Normal => sync::commit(&self.repo.borrow(), &msg), + match &self.mode { + Mode::Normal => sync::commit(&self.repo.borrow(), &msg)?, Mode::Amend(amend) => { - sync::amend(&self.repo.borrow(), *amend, &msg) + sync::amend(&self.repo.borrow(), *amend, &msg)? } Mode::Merge(ids) => { - sync::merge_commit(&self.repo.borrow(), &msg, ids) + sync::merge_commit(&self.repo.borrow(), &msg, ids)? } }; - if let Err(e) = res { - log::error!("commit error: {}", &e); - self.queue.push(InternalEvent::ShowErrorMsg(format!( - "commit failed:\n{}", - &e - ))); - return Ok(()); - } - if let HookResult::NotOk(e) = sync::hooks_post_commit(&self.repo.borrow())? { @@ -231,11 +239,7 @@ impl CommitComponent { ))); } - self.hide(); - - self.queue.push(InternalEvent::Update(NeedsUpdate::ALL)); - - Ok(()) + Ok(CommitResult::ComitDone) } fn can_commit(&self) -> bool {