diff --git a/crates/code2prompt-core/src/git.rs b/crates/code2prompt-core/src/git.rs index 2f6971b..e8d8004 100644 --- a/crates/code2prompt-core/src/git.rs +++ b/crates/code2prompt-core/src/git.rs @@ -11,6 +11,23 @@ use log::info; use std::collections::HashSet; use std::path::{Path, PathBuf}; +/// Helper: write a diff line with proper `+/-`/` ` prefix based on `origin()`. +/// +/// `git2`'s `DiffFormat::Patch` splits the prefix from content: +/// - `origin()` returns `'+'` / `'-'` / `' '` for added/removed/context lines +/// - `content()` returns ONLY the raw line text WITHOUT the prefix character +/// - File headers (origin `'F'`) and hunk headers (origin `'H'`) already contain full text in `content()` +fn write_diff_line(buf: &mut Vec, line: git2::DiffLine) { + match line.origin() { + '+' => buf.push(b'+'), + '-' => buf.push(b'-'), + ' ' => buf.push(b' '), + 'F' | 'H' | 'B' | '\n' => {} // file/hunk/binary/separator + _ => {} + } + buf.extend_from_slice(line.content()); +} + /// Extract git diff for staged changes in repository. /// /// Generates diff between HEAD and index to show staged changes, with additional @@ -50,7 +67,7 @@ pub fn get_git_diff(repo_path: &Path) -> Result { let mut staged_diff_text = Vec::new(); staged_diff .print(git2::DiffFormat::Patch, |_delta, _hunk, line| { - staged_diff_text.extend_from_slice(line.content()); + write_diff_line(&mut staged_diff_text, line); true }) .context("Failed to print staged diff")?; @@ -70,7 +87,7 @@ pub fn get_git_diff(repo_path: &Path) -> Result { let mut unstaged_diff_text = Vec::new(); unstaged_diff .print(git2::DiffFormat::Patch, |_delta, _hunk, line| { - unstaged_diff_text.extend_from_slice(line.content()); + write_diff_line(&mut unstaged_diff_text, line); true }) .context("Failed to print unstaged diff")?; @@ -127,7 +144,7 @@ pub fn get_git_diff_between_branches( let mut diff_text = Vec::new(); diff.print(git2::DiffFormat::Patch, |_delta, _hunk, line| { - diff_text.extend_from_slice(line.content()); + write_diff_line(&mut diff_text, line); true }) .context("Failed to print diff")?;