Skip to content

Commit 850cd7a

Browse files
authored
git: add diff length limit and truncation (#54)
* Add MAX_DIFF_LENGTH constant to limit git diff output * Truncate diff and add truncation notice when exceeding limit * Prevent potential issues with overly large diff outputs Signed-off-by: Qiming Chu <cchuqiming@gmail.com>
1 parent 69d7f4e commit 850cd7a

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/git.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use anyhow::{Context, Result};
22
use std::process::Command;
33

4+
const MAX_DIFF_LENGTH: usize = 31000;
5+
46
/// Get diff of staged changes
57
pub fn get_staged_diff() -> Result<String> {
68
let output = Command::new("git")
@@ -13,8 +15,13 @@ pub fn get_staged_diff() -> Result<String> {
1315
anyhow::bail!("Failed to get staged diff: {}", error_message);
1416
}
1517

16-
let diff = String::from_utf8(output.stdout).context("Parse git diff output failed")?;
18+
let mut diff = String::from_utf8(output.stdout).context("Parse git diff output failed")?;
1719

20+
if diff.chars().count() > MAX_DIFF_LENGTH {
21+
diff = diff.chars().take(MAX_DIFF_LENGTH).collect::<String>()
22+
+ "\n\n[...]\n\n"
23+
+ "\n[... diff truncated due to length limit ...]\n";
24+
}
1825
Ok(diff)
1926
}
2027

0 commit comments

Comments
 (0)