From 1875723bd000d4921f4015d4bf965c3a1c5d0c38 Mon Sep 17 00:00:00 2001
From: Stephan Dilly <dilly.stephan@gmail.com>
Date: Thu, 9 Dec 2021 20:51:47 +0100
Subject: [PATCH 1/2] do not clear input on failing commit-hook

---
 README.md                   |  2 +-
 asyncgit/src/sync/commit.rs |  2 +-
 src/components/commit.rs    | 50 ++++++++++++++++++++-----------------
 3 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/README.md b/README.md
index a1aeadb1ec..68a3cc8ec4 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 25d733f10a..558df51685 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<CommitId> {
 	scope_time!("commit");
 
diff --git a/src/components/commit.rs b/src/components/commit.rs
index 670c52b059..fd58db2812 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<CommitResult> {
 		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 {

From 3acfed06f485d577aa9058520c2b136b8f353e1f Mon Sep 17 00:00:00 2001
From: Stephan Dilly <dilly.stephan@gmail.com>
Date: Thu, 9 Dec 2021 20:52:42 +0100
Subject: [PATCH 2/2] changlog

---
 CHANGELOG.md | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 39fae0adc5..5b553db4cd 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**