cleanup commit message line encodings to fix rendering commit msg (closes #245)

This commit is contained in:
Stephan Dilly 2020-08-27 01:10:38 +02:00
parent 0e9fdfcaa2
commit 80da95b6f5
2 changed files with 29 additions and 17 deletions

View file

@ -37,21 +37,23 @@ pub struct CommitMessage {
impl CommitMessage { impl CommitMessage {
/// ///
pub fn from(s: &str) -> Self { pub fn from(s: &str) -> Self {
if let Some(idx) = s.find('\n') { let mut lines = s.lines();
let (first, rest) = s.split_at(idx); let subject = if let Some(subject) = lines.next() {
Self { subject.to_string()
subject: first.to_string(),
body: if rest.is_empty() {
None
} else {
Some(rest.to_string())
},
}
} else { } else {
Self { String::new()
subject: s.to_string(), };
body: None,
} let body: Vec<String> =
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)] #[cfg(test)]
mod tests { mod tests {
use super::get_commit_details; use super::{get_commit_details, CommitMessage};
use crate::error::Result; use crate::error::Result;
use crate::sync::{ use crate::sync::{
commit, stage_add_file, tests::repo_init_empty, commit, stage_add_file, tests::repo_init_empty,
@ -146,4 +148,14 @@ mod tests {
Ok(()) 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(())
}
} }

View file

@ -477,13 +477,13 @@ mod tests {
assert_eq!( assert_eq!(
get_wrapped_lines(&message_with_body, 7), get_wrapped_lines(&message_with_body, 7),
vec![ vec![
"Commit", "message", "", "First", "line", "Second", "Commit", "message", "First", "line", "Second",
"line" "line"
] ]
); );
assert_eq!( assert_eq!(
get_wrapped_lines(&message_with_body, 14), get_wrapped_lines(&message_with_body, 14),
vec!["Commit message", "", "First line", "Second line"] vec!["Commit message", "First line", "Second line"]
); );
} }
} }