| @@ -1,31 +1,34 @@ |
| 1 | use gix::bstr::{BString, ByteSlice}; | 1 | use gix::bstr::{BString, ByteSlice}; |
| | 2 | use gix::objs::tree::EntryMode; |
| | 3 | use gix::prelude::ObjectIdExt; |
| 2 | | 4 | |
| 3 | pub fn tree( | 5 | pub fn tree( |
| 4 | repo: gix::Repository, | 6 | mut repo: gix::Repository, |
| 5 | out: &mut dyn std::io::Write, | 7 | out: &mut dyn std::io::Write, |
| 6 | old_treeish: BString, | 8 | old_treeish: BString, |
| 7 | new_treeish: BString, | 9 | new_treeish: BString, |
| 8 | ) -> anyhow::Result<()> { | 10 | ) -> anyhow::Result<()> { |
| | 11 | repo.object_cache_size_if_unset(repo.compute_object_cache_size_for_tree_diffs(&**repo.index_or_empty()?)); |
| | 12 | |
| 9 | let old_tree_id = repo.rev_parse_single(old_treeish.as_bstr())?; | 13 | 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())?; | 14 | let new_tree_id = repo.rev_parse_single(new_treeish.as_bstr())?; |
| 11 | | 15 | |
| 12 | let old_tree = old_tree_id.object()?.peel_to_kind(gix::object::Kind::Tree)?.into_tree(); | 16 | let old_tree = old_tree_id.object()?.peel_to_tree()?; |
| 13 | let new_tree = new_tree_id.object()?.peel_to_kind(gix::object::Kind::Tree)?.into_tree(); | 17 | let new_tree = new_tree_id.object()?.peel_to_tree()?; |
| 14 | | 18 | |
| 15 | let changes = repo.diff_tree_to_tree(&old_tree, &new_tree, None)?; | 19 | let changes = repo.diff_tree_to_tree(&old_tree, &new_tree, None)?; |
| 16 | | 20 | |
| 17 | writeln!( | 21 | writeln!( |
| 18 | out, | 22 | out, |
| 19 | "Diffing trees `{old_treeish}` ({old_tree_id}) -> `{new_treeish}` ({new_tree_id})" | 23 | "Diffing trees `{old_treeish}` ({old_tree_id}) -> `{new_treeish}` ({new_tree_id})\n" |
| 20 | )?; | 24 | )?; |
| 21 | writeln!(out)?; | 25 | write_changes(&repo, out, changes)?; |
| 22 | | | |
| 23 | write_changes(out, changes)?; | | |
| 24 | | 26 | |
| 25 | Ok(()) | 27 | Ok(()) |
| 26 | } | 28 | } |
| 27 | | 29 | |
| 28 | fn write_changes( | 30 | fn write_changes( |
| | 31 | repo: &gix::Repository, |
| 29 | mut out: impl std::io::Write, | 32 | mut out: impl std::io::Write, |
| 30 | changes: Vec<gix::diff::tree_with_rewrites::Change>, | 33 | changes: Vec<gix::diff::tree_with_rewrites::Change>, |
| 31 | ) -> Result<(), std::io::Error> { | 34 | ) -> Result<(), std::io::Error> { |
| @@ -37,8 +40,8 @@ fn write_changes( · 5 unmodified lines |
| 37 | entry_mode, | 40 | entry_mode, |
| 38 | .. | 41 | .. |
| 39 | } => { | 42 | } => { |
| 40 | writeln!(out, "A: {location}")?; | 43 | writeln!(out, "A: {}", typed_location(location, entry_mode))?; |
| 41 | writeln!(out, " {id}")?; | 44 | writeln!(out, " {}", id.attach(repo).shorten_or_id())?; |
| 42 | writeln!(out, " -> {:o}", entry_mode.0)?; | 45 | writeln!(out, " -> {:o}", entry_mode.0)?; |
| 43 | } | 46 | } |
| 44 | gix::diff::tree_with_rewrites::Change::Deletion { | 47 | gix::diff::tree_with_rewrites::Change::Deletion { |
| @@ -47,8 +50,8 @@ fn write_changes( · 2 unmodified lines |
| 47 | entry_mode, | 50 | entry_mode, |
| 48 | .. | 51 | .. |
| 49 | } => { | 52 | } => { |
| 50 | writeln!(out, "D: {location}")?; | 53 | writeln!(out, "D: {}", typed_location(location, entry_mode))?; |
| 51 | writeln!(out, " {id}")?; | 54 | writeln!(out, " {}", id.attach(repo).shorten_or_id())?; |
| 52 | writeln!(out, " {:o} ->", entry_mode.0)?; | 55 | writeln!(out, " {:o} ->", entry_mode.0)?; |
| 53 | } | 56 | } |
| 54 | gix::diff::tree_with_rewrites::Change::Modification { | 57 | gix::diff::tree_with_rewrites::Change::Modification { |
| @@ -58,9 +61,16 @@ fn write_changes( · 3 unmodified lines |
| 58 | previous_entry_mode, | 61 | previous_entry_mode, |
| 59 | entry_mode, | 62 | entry_mode, |
| 60 | } => { | 63 | } => { |
| 61 | writeln!(out, "M: {location}")?; | 64 | writeln!(out, "M: {}", typed_location(location, entry_mode))?; |
| 62 | writeln!(out, " {previous_id} -> {id}")?; | 65 | writeln!( |
| 63 | writeln!(out, " {:o} -> {:o}", previous_entry_mode.0, entry_mode.0)?; | 66 | out, |
| | 67 | " {previous_id} -> {id}", |
| | 68 | previous_id = previous_id.attach(repo).shorten_or_id(), |
| | 69 | id = id.attach(repo).shorten_or_id() |
| | 70 | )?; |
| | 71 | if previous_entry_mode != entry_mode { |
| | 72 | writeln!(out, " {:o} -> {:o}", previous_entry_mode.0, entry_mode.0)?; |
| | 73 | } |
| 64 | } | 74 | } |
| 65 | gix::diff::tree_with_rewrites::Change::Rewrite { | 75 | gix::diff::tree_with_rewrites::Change::Rewrite { |
| 66 | source_location, | 76 | source_location, |
| @@ -71,12 +81,31 @@ fn write_changes( · 4 unmodified lines |
| 71 | entry_mode, | 81 | entry_mode, |
| 72 | .. | 82 | .. |
| 73 | } => { | 83 | } => { |
| 74 | writeln!(out, "R: {source_location} -> {location}")?; | 84 | writeln!( |
| 75 | writeln!(out, " {source_id} -> {id}")?; | 85 | out, |
| 76 | writeln!(out, " {:o} -> {:o}", source_entry_mode.0, entry_mode.0)?; | 86 | "R: {source} -> {dest}", |
| | 87 | source = typed_location(source_location, source_entry_mode), |
| | 88 | dest = typed_location(location, entry_mode) |
| | 89 | )?; |
| | 90 | writeln!( |
| | 91 | out, |
| | 92 | " {source_id} -> {id}", |
| | 93 | source_id = source_id.attach(repo).shorten_or_id(), |
| | 94 | id = id.attach(repo).shorten_or_id() |
| | 95 | )?; |
| | 96 | if source_entry_mode != entry_mode { |
| | 97 | writeln!(out, " {:o} -> {:o}", source_entry_mode.0, entry_mode.0)?; |
| | 98 | } |
| 77 | } | 99 | } |
| 78 | }; | 100 | }; |
| 79 | } | 101 | } |
| 80 | | 102 | |
| 81 | Ok(()) | 103 | Ok(()) |
| 82 | } | 104 | } |
| | 105 | |
| | 106 | fn typed_location(mut location: BString, mode: EntryMode) -> BString { |
| | 107 | if mode.is_tree() { |
| | 108 | location.push(b'/'); |
| | 109 | } |
| | 110 | location |
| | 111 | } |