Skip to content

Commit

Permalink
add tag_foreach to repo (#594) (#595)
Browse files Browse the repository at this point in the history
* add tag_foreach to repo (#594)

* switch to raw bytes for tag name
  • Loading branch information
Stephan Dilly committed Jul 20, 2020
1 parent 9f5cef6 commit a3e129e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -679,6 +679,7 @@ mod stash;
mod status;
mod submodule;
mod tag;
mod tagforeach;
mod time;
mod tree;
mod treebuilder;
Expand Down
21 changes: 21 additions & 0 deletions src/repo.rs
Expand Up @@ -14,6 +14,7 @@ use crate::diff::{
use crate::oid_array::OidArray;
use crate::stash::{stash_cb, StashApplyOptions, StashCbData};
use crate::string_array::StringArray;
use crate::tagforeach::{tag_foreach_cb, TagForeachCB, TagForeachData};
use crate::util::{self, path_to_repo_path, Binding};
use crate::CherrypickOptions;
use crate::RevertOptions;
Expand Down Expand Up @@ -1731,6 +1732,26 @@ impl Repository {
}
}

/// iterate over all tags calling `cb` on each.
/// the callback is provided the tag id and name
pub fn tag_foreach<T>(&self, cb: T) -> Result<(), Error>
where
T: FnMut(Oid, &[u8]) -> bool,
{
let mut data = TagForeachData {
cb: Box::new(cb) as TagForeachCB<'_>,
};

unsafe {
raw::git_tag_foreach(
self.raw,
Some(tag_foreach_cb),
(&mut data) as *mut _ as *mut _,
);
}
Ok(())
}

/// Updates files in the index and the working tree to match the content of
/// the commit pointed at by HEAD.
pub fn checkout_head(&self, opts: Option<&mut CheckoutBuilder<'_>>) -> Result<(), Error> {
Expand Down
69 changes: 69 additions & 0 deletions src/tagforeach.rs
@@ -0,0 +1,69 @@
//! git_tag_foreach support
//! see original: https://libgit2.org/libgit2/#HEAD/group/tag/git_tag_foreach

use crate::{panic, raw, util::Binding, Oid};
use libc::{c_char, c_int};
use raw::git_oid;
use std::ffi::{c_void, CStr};

/// boxed callback type
pub(crate) type TagForeachCB<'a> = Box<dyn FnMut(Oid, &[u8]) -> bool + 'a>;

/// helper type to be able to pass callback to payload
pub(crate) struct TagForeachData<'a> {
/// callback
pub(crate) cb: TagForeachCB<'a>,
}

/// c callback forwarding to rust callback inside `TagForeachData`
/// see original: https://libgit2.org/libgit2/#HEAD/group/callback/git_tag_foreach_cb
pub(crate) extern "C" fn tag_foreach_cb(
name: *const c_char,
oid: *mut git_oid,
payload: *mut c_void,
) -> c_int {
panic::wrap(|| unsafe {
let id: Oid = Binding::from_raw(oid as *const _);

let name = CStr::from_ptr(name);
let name = name.to_bytes();

let payload = &mut *(payload as *mut TagForeachData<'_>);
let cb = &mut payload.cb;

let res = cb(id, name);

if res {
0
} else {
-1
}
})
.unwrap_or(-1)
}

#[cfg(test)]
mod tests {

#[test]
fn smoke() {
let (_td, repo) = crate::test::repo_init();
let head = repo.head().unwrap();
let id = head.target().unwrap();
assert!(repo.find_tag(id).is_err());

let obj = repo.find_object(id, None).unwrap();
let sig = repo.signature().unwrap();
let tag_id = repo.tag("foo", &obj, &sig, "msg", false).unwrap();

let mut tags = Vec::new();
repo.tag_foreach(|id, name| {
tags.push((id, String::from_utf8(name.into()).unwrap()));
true
})
.unwrap();

assert_eq!(tags[0].0, tag_id);
assert_eq!(tags[0].1, "refs/tags/foo");
}
}

0 comments on commit a3e129e

Please sign in to comment.