History

First sketch of diff API

Sebastian Thiel committed 5 years ago

Commit fc3f2b71 parent ae3eab63 files · +46 −2Browse files

Showing the changes to git-diff (2 of 3 files). Show all

Unified · Split

2 files
  1. git-diff/src/lib.rs +13 −0
  2. git-diff/tests/tree/mod.rs +26 −2
git-diff/src/lib.rs+13−0
@@ -1 +1,14 @@
1+// TODO: add deny(missing_docs)
12 #![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+}
git-diff/tests/tree/mod.rs+26−2
@@ -1,11 +1,16 @@
11 mod with_tree {
22 use git_odb::{pack, Locate};
3+ const FIRST_COMMIT: &str = "055df97e18cd537da3cb16bcbdf1733fdcdfb430";
34
45 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+ )?;
611 let commit_id = git_hash::ObjectId::from_hex(commit_id.as_bytes())?;
712 let mut buf = Vec::new();
8− let (_main_tree_id, _parent_commit_id) = {
13+ let (main_tree_id, parent_commit_id) = {
914 let commit = db
1015 .locate(commit_id, &mut buf, &mut pack::cache::Never)?
1116 .expect("start commit to be present")
@@ -18,12 +23,31 @@ mod with_tree { · 6 unmodified lines
1823 p
1924 })
2025 };
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+ };
2143
44+ git_diff::Tree(&main_tree).changes_from(previous_tree.as_ref());
2245 Ok(())
2346 }
2447 #[test]
2548 #[should_panic]
2649 fn file_added() {
50+ diff_at(FIRST_COMMIT).unwrap();
2751 todo!("detect an added file in the root tree")
2852 }
2953 }