Debian Codegraph @

src/refdb.rs

raw
1 use std::marker;
2
3 use crate::util::Binding;
4 use crate::{raw, Error, Repository};
5
6 /// A structure to represent a git reference database.
7 pub struct Refdb<'repo> {
8 raw: *mut raw::git_refdb,
9 _marker: marker::PhantomData<&'repo Repository>,
10 }
11
12 impl Drop for Refdb<'_> {
13 fn drop(&mut self) {
14 unsafe { raw::git_refdb_free(self.raw) }
15 }
16 }
17
18 impl<'repo> Binding for Refdb<'repo> {
19 type Raw = *mut raw::git_refdb;
20
21 unsafe fn from_raw(raw: *mut raw::git_refdb) -> Refdb<'repo> {
22 Refdb {
23 raw,
24 _marker: marker::PhantomData,
25 }
26 }
27
28 fn raw(&self) -> *mut raw::git_refdb {
29 self.raw
30 }
31 }
32
33 impl<'repo> Refdb<'repo> {
34 /// Suggests that the reference database compress or optimize its
35 /// references. This mechanism is implementation specific. For on-disk
36 /// reference databases, for example, this may pack all loose references.
37 pub fn compress(&self) -> Result<(), Error> {
38 unsafe {
39 try_call!(raw::git_refdb_compress(self.raw));
40 }
41 Ok(())
42 }
43 }
44
45 #[cfg(test)]
46 mod tests {
47 #[test]
48 fn smoke() {
49 let (_td, repo) = crate::test::repo_init();
50 let refdb = repo.refdb().unwrap();
51 refdb.compress().unwrap();
52 }
53
54 #[test]
55 fn set_refdb_roundtrip() {
56 let (_td, repo) = crate::test::repo_init();
57 let refdb = repo.refdb().unwrap();
58 repo.set_refdb(&refdb).unwrap();
59 // References should still be resolvable after setting the same refdb back.
60 repo.refname_to_id("HEAD").unwrap();
61 }
62
63 #[test]
64 fn compress_with_loose_refs() {
65 let (_td, repo) = crate::test::repo_init();
66 let head_id = repo.refname_to_id("HEAD").unwrap();
67 for i in 0..10 {
68 repo.reference(
69 &format!("refs/tags/refdb-test-{}", i),
70 head_id,
71 false,
72 "test",
73 )
74 .unwrap();
75 }
76 let refdb = repo.refdb().unwrap();
77 refdb.compress().unwrap();
78 assert_eq!(
79 repo.references_glob("refs/tags/refdb-test-*")
80 .unwrap()
81 .count(),
82 10
83 );
84 }
85 }