From 06af1c5fe95035f38d31511b02ec44da6a8d533b Mon Sep 17 00:00:00 2001 From: Caleb Owens Date: Sun, 29 Sep 2024 23:23:50 +0200 Subject: [PATCH] Ensure that the index indeed remains unchanged --- crates/gitbutler-repo/src/repository_ext.rs | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/crates/gitbutler-repo/src/repository_ext.rs b/crates/gitbutler-repo/src/repository_ext.rs index 17504354b..66df3c90d 100644 --- a/crates/gitbutler-repo/src/repository_ext.rs +++ b/crates/gitbutler-repo/src/repository_ext.rs @@ -1020,5 +1020,38 @@ mod test { assert!(tree.get_name("file1.txt").is_none()); assert!(tree.get_name("file2.txt").is_some()); } + + #[test] + fn should_not_change_index() { + let test_repository = TestingRepository::open(); + + let commit = test_repository.commit_tree(None, &[("file1.txt", "content1")]); + test_repository + .repository + .branch("master", &commit, true) + .unwrap(); + + let mut index = test_repository.repository.index().unwrap(); + index.remove_all(["*"], None).unwrap(); + index.write().unwrap(); + + let index_tree = index.write_tree().unwrap(); + let index_tree = test_repository.repository.find_tree(index_tree).unwrap(); + assert_eq!(index_tree.len(), 0); + + let tree: git2::Tree = test_repository.repository.create_wd_tree().unwrap(); + + let mut index = test_repository.repository.index().unwrap(); + let index_tree = index.write_tree().unwrap(); + let index_tree = test_repository.repository.find_tree(index_tree).unwrap(); + assert_eq!(index_tree.len(), 0); + + // Tree should match whatever is written on disk + assert_tree_matches( + &test_repository.repository, + &tree, + &[("file1.txt", b"content1")], + ); + } } }