Showing the changes to git-diff (2 of 3 files). Show all
Unified · Split
| @@ -1 +1,14 @@ | |||
| 1 | // TODO: add deny(missing_docs) | ||
| 1 | #![forbid(unsafe_code, rust_2018_idioms)] | 2 | #![forbid(unsafe_code, rust_2018_idioms)] |
| 3 | |||
| 4 | pub struct Tree<'a>(pub &'a git_object::immutable::Tree<'a>); | ||
| 5 | |||
| 6 | const EMPTY_TREE: git_object::immutable::Tree<'static> = git_object::immutable::Tree::empty(); | ||
| 7 | |||
| 8 | impl<'a> Tree<'a> { | ||
| 9 | /// Returns the changes that need to be applied to `other` to get `self`. | ||
| 10 | pub fn changes_from(&self, other: Option<&git_object::immutable::Tree<'_>>) { | ||
| 11 | let other = other.unwrap_or(&EMPTY_TREE); | ||
| 12 | todo!("changes tree to tree") | ||
| 13 | } | ||
| 14 | } | ||
| @@ -1,11 +1,16 @@ | |||
| 1 | mod with_tree { | 1 | mod with_tree { |
| 2 | use git_odb::{pack, Locate}; | 2 | use git_odb::{pack, Locate}; |
| 3 | const FIRST_COMMIT: &str = "055df97e18cd537da3cb16bcbdf1733fdcdfb430"; | ||
| 3 | 4 | ||
| 4 | fn diff_at(commit_id: &str) -> crate::Result { | 5 | fn diff_at(commit_id: &str) -> crate::Result { |
| 5 | let db = git_odb::linked::Db::at(test_tools::scripted_fixture_repo_read_only("make_diff_repo.sh")?)?; | 6 | let db = git_odb::linked::Db::at( |
| 7 | test_tools::scripted_fixture_repo_read_only("make_diff_repo.sh")? | ||
| 8 | .join(".git") | ||
| 9 | .join("objects"), | ||
| 10 | )?; | ||
| 6 | let commit_id = git_hash::ObjectId::from_hex(commit_id.as_bytes())?; | 11 | let commit_id = git_hash::ObjectId::from_hex(commit_id.as_bytes())?; |
| 7 | let mut buf = Vec::new(); | 12 | let mut buf = Vec::new(); |
| 8 | let (_main_tree_id, _parent_commit_id) = { | 13 | let (main_tree_id, parent_commit_id) = { |
| 9 | let commit = db | 14 | let commit = db |
| 10 | .locate(commit_id, &mut buf, &mut pack::cache::Never)? | 15 | .locate(commit_id, &mut buf, &mut pack::cache::Never)? |
| 11 | .expect("start commit to be present") | 16 | .expect("start commit to be present") |
| @@ -18,12 +23,31 @@ mod with_tree { · 6 unmodified lines | |||
| 18 | p | 23 | p |
| 19 | }) | 24 | }) |
| 20 | }; | 25 | }; |
| 26 | let main_tree = db | ||
| 27 | .locate(main_tree_id, &mut buf, &mut pack::cache::Never)? | ||
| 28 | .expect("main tree present") | ||
| 29 | .decode()? | ||
| 30 | .into_tree() | ||
| 31 | .expect("id to be a tree"); | ||
| 32 | let mut buf2 = Vec::new(); | ||
| 33 | let previous_tree = { | ||
| 34 | parent_commit_id | ||
| 35 | .and_then(|id| db.locate(id, &mut buf2, &mut pack::cache::Never).ok().flatten()) | ||
| 36 | .and_then(|c| c.decode().ok()) | ||
| 37 | .and_then(|c| c.into_commit()) | ||
| 38 | .map(|c| c.tree()) | ||
| 39 | .and_then(|tree| db.locate(tree, &mut buf2, &mut pack::cache::Never).ok().flatten()) | ||
| 40 | .and_then(|tree| tree.decode().ok()) | ||
| 41 | .and_then(|tree| tree.into_tree()) | ||
| 42 | }; | ||
| 21 | 43 | ||
| 44 | git_diff::Tree(&main_tree).changes_from(previous_tree.as_ref()); | ||
| 22 | Ok(()) | 45 | Ok(()) |
| 23 | } | 46 | } |
| 24 | #[test] | 47 | #[test] |
| 25 | #[should_panic] | 48 | #[should_panic] |
| 26 | fn file_added() { | 49 | fn file_added() { |
| 50 | diff_at(FIRST_COMMIT).unwrap(); | ||
| 27 | todo!("detect an added file in the root tree") | 51 | todo!("detect an added file in the root tree") |
| 28 | } | 52 | } |
| 29 | } | 53 | } |