mirror of
https://github.com/extrawurst/gitui.git
synced 2024-11-30 11:46:20 +03:00
cleanup commit message line encodings to fix rendering commit msg (closes #245)
This commit is contained in:
parent
0e9fdfcaa2
commit
80da95b6f5
@ -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(())
|
||||
}
|
||||
}
|
||||
|
@ -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"]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user