From 1028a516521a23308ddd59d8d6b71445b4f4c6f1 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Wed, 24 Jun 2020 16:08:46 +0200 Subject: [PATCH] fix invalid utf8 in commit msg also for commit details (see #150) --- asyncgit/src/sync/commit_details.rs | 47 +++++++++++++++++++++++++++-- asyncgit/src/sync/commits_info.rs | 19 ++++++++---- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/asyncgit/src/sync/commit_details.rs b/asyncgit/src/sync/commit_details.rs index fd806988..9591200a 100644 --- a/asyncgit/src/sync/commit_details.rs +++ b/asyncgit/src/sync/commit_details.rs @@ -1,4 +1,4 @@ -use super::{utils::repo, CommitId}; +use super::{commits_info::get_message, utils::repo, CommitId}; use crate::error::Result; use git2::Signature; use scopetime::scope_time; @@ -95,14 +95,55 @@ pub fn get_commit_details( Some(committer) }; - let message = commit.message().map(|m| CommitMessage::from(m)); + let msg = + CommitMessage::from(get_message(&commit, None).as_str()); let details = CommitDetails { author, committer, - message, + message: Some(msg), hash: id.to_string(), }; Ok(details) } + +#[cfg(test)] +mod tests { + + use super::get_commit_details; + use crate::error::Result; + use crate::sync::{ + commit, stage_add_file, tests::repo_init_empty, CommitId, + }; + use std::{fs::File, io::Write, path::Path}; + + #[test] + fn test_msg_invalid_utf8() -> Result<()> { + let file_path = Path::new("foo"); + let (_td, repo) = repo_init_empty().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); + + File::create(&root.join(file_path))?.write_all(b"a")?; + stage_add_file(repo_path, file_path).unwrap(); + + let msg = invalidstring::invalid_utf8("test msg"); + let id = commit(repo_path, msg.as_str()).unwrap(); + + let res = + get_commit_details(repo_path, CommitId::new(id)).unwrap(); + + dbg!(&res.message.as_ref().unwrap().subject); + assert_eq!( + res.message + .as_ref() + .unwrap() + .subject + .starts_with("test msg"), + true + ); + + Ok(()) + } +} diff --git a/asyncgit/src/sync/commits_info.rs b/asyncgit/src/sync/commits_info.rs index 39256352..268fb0e7 100644 --- a/asyncgit/src/sync/commits_info.rs +++ b/asyncgit/src/sync/commits_info.rs @@ -63,7 +63,7 @@ pub fn get_commits_info( let res = commits .map(|c: Commit| { - let message = get_message(&c, message_length_limit); + let message = get_message(&c, Some(message_length_limit)); let author = if let Some(name) = c.author().name() { String::from(name) } else { @@ -81,11 +81,18 @@ pub fn get_commits_info( Ok(res) } -fn get_message(c: &Commit, message_length_limit: usize) -> String { - limit_str( - String::from_utf8_lossy(c.message_bytes()), - message_length_limit, - ) +/// +pub fn get_message( + c: &Commit, + message_length_limit: Option, +) -> String { + let msg = String::from_utf8_lossy(c.message_bytes()); + + if let Some(limit) = message_length_limit { + limit_str(msg, limit) + } else { + msg.to_string() + } } fn limit_str(s: Cow<'_, str>, limit: usize) -> String {