From 0c45c4b6471c8660ca8c7adee3e2e2195e0ae2f0 Mon Sep 17 00:00:00 2001 From: ConcelareDev <113200622+ConcelareDev@users.noreply.github.com> Date: Wed, 20 Mar 2024 20:58:39 +0000 Subject: [PATCH 1/7] Updated: - Commit Comments Will Now Respect the Comments Prefix in the Config --- asyncgit/src/sync/commit.rs | 19 ++++++++++++++++++- asyncgit/src/sync/mod.rs | 2 +- src/popups/commit.rs | 6 +++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/asyncgit/src/sync/commit.rs b/asyncgit/src/sync/commit.rs index b2b7a47440..7d15f5142e 100644 --- a/asyncgit/src/sync/commit.rs +++ b/asyncgit/src/sync/commit.rs @@ -1,9 +1,10 @@ +//! Git Api for Commits use super::{CommitId, RepoPath}; use crate::{ error::Result, sync::{repository::repo, utils::get_head_repo}, }; -use git2::{ErrorCode, ObjectType, Repository, Signature}; +use git2::{ErrorCode, message_prettify, ObjectType, Repository, Signature}; use scopetime::scope_time; /// @@ -119,6 +120,22 @@ pub fn tag_commit( Ok(c) } +/// Loads the comment prefix from config & uses it to prettify commit messages +pub fn commit_message_prettify( + repo_path: &RepoPath, + message: String, +) -> Result { + let char = repo(repo_path)? + .config()? + .get_string("core.commentChar")? + .as_bytes() + .first() + .unwrap() + .to_owned(); + + Ok(message_prettify(message, Some(char))?) +} + #[cfg(test)] mod tests { use crate::error::Result; diff --git a/asyncgit/src/sync/mod.rs b/asyncgit/src/sync/mod.rs index c188f28c63..dcc3419ec7 100644 --- a/asyncgit/src/sync/mod.rs +++ b/asyncgit/src/sync/mod.rs @@ -5,7 +5,7 @@ pub mod blame; pub mod branch; -mod commit; +pub mod commit; mod commit_details; pub mod commit_files; mod commit_filter; diff --git a/src/popups/commit.rs b/src/popups/commit.rs index cb68bd1f7c..db7bf2316b 100644 --- a/src/popups/commit.rs +++ b/src/popups/commit.rs @@ -32,6 +32,7 @@ use std::{ path::PathBuf, str::FromStr, }; +use asyncgit::sync::commit::commit_message_prettify; use super::ExternalEditorPopup; @@ -195,7 +196,7 @@ impl CommitPopup { drop(file); std::fs::remove_file(&file_path)?; - message = message_prettify(message, Some(b'#'))?; + message = commit_message_prettify(&self.repo.borrow(), message)?; self.input.set_text(message); self.input.show()?; @@ -254,8 +255,7 @@ impl CommitPopup { } } - //TODO: honor `core.commentChar` - let mut msg = message_prettify(msg, Some(b'#'))?; + let mut msg = commit_message_prettify(&self.repo.borrow(), msg)?; if verify { // run commit message check hook - can reject commit From a189c50c4c998e803a0558c9e5082de1bd3f6c7c Mon Sep 17 00:00:00 2001 From: Concelare <113200622+Concelare@users.noreply.github.com> Date: Thu, 21 Mar 2024 03:13:39 +0000 Subject: [PATCH 2/7] Updated: - No longer uses unwrap() --- asyncgit/src/sync/commit.rs | 6 ++++-- src/popups/commit.rs | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/asyncgit/src/sync/commit.rs b/asyncgit/src/sync/commit.rs index 7d15f5142e..01688391c4 100644 --- a/asyncgit/src/sync/commit.rs +++ b/asyncgit/src/sync/commit.rs @@ -4,7 +4,9 @@ use crate::{ error::Result, sync::{repository::repo, utils::get_head_repo}, }; -use git2::{ErrorCode, message_prettify, ObjectType, Repository, Signature}; +use git2::{ + message_prettify, ErrorCode, ObjectType, Repository, Signature, +}; use scopetime::scope_time; /// @@ -130,7 +132,7 @@ pub fn commit_message_prettify( .get_string("core.commentChar")? .as_bytes() .first() - .unwrap() + .unwrap_or(&b"#"[0]) .to_owned(); Ok(message_prettify(message, Some(char))?) diff --git a/src/popups/commit.rs b/src/popups/commit.rs index db7bf2316b..36cef4e33f 100644 --- a/src/popups/commit.rs +++ b/src/popups/commit.rs @@ -11,8 +11,9 @@ use crate::{ ui::style::SharedTheme, }; use anyhow::{bail, Ok, Result}; +use asyncgit::sync::commit::commit_message_prettify; use asyncgit::{ - cached, message_prettify, + cached, sync::{ self, get_config_string, CommitId, HookResult, PrepareCommitMsgSource, RepoPathRef, RepoState, @@ -32,7 +33,6 @@ use std::{ path::PathBuf, str::FromStr, }; -use asyncgit::sync::commit::commit_message_prettify; use super::ExternalEditorPopup; @@ -196,7 +196,8 @@ impl CommitPopup { drop(file); std::fs::remove_file(&file_path)?; - message = commit_message_prettify(&self.repo.borrow(), message)?; + message = + commit_message_prettify(&self.repo.borrow(), message)?; self.input.set_text(message); self.input.show()?; @@ -255,7 +256,8 @@ impl CommitPopup { } } - let mut msg = commit_message_prettify(&self.repo.borrow(), msg)?; + let mut msg = + commit_message_prettify(&self.repo.borrow(), msg)?; if verify { // run commit message check hook - can reject commit From 363c7f31137e187612c33cb3bc427b977a31a5cb Mon Sep 17 00:00:00 2001 From: Concelare <113200622+Concelare@users.noreply.github.com> Date: Thu, 21 Mar 2024 18:24:20 +0000 Subject: [PATCH 3/7] Fixed: - Conversion to u8 --- asyncgit/src/sync/commit.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/asyncgit/src/sync/commit.rs b/asyncgit/src/sync/commit.rs index 01688391c4..dc963d2ae9 100644 --- a/asyncgit/src/sync/commit.rs +++ b/asyncgit/src/sync/commit.rs @@ -127,15 +127,13 @@ pub fn commit_message_prettify( repo_path: &RepoPath, message: String, ) -> Result { - let char = repo(repo_path)? + let comment_char = repo(repo_path)? .config()? .get_string("core.commentChar")? - .as_bytes() - .first() - .unwrap_or(&b"#"[0]) - .to_owned(); + .chars() + .collect::>()[0] as u8; - Ok(message_prettify(message, Some(char))?) + Ok(message_prettify(message, Some(comment_char))?) } #[cfg(test)] From 4ceedd84d12e9096aeeb43b3f9caa85b977a4c66 Mon Sep 17 00:00:00 2001 From: Concelare <113200622+Concelare@users.noreply.github.com> Date: Thu, 21 Mar 2024 20:03:47 +0000 Subject: [PATCH 4/7] Fixed: - Nullable error when core.commentChar is not set --- asyncgit/src/sync/commit.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/asyncgit/src/sync/commit.rs b/asyncgit/src/sync/commit.rs index dc963d2ae9..4b59124a34 100644 --- a/asyncgit/src/sync/commit.rs +++ b/asyncgit/src/sync/commit.rs @@ -127,11 +127,13 @@ pub fn commit_message_prettify( repo_path: &RepoPath, message: String, ) -> Result { - let comment_char = repo(repo_path)? - .config()? - .get_string("core.commentChar")? - .chars() - .collect::>()[0] as u8; + let comment_char_string = + repo(repo_path)?.config()?.get_string("core.commentChar"); + + let comment_char = + comment_char_string.map_or(b'#', |char_string| { + char_string.chars().collect::>()[0] as u8 + }); Ok(message_prettify(message, Some(comment_char))?) } From 3fd45933deafbf8948c8060662794075c8dcf50f Mon Sep 17 00:00:00 2001 From: Concelare <113200622+Concelare@users.noreply.github.com> Date: Fri, 22 Mar 2024 18:34:47 +0000 Subject: [PATCH 5/7] Updated: - Using getter instead of index access --- asyncgit/src/sync/commit.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/asyncgit/src/sync/commit.rs b/asyncgit/src/sync/commit.rs index 4b59124a34..6ddbaf5129 100644 --- a/asyncgit/src/sync/commit.rs +++ b/asyncgit/src/sync/commit.rs @@ -132,7 +132,12 @@ pub fn commit_message_prettify( let comment_char = comment_char_string.map_or(b'#', |char_string| { - char_string.chars().collect::>()[0] as u8 + char_string + .chars() + .collect::>() + .first() + .unwrap_or(&char::from('#')) + .to_owned() as u8 }); Ok(message_prettify(message, Some(comment_char))?) From af4868b2a89497fbaa89ab55bd6e02105af99d23 Mon Sep 17 00:00:00 2001 From: Concelare <113200622+Concelare@users.noreply.github.com> Date: Fri, 22 Mar 2024 18:42:41 +0000 Subject: [PATCH 6/7] Fixed: - Unneeded usage of char::from() --- asyncgit/src/sync/commit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asyncgit/src/sync/commit.rs b/asyncgit/src/sync/commit.rs index 6ddbaf5129..70254e50b5 100644 --- a/asyncgit/src/sync/commit.rs +++ b/asyncgit/src/sync/commit.rs @@ -136,7 +136,7 @@ pub fn commit_message_prettify( .chars() .collect::>() .first() - .unwrap_or(&char::from('#')) + .unwrap_or(&'#') .to_owned() as u8 }); From 785d05eb24ebd7a2d07586b6d5aa9414732f6258 Mon Sep 17 00:00:00 2001 From: Concelare <113200622+Concelare@users.noreply.github.com> Date: Fri, 22 Mar 2024 20:14:08 +0000 Subject: [PATCH 7/7] Updated: - Uses Map instead of double unwrap --- asyncgit/src/sync/commit.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/asyncgit/src/sync/commit.rs b/asyncgit/src/sync/commit.rs index 70254e50b5..36884a0bb5 100644 --- a/asyncgit/src/sync/commit.rs +++ b/asyncgit/src/sync/commit.rs @@ -127,18 +127,11 @@ pub fn commit_message_prettify( repo_path: &RepoPath, message: String, ) -> Result { - let comment_char_string = - repo(repo_path)?.config()?.get_string("core.commentChar"); - - let comment_char = - comment_char_string.map_or(b'#', |char_string| { - char_string - .chars() - .collect::>() - .first() - .unwrap_or(&'#') - .to_owned() as u8 - }); + let comment_char = repo(repo_path)? + .config()? + .get_string("core.commentChar") + .map(|char_string| char_string.chars().next())? + .unwrap_or('#') as u8; Ok(message_prettify(message, Some(comment_char))?) }