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,22 +37,24 @@ 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);
let mut lines = s.lines();
let subject = if let Some(subject) = lines.next() {
subject.to_string()
} else {
String::new()
};
let body: Vec<String> =
lines.map(|line| line.to_string()).collect();
Self {
subject: first.to_string(),
body: if rest.is_empty() {
subject,
body: if body.is_empty() {
None
} else {
Some(rest.to_string())
Some(body.join("\n"))
},
}
} else {
Self {
subject: s.to_string(),
body: None,
}
}
}
///
@ -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(())
}
}

View File

@ -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"]
);
}
}