Create commit message correctly (#980)

* Create commit message correctly

Avoid trucating message string at invalid character boundary.

* refactor and add test
This commit is contained in:
Solomon
2025-10-09 10:58:46 +01:00
committed by GitHub
parent 37d273a851
commit f2cc538a8f

View File

@@ -1061,10 +1061,10 @@ impl LocalContainerService {
{
let content = entry.content.trim();
if !content.is_empty() {
// Truncate to reasonable size (4KB as Oracle suggested)
const MAX_SUMMARY_LENGTH: usize = 4096;
if content.len() > MAX_SUMMARY_LENGTH {
return Some(format!("{}...", &content[..MAX_SUMMARY_LENGTH]));
let truncated = truncate_to_char_boundary(content, MAX_SUMMARY_LENGTH);
return Some(format!("{truncated}..."));
}
return Some(content.to_string());
}
@@ -1247,3 +1247,39 @@ impl LocalContainerService {
Ok(())
}
}
fn truncate_to_char_boundary(content: &str, max_len: usize) -> &str {
if content.len() <= max_len {
return content;
}
let cutoff = content
.char_indices()
.map(|(idx, _)| idx)
.chain(std::iter::once(content.len()))
.take_while(|&idx| idx <= max_len)
.last()
.unwrap_or(0);
debug_assert!(content.is_char_boundary(cutoff));
&content[..cutoff]
}
#[cfg(test)]
mod tests {
#[test]
fn test_truncate_to_char_boundary() {
use super::truncate_to_char_boundary;
let input = "a".repeat(10);
assert_eq!(truncate_to_char_boundary(&input, 7), "a".repeat(7));
let input = "hello world";
assert_eq!(truncate_to_char_boundary(input, input.len()), input);
let input = "🔥🔥🔥"; // each fire emoji is 4 bytes
assert_eq!(truncate_to_char_boundary(input, 5), "🔥");
assert_eq!(truncate_to_char_boundary(input, 3), "");
}
}