Skip to content

Commit

Permalink
Merge pull request ostreedev#2701 from cgwalters/bind-list-commits
Browse files Browse the repository at this point in the history
 rust: Bind `ostree_repo_list_commits_starting_with`
  • Loading branch information
cgwalters authored Aug 24, 2022
2 parents a266fab + b55054e commit 93a6d7b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rust-bindings/conf/ostree.toml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ concurrency = "send"
pattern = "^(load_file)$"
ignore = true

[[object.function]]
# [MANUAL] hash table with variants
pattern = "^(list_commit_objects_starting_with)$"
ignore = true

[[object]]
name = "OSTree.RepoFinder"
status = "generate"
Expand Down
48 changes: 48 additions & 0 deletions rust-bindings/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,4 +556,52 @@ impl Repo {
// Safety: We know the variant type will match since we just passed it above
Ok(crate::DirMetaParsed::from_variant(&v).unwrap())
}

/// List all commit objects; an optional prefix filter may be applied.
#[doc(alias = "ostree_repo_list_commit_objects_starting_with")]
pub fn list_commit_objects_starting_with<P: IsA<gio::Cancellable>>(
&self,
prefix: Option<&str>,
cancellable: Option<&P>,
) -> Result<HashSet<glib::GString>, glib::Error> {
use glib::ffi::gpointer;
let prefix = prefix.unwrap_or("");
unsafe {
let repo = self.to_glib_none().0;
let mut commits = ptr::null_mut();
let cancellable = cancellable.map(|p| p.as_ref()).to_glib_none().0;
let mut error = ptr::null_mut();
let prefix = prefix.to_glib_none();
let r = ffi::ostree_repo_list_commit_objects_starting_with(
repo,
prefix.0,
&mut commits,
cancellable,
&mut error,
);
if !error.is_null() {
assert_eq!(r, 0);
return Err(from_glib_full(error));
}
assert_ne!(r, 0);
let mut ret = HashSet::with_capacity(glib::ffi::g_hash_table_size(commits) as usize);
unsafe extern "C" fn visit_hash_table(
key: *mut libc::c_void,
_value: gpointer,
r: *mut libc::c_void,
) -> glib::ffi::gboolean {
let key: glib::Variant = from_glib_none(key as *const glib::ffi::GVariant);
let checksum = crate::object_name_deserialize(&key).0;
let r = &mut *(r as *mut HashSet<glib::GString>);
r.insert(checksum);
true.into()
}
glib::ffi::g_hash_table_foreach_remove(
commits,
Some(visit_hash_table),
&mut ret as *mut HashSet<glib::GString> as *mut _,
);
Ok(ret)
}
}
}
27 changes: 27 additions & 0 deletions rust-bindings/tests/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ fn should_commit_content_to_repo_and_list_refs_again() {
assert_eq!(checksum, refs["test"]);
}

#[test]
fn list_commits() {
let cancellable = gio::NONE_CANCELLABLE;
let test_repo = TestRepo::new();

for prefix in [None, Some("a"), Some("0abcde")] {
let commits = test_repo
.repo
.list_commit_objects_starting_with(prefix, cancellable)
.unwrap();
assert_eq!(commits.len(), 0);
}

let rev = test_repo.test_commit("testref");

for prefix in [None, Some(&rev[0..1]), Some(&rev[0..5])] {
let commits = test_repo
.repo
.list_commit_objects_starting_with(prefix, cancellable)
.unwrap()
.into_iter()
.collect::<Vec<_>>();
assert_eq!(commits.len(), 1);
assert_eq!(commits[0].as_str(), rev.as_str());
}
}

#[test]
#[cfg(feature = "cap-std-apis")]
fn cap_std_commit() {
Expand Down

0 comments on commit 93a6d7b

Please sign in to comment.