1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
fn process_git_diff_output(diff: &str) -> String {
// Early return if diff is small enough
if diff.len() <= MAX_DIFF_CHARS {
return diff.to_string();
}
// Split the diff into file sections
let file_pattern = r"(?m)^diff --git ";
let sections: Vec<&str> = diff.split(file_pattern).collect();
// First segment is empty or small header - keep it as is
let mut processed = if !sections.is_empty() && !sections[0].trim().is_empty() {
sections[0].to_string()
} else {
String::new()
};
// Process each file section
for (i, section) in sections.iter().enumerate().skip(1) {
// Skip empty sections
if section.trim().is_empty() {
continue;
}
// Add the "diff --git " prefix back (we split on it, so it's missing)
processed.push_str("diff --git ");
// Check if this section is too large
if section.len() > MAX_FILE_DIFF_CHARS {
// Find the file name from the section
let _file_name = if let Some(end) = section.find('\n') {
section[..end].trim()
} else {
section.trim()
};
// Take the header part (usually 4-5 lines) - this includes the file name, index, and --- +++ lines
let _header_end = section.lines().take(5).collect::<Vec<&str>>().join("\n").len();
// Take the beginning of the diff content - safely truncate at char boundary
let safe_len = get_safe_slice_length(section, MAX_FILE_DIFF_CHARS.min(section.len()));
let content = §ion[..safe_len];
// Add truncation notice for this specific file
processed.push_str(&format!("{}\n\n[... diff for this file truncated due to length ...]", content));
} else {
// Section is small enough, add it as is
processed.push_str(section);
}
// Add separating newline if needed
if i < sections.len() - 1 && !processed.ends_with('\n') {
processed.push('\n');
}
}
// Final overall truncation check (as a safety measure)
if processed.len() > MAX_DIFF_CHARS {
let safe_len = get_safe_slice_length(&processed, MAX_DIFF_CHARS - 100);
processed = format!("{}...\n\n[... total diff truncated due to length (first {} chars shown) ...]",
&processed[..safe_len],
safe_len);
}
processed
}