-
Notifications
You must be signed in to change notification settings - Fork 3
Fix agentdiff sync and PR attribution reporting #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,7 +13,8 @@ pub fn run(store: &Store, args: &DiffArgs) -> Result<()> { | |||||||||||||||||||||||||||||||||||||||||||
| let commit = args.commit.as_deref().unwrap_or("HEAD"); | ||||||||||||||||||||||||||||||||||||||||||||
| let entries = store.load_entries()?; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // Run git diff to get changed lines | ||||||||||||||||||||||||||||||||||||||||||||
| // Run a zero-context diff so changed-line parsing is not polluted by | ||||||||||||||||||||||||||||||||||||||||||||
| // surrounding context lines. | ||||||||||||||||||||||||||||||||||||||||||||
| let diff_output = run_git_diff(&store.repo_root, commit)?; | ||||||||||||||||||||||||||||||||||||||||||||
| let changed = parse_diff_hunks(&diff_output); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -69,7 +70,7 @@ pub fn run(store: &Store, args: &DiffArgs) -> Result<()> { | |||||||||||||||||||||||||||||||||||||||||||
| total_changed, | ||||||||||||||||||||||||||||||||||||||||||||
| total_ai, | ||||||||||||||||||||||||||||||||||||||||||||
| if total_changed > 0 { | ||||||||||||||||||||||||||||||||||||||||||||
| (total_ai as f64 / total_changed as f64 * 100.0) | ||||||||||||||||||||||||||||||||||||||||||||
| total_ai as f64 / total_changed as f64 * 100.0 | ||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||
| 0.0 | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -79,52 +80,116 @@ pub fn run(store: &Store, args: &DiffArgs) -> Result<()> { | |||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| fn run_git_diff(repo_root: &std::path::Path, commit: &str) -> Result<String> { | ||||||||||||||||||||||||||||||||||||||||||||
| let spec = diff_spec(commit); | ||||||||||||||||||||||||||||||||||||||||||||
| let output = std::process::Command::new("git") | ||||||||||||||||||||||||||||||||||||||||||||
| .args(["diff", commit]) | ||||||||||||||||||||||||||||||||||||||||||||
| .args(["diff", "--unified=0", "--no-color", "--no-ext-diff", &spec]) | ||||||||||||||||||||||||||||||||||||||||||||
| .current_dir(repo_root) | ||||||||||||||||||||||||||||||||||||||||||||
| .output()?; | ||||||||||||||||||||||||||||||||||||||||||||
| if !output.status.success() { | ||||||||||||||||||||||||||||||||||||||||||||
| let stderr = String::from_utf8_lossy(&output.stderr); | ||||||||||||||||||||||||||||||||||||||||||||
| anyhow::bail!("git diff failed: {}", stderr.trim()); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| Ok(String::from_utf8_lossy(&output.stdout).to_string()) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
82
to
93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| fn diff_spec(commit: &str) -> String { | ||||||||||||||||||||||||||||||||||||||||||||
| if commit.contains("..") { | ||||||||||||||||||||||||||||||||||||||||||||
| commit.to_string() | ||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||
| format!("{commit}^!") | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| fn parse_diff_hunks(diff: &str) -> std::collections::HashMap<String, Vec<u32>> { | ||||||||||||||||||||||||||||||||||||||||||||
| let mut result: std::collections::HashMap<String, Vec<u32>> = std::collections::HashMap::new(); | ||||||||||||||||||||||||||||||||||||||||||||
| let mut current_file = String::new(); | ||||||||||||||||||||||||||||||||||||||||||||
| let mut base_line: u32 = 0; | ||||||||||||||||||||||||||||||||||||||||||||
| let mut new_line: u32 = 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| for line in diff.lines() { | ||||||||||||||||||||||||||||||||||||||||||||
| if line.starts_with("diff --git") { | ||||||||||||||||||||||||||||||||||||||||||||
| // Extract file from "diff --git a/path b/path" | ||||||||||||||||||||||||||||||||||||||||||||
| if let Some(path) = line.split_whitespace().last() { | ||||||||||||||||||||||||||||||||||||||||||||
| // Remove "b/" prefix | ||||||||||||||||||||||||||||||||||||||||||||
| current_file.clear(); | ||||||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if let Some(path) = line.strip_prefix("+++ ") { | ||||||||||||||||||||||||||||||||||||||||||||
| if path == "/dev/null" { | ||||||||||||||||||||||||||||||||||||||||||||
| current_file.clear(); | ||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||
| current_file = path.trim_start_matches("b/").to_string(); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } else if line.starts_with("@@") { | ||||||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if line.starts_with("@@") { | ||||||||||||||||||||||||||||||||||||||||||||
| // Parse hunk header: @@ -start,count +start,count @@ | ||||||||||||||||||||||||||||||||||||||||||||
| if let Some(end) = line.find(" @@") { | ||||||||||||||||||||||||||||||||||||||||||||
| let header = &line[4..end]; | ||||||||||||||||||||||||||||||||||||||||||||
| if let Some(pos) = header.find("+") { | ||||||||||||||||||||||||||||||||||||||||||||
| let after_plus = &header[pos + 1..]; | ||||||||||||||||||||||||||||||||||||||||||||
| if let Some(comma) = after_plus.find(',') { | ||||||||||||||||||||||||||||||||||||||||||||
| base_line = after_plus[..comma].parse().unwrap_or(1); | ||||||||||||||||||||||||||||||||||||||||||||
| new_line = after_plus[..comma].parse().unwrap_or(1); | ||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||
| base_line = after_plus.parse().unwrap_or(1); | ||||||||||||||||||||||||||||||||||||||||||||
| new_line = after_plus.parse().unwrap_or(1); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } else if (line.starts_with('+') || line.starts_with(' ')) | ||||||||||||||||||||||||||||||||||||||||||||
| && !line.starts_with("+++") | ||||||||||||||||||||||||||||||||||||||||||||
| && !line.starts_with("index") | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| // Added or context line | ||||||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if line.starts_with('+') && !line.starts_with("+++") { | ||||||||||||||||||||||||||||||||||||||||||||
| if !current_file.is_empty() { | ||||||||||||||||||||||||||||||||||||||||||||
| result | ||||||||||||||||||||||||||||||||||||||||||||
| .entry(current_file.clone()) | ||||||||||||||||||||||||||||||||||||||||||||
| .or_default() | ||||||||||||||||||||||||||||||||||||||||||||
| .push(base_line); | ||||||||||||||||||||||||||||||||||||||||||||
| .push(new_line); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| base_line += 1; | ||||||||||||||||||||||||||||||||||||||||||||
| new_line += 1; | ||||||||||||||||||||||||||||||||||||||||||||
| } else if line.starts_with(' ') { | ||||||||||||||||||||||||||||||||||||||||||||
| new_line += 1; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| for lines in result.values_mut() { | ||||||||||||||||||||||||||||||||||||||||||||
| lines.sort_unstable(); | ||||||||||||||||||||||||||||||||||||||||||||
| lines.dedup(); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| result | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[cfg(test)] | ||||||||||||||||||||||||||||||||||||||||||||
| mod tests { | ||||||||||||||||||||||||||||||||||||||||||||
| use super::*; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||
| fn default_commit_diff_uses_commit_changes_not_worktree_delta() { | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(diff_spec("HEAD"), "HEAD^!"); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(diff_spec("abc123"), "abc123^!"); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||
| fn explicit_range_is_preserved() { | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(diff_spec("main..HEAD"), "main..HEAD"); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(diff_spec("main...HEAD"), "main...HEAD"); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||
| fn parses_only_new_side_changed_lines() { | ||||||||||||||||||||||||||||||||||||||||||||
| let diff = r#"diff --git a/src/lib.rs b/src/lib.rs | ||||||||||||||||||||||||||||||||||||||||||||
| index 1111111..2222222 100644 | ||||||||||||||||||||||||||||||||||||||||||||
| --- a/src/lib.rs | ||||||||||||||||||||||||||||||||||||||||||||
| +++ b/src/lib.rs | ||||||||||||||||||||||||||||||||||||||||||||
| @@ -10,3 +10,3 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| context | ||||||||||||||||||||||||||||||||||||||||||||
| -old | ||||||||||||||||||||||||||||||||||||||||||||
| +new | ||||||||||||||||||||||||||||||||||||||||||||
| context | ||||||||||||||||||||||||||||||||||||||||||||
| @@ -20,0 +21,2 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| +added one | ||||||||||||||||||||||||||||||||||||||||||||
| +added two | ||||||||||||||||||||||||||||||||||||||||||||
| "#; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| let changed = parse_diff_hunks(diff); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(changed.get("src/lib.rs"), Some(&vec![11, 21, 22])); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
range/selectionWhen a change dict carries both a top-level
startLine/endLineand a nestedrangeorselectionkey (a plausible Cursor payload, e.g. edit at line 10 inside a selected block spanning lines 5–20),_add_lines_from_range_object(out, change)adds only lines 10–10, but the subsequentfor keyloop then also calls_add_lines_from_range_object(out, change.get("range")), adding lines 5–20. The union over-attributes the surrounding selection as AI-authored. If a specific top-level range is already resolved, there is no reason to also expand from the nested range keys.