Skip to content

Commit 80da95b

Browse files
author
Stephan Dilly
committed
cleanup commit message line encodings to fix rendering commit msg (closes #245)
1 parent 0e9fdfc commit 80da95b

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

asyncgit/src/sync/commit_details.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,23 @@ pub struct CommitMessage {
3737
impl CommitMessage {
3838
///
3939
pub fn from(s: &str) -> Self {
40-
if let Some(idx) = s.find('\n') {
41-
let (first, rest) = s.split_at(idx);
42-
Self {
43-
subject: first.to_string(),
44-
body: if rest.is_empty() {
45-
None
46-
} else {
47-
Some(rest.to_string())
48-
},
49-
}
40+
let mut lines = s.lines();
41+
let subject = if let Some(subject) = lines.next() {
42+
subject.to_string()
5043
} else {
51-
Self {
52-
subject: s.to_string(),
53-
body: None,
54-
}
44+
String::new()
45+
};
46+
47+
let body: Vec<String> =
48+
lines.map(|line| line.to_string()).collect();
49+
50+
Self {
51+
subject,
52+
body: if body.is_empty() {
53+
None
54+
} else {
55+
Some(body.join("\n"))
56+
},
5557
}
5658
}
5759

@@ -112,7 +114,7 @@ pub fn get_commit_details(
112114
#[cfg(test)]
113115
mod tests {
114116

115-
use super::get_commit_details;
117+
use super::{get_commit_details, CommitMessage};
116118
use crate::error::Result;
117119
use crate::sync::{
118120
commit, stage_add_file, tests::repo_init_empty,
@@ -146,4 +148,14 @@ mod tests {
146148

147149
Ok(())
148150
}
151+
152+
#[test]
153+
fn test_msg_linefeeds() -> Result<()> {
154+
let msg = CommitMessage::from("foo\nbar\r\ntest");
155+
156+
assert_eq!(msg.subject, String::from("foo"),);
157+
assert_eq!(msg.body, Some(String::from("bar\ntest")),);
158+
159+
Ok(())
160+
}
149161
}

src/components/commit_details/details.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,13 @@ mod tests {
477477
assert_eq!(
478478
get_wrapped_lines(&message_with_body, 7),
479479
vec![
480-
"Commit", "message", "", "First", "line", "Second",
480+
"Commit", "message", "First", "line", "Second",
481481
"line"
482482
]
483483
);
484484
assert_eq!(
485485
get_wrapped_lines(&message_with_body, 14),
486-
vec!["Commit message", "", "First line", "Second line"]
486+
vec!["Commit message", "First line", "Second line"]
487487
);
488488
}
489489
}

0 commit comments

Comments
 (0)