Skip to content

keep commit msg on hook-fail #1036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion asyncgit/src/sync/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
50 changes: 27 additions & 23 deletions src/components/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ use tui::{
Frame,
};

enum CommitResult {
ComitDone,
Aborted,
}

enum Mode {
Normal,
Amend(CommitId),
Expand Down Expand Up @@ -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())?
{
Expand All @@ -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) =
Expand All @@ -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())?
{
Expand All @@ -231,11 +239,7 @@ impl CommitComponent {
)));
}

self.hide();

self.queue.push(InternalEvent::Update(NeedsUpdate::ALL));

Ok(())
Ok(CommitResult::ComitDone)
}

fn can_commit(&self) -> bool {
Expand Down