MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / parse_diff_files

Function parse_diff_files

atomic-cli/src/commands/git/parallel.rs:3915–4034  ·  view source on GitHub ↗

Parse files from a git diff. For each changed file we capture: - The operation type (Added / Modified / Deleted / Renamed / Copied) - The new file content (for adds/modifies) - The old file content (for modifies/deletes) - The exact diff lines that git computed, so Phase 2 can build BranchOps directly from git's diff rather than re-diffing.

(
    git_repo: &GitRepository,
    diff: &Diff,
    tree: &Tree,
    parent_tree: Option<&Tree>,
    capture_diff_lines: bool,
)

Source from the content-addressed store, hash-verified

3913/// - The exact diff lines that git computed, so Phase 2 can build
3914/// BranchOps directly from git's diff rather than re-diffing.
3915fn parse_diff_files(
3916 git_repo: &GitRepository,
3917 diff: &Diff,
3918 tree: &Tree,
3919 parent_tree: Option<&Tree>,
3920 capture_diff_lines: bool,
3921) -> CliResult<Vec<ParsedFile>> {
3922 use std::collections::HashMap;
3923
3924 // ── Step 1: collect per-file diff lines via diff.foreach ────────────
3925 //
3926 // git2::Diff::foreach gives us each DiffLine with its origin (`+`/`-`/` `),
3927 // raw bytes, and old/new line numbers — exactly what `git diff` outputs.
3928 // We key by file path so we can attach them to the ParsedFile below.
3929
3930 // Map from file path → accumulated diff lines for that file.
3931 let mut lines_by_path: HashMap<String, Vec<GitDiffLine>> = HashMap::new();
3932
3933 if capture_diff_lines {
3934 let _ = diff.foreach(
3935 &mut |_delta, _progress| true, // file_cb (no-op)
3936 None, // binary_cb
3937 None, // hunk_cb
3938 Some(&mut |delta, _hunk, line| {
3939 let origin = line.origin();
3940 // We only keep `+`, `-`, and context (` `) lines.
3941 if origin != '+' && origin != '-' && origin != ' ' {
3942 return true;
3943 }
3944 let path = delta
3945 .new_file()
3946 .path()
3947 .or_else(|| delta.old_file().path())
3948 .map(|p| p.to_string_lossy().to_string())
3949 .unwrap_or_default();
3950
3951 lines_by_path.entry(path).or_default().push(GitDiffLine {
3952 origin,
3953 content: line.content().to_vec(),
3954 old_lineno: line.old_lineno(),
3955 new_lineno: line.new_lineno(),
3956 });
3957 true
3958 }),
3959 );
3960 }
3961
3962 // ── Step 2: build ParsedFile entries from the delta list ─────────────
3963
3964 let mut files = Vec::new();
3965
3966 for delta in diff.deltas() {
3967 let new_file = delta.new_file();
3968 let old_file = delta.old_file();
3969
3970 // Skip submodules silently (warnings printed during Phase 2)
3971 if new_file.mode() == git2::FileMode::Commit || old_file.mode() == git2::FileMode::Commit {
3972 continue;

Callers 1

parse_commitFunction · 0.85

Calls 7

get_file_contentFunction · 0.85
new_fileMethod · 0.80
removeMethod · 0.65
pathMethod · 0.45
pushMethod · 0.45
contentMethod · 0.45
statusMethod · 0.45

Tested by

no test coverage detected