Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions crates/code2prompt-core/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>, 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
Expand Down Expand Up @@ -50,7 +67,7 @@ pub fn get_git_diff(repo_path: &Path) -> Result<String> {
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")?;
Expand All @@ -70,7 +87,7 @@ pub fn get_git_diff(repo_path: &Path) -> Result<String> {
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")?;
Expand Down Expand Up @@ -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")?;
Expand Down