Skip to content

Commit 6249491

Browse files
author
Stephan Dilly
authored
keep commit msg on hook-fail (#1036)
fixes #1035
1 parent cd639b2 commit 6249491

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Fixed
11+
- Keep commit message when pre-commit hook fails ([#1035](https://github.com/extrawurst/gitui/issues/1035))
12+
1013
## [0.19] - 2021-12-08 - Bare Repo Support
1114

1215
**finder highlighting matches**

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
- Fast and intuitive **keyboard only** control
4444
- Context based help (**no need to memorize** tons of hot-keys)
45-
- Inspect, commit, and amend changes (incl. hooks: _commit-msg_/_post-commit_)
45+
- Inspect, commit, and amend changes (incl. hooks: _pre-commit_,_commit-msg_,_post-commit_)
4646
- Stage, unstage, revert and reset files, hunks and lines
4747
- Stashing (save, pop, apply, drop, and inspect)
4848
- Push/Fetch to/from remote

asyncgit/src/sync/commit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(crate) fn signature_allow_undefined_name(
6060
signature
6161
}
6262

63-
/// this does not run any git hooks
63+
/// this does not run any git hooks, git-hooks have to be executed manually, checkout `hooks_commit_msg` for example
6464
pub fn commit(repo_path: &RepoPath, msg: &str) -> Result<CommitId> {
6565
scope_time!("commit");
6666

src/components/commit.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ use tui::{
3030
Frame,
3131
};
3232

33+
enum CommitResult {
34+
ComitDone,
35+
Aborted,
36+
}
37+
3338
enum Mode {
3439
Normal,
3540
Amend(CommitId),
@@ -175,11 +180,23 @@ impl CommitComponent {
175180
}
176181

177182
let msg = self.input.get_text().to_string();
178-
self.input.clear();
179-
self.commit_with_msg(msg)
183+
184+
if matches!(
185+
self.commit_with_msg(msg)?,
186+
CommitResult::ComitDone
187+
) {
188+
self.hide();
189+
self.queue.push(InternalEvent::Update(NeedsUpdate::ALL));
190+
self.input.clear();
191+
}
192+
193+
Ok(())
180194
}
181195

182-
fn commit_with_msg(&mut self, msg: String) -> Result<()> {
196+
fn commit_with_msg(
197+
&mut self,
198+
msg: String,
199+
) -> Result<CommitResult> {
183200
if let HookResult::NotOk(e) =
184201
sync::hooks_pre_commit(&self.repo.borrow())?
185202
{
@@ -188,7 +205,7 @@ impl CommitComponent {
188205
"pre-commit hook error:\n{}",
189206
e
190207
)));
191-
return Ok(());
208+
return Ok(CommitResult::Aborted);
192209
}
193210
let mut msg = message_prettify(msg, Some(b'#'))?;
194211
if let HookResult::NotOk(e) =
@@ -199,28 +216,19 @@ impl CommitComponent {
199216
"commit-msg hook error:\n{}",
200217
e
201218
)));
202-
return Ok(());
219+
return Ok(CommitResult::Aborted);
203220
}
204221

205-
let res = match &self.mode {
206-
Mode::Normal => sync::commit(&self.repo.borrow(), &msg),
222+
match &self.mode {
223+
Mode::Normal => sync::commit(&self.repo.borrow(), &msg)?,
207224
Mode::Amend(amend) => {
208-
sync::amend(&self.repo.borrow(), *amend, &msg)
225+
sync::amend(&self.repo.borrow(), *amend, &msg)?
209226
}
210227
Mode::Merge(ids) => {
211-
sync::merge_commit(&self.repo.borrow(), &msg, ids)
228+
sync::merge_commit(&self.repo.borrow(), &msg, ids)?
212229
}
213230
};
214231

215-
if let Err(e) = res {
216-
log::error!("commit error: {}", &e);
217-
self.queue.push(InternalEvent::ShowErrorMsg(format!(
218-
"commit failed:\n{}",
219-
&e
220-
)));
221-
return Ok(());
222-
}
223-
224232
if let HookResult::NotOk(e) =
225233
sync::hooks_post_commit(&self.repo.borrow())?
226234
{
@@ -231,11 +239,7 @@ impl CommitComponent {
231239
)));
232240
}
233241

234-
self.hide();
235-
236-
self.queue.push(InternalEvent::Update(NeedsUpdate::ALL));
237-
238-
Ok(())
242+
Ok(CommitResult::ComitDone)
239243
}
240244

241245
fn can_commit(&self) -> bool {

0 commit comments

Comments
 (0)