From 95c41dbf45bf7aa7818c3b8e27f58b038f759243 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sat, 24 Apr 2021 14:55:26 +0200 Subject: [PATCH] limit log message to first line of commit msg (#662) --- CHANGELOG.md | 1 + asyncgit/src/sync/commits_info.rs | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 654810ec..5fa30dde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - debug print when adding a file to ignore +- limit log messages in log tab ([#652](https://github.com/extrawurst/gitui/issues/652)) - fetch crashed when no upstream of branch is set ([#637](https://github.com/extrawurst/gitui/issues/637)) - `enter` key panics in empty remote branch list ([#643](https://github.com/extrawurst/gitui/issues/643)) diff --git a/asyncgit/src/sync/commits_info.rs b/asyncgit/src/sync/commits_info.rs index 3a20f3de..c1058715 100644 --- a/asyncgit/src/sync/commits_info.rs +++ b/asyncgit/src/sync/commits_info.rs @@ -115,17 +115,21 @@ pub fn get_commit_info( }) } -/// +/// if `message_limit` is set the message will be +/// limited to the first line and truncated to fit pub fn get_message( c: &Commit, - message_length_limit: Option, + message_limit: Option, ) -> String { let msg = String::from_utf8_lossy(c.message_bytes()); let msg = msg.trim(); - message_length_limit.map_or_else( + message_limit.map_or_else( || msg.to_string(), - |limit| msg.unicode_truncate(limit).0.to_string(), + |limit| { + let msg = msg.lines().next().unwrap_or_default(); + msg.unicode_truncate(limit).0.to_string() + }, ) } @@ -164,6 +168,25 @@ mod tests { Ok(()) } + #[test] + fn test_log_first_msg_line() -> Result<()> { + let file_path = Path::new("foo"); + let (_td, repo) = repo_init_empty().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); + + File::create(&root.join(file_path))?.write_all(b"a")?; + stage_add_file(repo_path, file_path).unwrap(); + let c1 = commit(repo_path, "subject\nbody").unwrap(); + + let res = get_commits_info(repo_path, &vec![c1], 50).unwrap(); + + assert_eq!(res.len(), 1); + assert_eq!(res[0].message.as_str(), "subject"); + + Ok(()) + } + #[test] fn test_invalid_utf8() -> Result<()> { let file_path = Path::new("foo");