From 80da95b6f59e042d375624e5cafa4d641a5a8970 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Thu, 27 Aug 2020 01:10:38 +0200 Subject: [PATCH] cleanup commit message line encodings to fix rendering commit msg (closes #245) --- asyncgit/src/sync/commit_details.rs | 42 +++++++++++++++--------- src/components/commit_details/details.rs | 4 +-- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/asyncgit/src/sync/commit_details.rs b/asyncgit/src/sync/commit_details.rs index 4f65ad7a..28a851db 100644 --- a/asyncgit/src/sync/commit_details.rs +++ b/asyncgit/src/sync/commit_details.rs @@ -37,21 +37,23 @@ pub struct CommitMessage { impl CommitMessage { /// pub fn from(s: &str) -> Self { - if let Some(idx) = s.find('\n') { - let (first, rest) = s.split_at(idx); - Self { - subject: first.to_string(), - body: if rest.is_empty() { - None - } else { - Some(rest.to_string()) - }, - } + let mut lines = s.lines(); + let subject = if let Some(subject) = lines.next() { + subject.to_string() } else { - Self { - subject: s.to_string(), - body: None, - } + String::new() + }; + + let body: Vec = + lines.map(|line| line.to_string()).collect(); + + Self { + subject, + body: if body.is_empty() { + None + } else { + Some(body.join("\n")) + }, } } @@ -112,7 +114,7 @@ pub fn get_commit_details( #[cfg(test)] mod tests { - use super::get_commit_details; + use super::{get_commit_details, CommitMessage}; use crate::error::Result; use crate::sync::{ commit, stage_add_file, tests::repo_init_empty, @@ -146,4 +148,14 @@ mod tests { Ok(()) } + + #[test] + fn test_msg_linefeeds() -> Result<()> { + let msg = CommitMessage::from("foo\nbar\r\ntest"); + + assert_eq!(msg.subject, String::from("foo"),); + assert_eq!(msg.body, Some(String::from("bar\ntest")),); + + Ok(()) + } } diff --git a/src/components/commit_details/details.rs b/src/components/commit_details/details.rs index 59eb4793..8d15bebd 100644 --- a/src/components/commit_details/details.rs +++ b/src/components/commit_details/details.rs @@ -477,13 +477,13 @@ mod tests { assert_eq!( get_wrapped_lines(&message_with_body, 7), vec![ - "Commit", "message", "", "First", "line", "Second", + "Commit", "message", "First", "line", "Second", "line" ] ); assert_eq!( get_wrapped_lines(&message_with_body, 14), - vec!["Commit message", "", "First line", "Second line"] + vec!["Commit message", "First line", "Second line"] ); } }