Fix/git commit silently fail missing username email (#2878)

* fix: Commit on freshly initialized repository.

Fixes #2779

* fix: Prompt error message when Git user.name or user.email is missing.

Fixes #1277.

* Update CHANGELOG.md

---------

Co-authored-by: Alexandre Leblanc <a.leblanc@kitai.dev>
This commit is contained in:
Alexandre Leblanc 2024-01-02 04:44:52 -05:00 committed by GitHub
parent e1f3892729
commit 83120215ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 13 deletions

View File

@ -4,6 +4,7 @@
### Features/Changes
- [#2723](https://github.com/lapce/lapce/pull/2723): Line wrapping based on width (no column-based yet)
- [#1277](https://github.com/lapce/lapce/pull/1277): Error message prompted on missing git user.email and/or user.name
### Bug Fixes
- [#2779](https://github.com/lapce/lapce/pull/2779): Fix files detection on fresh git/VCS repository

View File

@ -13,6 +13,7 @@
use alacritty_terminal::{event::WindowSize, event_loop::Msg};
use anyhow::{anyhow, Context, Result};
use crossbeam_channel::Sender;
use git2::ErrorCode::NotFound;
use git2::{build::CheckoutBuilder, DiffOptions, Oid, Repository};
use grep_matcher::Matcher;
use grep_regex::RegexMatcherBuilder;
@ -31,7 +32,9 @@
RequestId, RpcError,
};
use lapce_xi_rope::Rope;
use lsp_types::{Position, Range, TextDocumentItem, Url};
use lsp_types::{
MessageType, Position, Range, ShowMessageParams, TextDocumentItem, Url,
};
use parking_lot::Mutex;
use crate::{
@ -284,7 +287,15 @@ fn handle_notification(&mut self, rpc: ProxyNotification) {
if let Some(workspace) = self.workspace.as_ref() {
match git_commit(workspace, &message, diffs) {
Ok(()) => (),
Err(e) => eprintln!("{e:?}"),
Err(e) => {
self.core_rpc.show_message(
"Git Commit failure".to_owned(),
ShowMessageParams {
typ: MessageType::ERROR,
message: e.to_string(),
},
);
}
}
}
}
@ -1205,18 +1216,35 @@ fn git_commit(
index.write()?;
let tree = index.write_tree()?;
let tree = repo.find_tree(tree)?;
let signature = repo.signature()?;
let parent = repo.head()?.peel_to_commit()?;
repo.commit(
Some("HEAD"),
&signature,
&signature,
message,
&tree,
&[&parent],
)?;
Ok(())
match repo.signature() {
Ok(signature) => {
let parents = repo
.head()
.and_then(|head| Ok(vec![head.peel_to_commit()?]))
.unwrap_or(vec![]);
let parents_refs = parents.iter().collect::<Vec<_>>();
repo.commit(
Some("HEAD"),
&signature,
&signature,
message,
&tree,
&parents_refs,
)?;
Ok(())
}
Err(e) => match e.code() {
NotFound => Err(anyhow!(
"No user.name and/or user.email configured for this git repository."
)),
_ => Err(anyhow!(
"Error while creating commit's signature: {}",
e.message()
)),
},
}
}
fn git_checkout(workspace_path: &Path, reference: &str) -> Result<()> {