|
1 | | -use gix::bstr::BString; |
| 1 | +use gix::bstr::{BString, ByteSlice}; |
2 | 2 |
|
3 | 3 | pub fn tree( |
4 | | - _repo: gix::Repository, |
5 | | - _out: &mut dyn std::io::Write, |
6 | | - _treeish1: BString, |
7 | | - _treeish2: BString, |
| 4 | + repo: gix::Repository, |
| 5 | + out: &mut dyn std::io::Write, |
| 6 | + old_treeish: BString, |
| 7 | + new_treeish: BString, |
8 | 8 | ) -> anyhow::Result<()> { |
| 9 | + let old_tree_id = repo.rev_parse_single(old_treeish.as_bstr())?; |
| 10 | + let new_tree_id = repo.rev_parse_single(new_treeish.as_bstr())?; |
| 11 | + |
| 12 | + let old_tree = old_tree_id |
| 13 | + .object() |
| 14 | + .unwrap() |
| 15 | + .peel_to_kind(gix::object::Kind::Tree) |
| 16 | + .unwrap() |
| 17 | + .into_tree(); |
| 18 | + let new_tree = new_tree_id |
| 19 | + .object() |
| 20 | + .unwrap() |
| 21 | + .peel_to_kind(gix::object::Kind::Tree) |
| 22 | + .unwrap() |
| 23 | + .into_tree(); |
| 24 | + |
| 25 | + let changes = repo.diff_tree_to_tree(&old_tree, &new_tree, None)?; |
| 26 | + |
| 27 | + writeln!( |
| 28 | + out, |
| 29 | + "Diffing tree `{old_treeish}` ({old_tree_id}) -> `{new_treeish}` ({new_tree_id})" |
| 30 | + )?; |
| 31 | + writeln!(out)?; |
| 32 | + |
| 33 | + write_changes(out, changes)?; |
| 34 | + |
| 35 | + Ok(()) |
| 36 | +} |
| 37 | + |
| 38 | +fn write_changes( |
| 39 | + mut out: impl std::io::Write, |
| 40 | + changes: Vec<gix::diff::tree_with_rewrites::Change>, |
| 41 | +) -> Result<(), std::io::Error> { |
| 42 | + for change in changes { |
| 43 | + match change { |
| 44 | + gix::diff::tree_with_rewrites::Change::Addition { |
| 45 | + location, |
| 46 | + id, |
| 47 | + entry_mode, |
| 48 | + .. |
| 49 | + } => { |
| 50 | + writeln!(out, "A: {location}")?; |
| 51 | + writeln!(out, " {id}")?; |
| 52 | + writeln!(out, " -> {:o}", entry_mode.0)?; |
| 53 | + } |
| 54 | + gix::diff::tree_with_rewrites::Change::Deletion { |
| 55 | + location, |
| 56 | + id, |
| 57 | + entry_mode, |
| 58 | + .. |
| 59 | + } => { |
| 60 | + writeln!(out, "D: {location}")?; |
| 61 | + writeln!(out, " {id}")?; |
| 62 | + writeln!(out, " {:o} ->", entry_mode.0)?; |
| 63 | + } |
| 64 | + gix::diff::tree_with_rewrites::Change::Modification { |
| 65 | + location, |
| 66 | + previous_id, |
| 67 | + id, |
| 68 | + previous_entry_mode, |
| 69 | + entry_mode, |
| 70 | + } => { |
| 71 | + writeln!(out, "M: {location}")?; |
| 72 | + writeln!(out, " {previous_id} -> {id}")?; |
| 73 | + writeln!(out, " {:o} -> {:o}", previous_entry_mode.0, entry_mode.0)?; |
| 74 | + } |
| 75 | + gix::diff::tree_with_rewrites::Change::Rewrite { |
| 76 | + source_location, |
| 77 | + source_id, |
| 78 | + id, |
| 79 | + location, |
| 80 | + source_entry_mode, |
| 81 | + entry_mode, |
| 82 | + .. |
| 83 | + } => { |
| 84 | + writeln!(out, "R: {source_location} -> {location}")?; |
| 85 | + writeln!(out, " {source_id} -> {id}")?; |
| 86 | + writeln!(out, " {:o} -> {:o}", source_entry_mode.0, entry_mode.0)?; |
| 87 | + } |
| 88 | + }; |
| 89 | + } |
| 90 | + |
9 | 91 | Ok(()) |
10 | 92 | } |
0 commit comments