Render the wire's DiffChunks as the local `diff --json` document — the same versioned shape (`schema_version` 1, `view`, null `change`, per-file hunks/lines with true numbers, aggregate stats), built from the chunk fields. Per-file status derives from the hunks' old/new counts (a pure insertion reads `added`, a pure deletion `deleted`, else `modified`), the same classification the local FileDiff c
(chunks: &[pb::DiffChunk], view: Option<&str>)
| 2845 | /// pure insertion reads `added`, a pure deletion `deleted`, else |
| 2846 | /// `modified`), the same classification the local FileDiff carries. |
| 2847 | fn print_diff_json(chunks: &[pb::DiffChunk], view: Option<&str>) -> CliResult<()> { |
| 2848 | let mut files = Vec::new(); |
| 2849 | let mut insertions = 0usize; |
| 2850 | let mut deletions = 0usize; |
| 2851 | for chunk in chunks { |
| 2852 | insertions += chunk.additions as usize; |
| 2853 | deletions += chunk.deletions as usize; |
| 2854 | let hunks = parse_patch_hunks(&String::from_utf8_lossy( |
| 2855 | chunk.patch.as_deref().unwrap_or(&[]), |
| 2856 | )); |
| 2857 | let old_side_empty = hunks.iter().all(|hunk| hunk.old_count == 0); |
| 2858 | let new_side_empty = hunks.iter().all(|hunk| hunk.new_count == 0); |
| 2859 | let (status, code, old_path, new_path) = if chunk.binary { |
| 2860 | ("binary", 'B', chunk.path.clone(), chunk.path.clone()) |
| 2861 | } else if old_side_empty && !new_side_empty { |
| 2862 | ("added", 'A', "/dev/null".to_string(), chunk.path.clone()) |
| 2863 | } else if new_side_empty && !old_side_empty { |
| 2864 | ("deleted", 'D', chunk.path.clone(), "/dev/null".to_string()) |
| 2865 | } else { |
| 2866 | ("modified", 'M', chunk.path.clone(), chunk.path.clone()) |
| 2867 | }; |
| 2868 | let json_hunks: Vec<serde_json::Value> = hunks |
| 2869 | .iter() |
| 2870 | .map(|hunk| { |
| 2871 | serde_json::json!({ |
| 2872 | "old_start": hunk.old_start, |
| 2873 | "old_count": hunk.old_count, |
| 2874 | "new_start": hunk.new_start, |
| 2875 | "new_count": hunk.new_count, |
| 2876 | "lines": hunk.lines.iter().map(|(marker, content, old_line, new_line)| { |
| 2877 | serde_json::json!({ |
| 2878 | "status": match marker { |
| 2879 | '-' => "removed", |
| 2880 | '+' => "added", |
| 2881 | _ => "context", |
| 2882 | }, |
| 2883 | "content": content, |
| 2884 | "old_line": old_line, |
| 2885 | "new_line": new_line, |
| 2886 | }) |
| 2887 | }).collect::<Vec<_>>(), |
| 2888 | }) |
| 2889 | }) |
| 2890 | .collect(); |
| 2891 | files.push(serde_json::json!({ |
| 2892 | "path": chunk.path, |
| 2893 | "old_path": old_path, |
| 2894 | "new_path": new_path, |
| 2895 | "status": status, |
| 2896 | "code": code, |
| 2897 | "binary": chunk.binary, |
| 2898 | "insertions": chunk.additions, |
| 2899 | "deletions": chunk.deletions, |
| 2900 | "hunks": json_hunks, |
| 2901 | })); |
| 2902 | } |
| 2903 | let document = serde_json::json!({ |
| 2904 | "schema_version": 1, |
no test coverage detected