From e8dd7d570a393e35dee8a7c7e44fb93db90f04e3 Mon Sep 17 00:00:00 2001 From: bordumb Date: Sun, 8 Mar 2026 23:58:01 +0000 Subject: [PATCH 1/3] style: fix rustfmt formatting in refdb_compress tests --- src/repo.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/repo.rs b/src/repo.rs index da498ef348..1d233f9e98 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -1232,6 +1232,20 @@ impl Repository { Ok(()) } + /// Suggests that the reference database compress or optimize its + /// references. This mechanism is implementation specific. For on-disk + /// reference databases, for example, this may pack all loose references. + pub fn refdb_compress(&self) -> Result<(), Error> { + let mut refdb = ptr::null_mut(); + unsafe { + try_call!(raw::git_repository_refdb(&mut refdb, self.raw())); + let result = crate::call::c_try(raw::git_refdb_compress(refdb)); + raw::git_refdb_free(refdb); + result?; + } + Ok(()) + } + /// Create a new branch pointing at a target commit /// /// A new direct reference will be created pointing to this target commit. @@ -4557,4 +4571,64 @@ Committer Name "#, crate::test::realpath(worktree_repo.commondir()).unwrap() ); } + + #[test] + fn smoke_refdb_compress() { + let (_td, repo) = crate::test::repo_init(); + // Compressing an empty-ish refdb should succeed. + repo.refdb_compress().unwrap(); + } + + #[test] + fn refdb_compress_with_loose_refs() { + let (_td, repo) = crate::test::repo_init(); + let head_id = repo.refname_to_id("HEAD").unwrap(); + + // Create several loose refs. + for i in 0..20 { + repo.reference(&format!("refs/tags/test-{}", i), head_id, false, "test ref") + .unwrap(); + } + + // Verify refs exist. + assert!(repo.references_glob("refs/tags/test-*").unwrap().count() == 20); + + // Compress should pack them without error. + repo.refdb_compress().unwrap(); + + // Refs should still be resolvable after packing. + assert!(repo.references_glob("refs/tags/test-*").unwrap().count() == 20); + for i in 0..20 { + let r = repo + .find_reference(&format!("refs/tags/test-{}", i)) + .unwrap(); + assert_eq!(r.target().unwrap(), head_id); + } + } + + #[test] + fn refdb_compress_bare_repo() { + let td = TempDir::new().unwrap(); + let repo = Repository::init_bare(td.path()).unwrap(); + // Compressing a bare repo with no refs should succeed. + repo.refdb_compress().unwrap(); + } + + #[test] + fn refdb_compress_idempotent() { + let (_td, repo) = crate::test::repo_init(); + let head_id = repo.refname_to_id("HEAD").unwrap(); + + for i in 0..5 { + repo.reference(&format!("refs/tags/idem-{}", i), head_id, false, "test") + .unwrap(); + } + + // Compress multiple times — should be safe and idempotent. + repo.refdb_compress().unwrap(); + repo.refdb_compress().unwrap(); + repo.refdb_compress().unwrap(); + + assert!(repo.references_glob("refs/tags/idem-*").unwrap().count() == 5); + } } From 751cf562508039db400f94c593c8e0fc90872d33 Mon Sep 17 00:00:00 2001 From: bordumb Date: Mon, 9 Mar 2026 20:47:37 +0000 Subject: [PATCH 2/3] tests: assert packed-refs file existence before and after refdb_compress --- src/repo.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/repo.rs b/src/repo.rs index 1d233f9e98..ac9bb55de3 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -4593,9 +4593,16 @@ Committer Name "#, // Verify refs exist. assert!(repo.references_glob("refs/tags/test-*").unwrap().count() == 20); + // packed-refs should not exist yet (refs are still loose). + let packed_refs = repo.path().join("packed-refs"); + assert!(!packed_refs.exists(), "packed-refs should not exist before compress"); + // Compress should pack them without error. repo.refdb_compress().unwrap(); + // packed-refs should now exist after compressing. + assert!(packed_refs.exists(), "packed-refs should exist after compress"); + // Refs should still be resolvable after packing. assert!(repo.references_glob("refs/tags/test-*").unwrap().count() == 20); for i in 0..20 { From 7fc4877ccc599ba7acc30fb6b58ea8e8cc05dc25 Mon Sep 17 00:00:00 2001 From: bordumb Date: Mon, 9 Mar 2026 21:04:29 +0000 Subject: [PATCH 3/3] fix: cargo fmt fixing --- src/repo.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/repo.rs b/src/repo.rs index ac9bb55de3..ab686b5bc0 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -4595,13 +4595,19 @@ Committer Name "#, // packed-refs should not exist yet (refs are still loose). let packed_refs = repo.path().join("packed-refs"); - assert!(!packed_refs.exists(), "packed-refs should not exist before compress"); + assert!( + !packed_refs.exists(), + "packed-refs should not exist before compress" + ); // Compress should pack them without error. repo.refdb_compress().unwrap(); // packed-refs should now exist after compressing. - assert!(packed_refs.exists(), "packed-refs should exist after compress"); + assert!( + packed_refs.exists(), + "packed-refs should exist after compress" + ); // Refs should still be resolvable after packing. assert!(repo.references_glob("refs/tags/test-*").unwrap().count() == 20);