Skip to content

Commit a3e129e

Browse files
author
Stephan Dilly
authored
add tag_foreach to repo (#594) (#595)
* add tag_foreach to repo (#594) * switch to raw bytes for tag name
1 parent 9f5cef6 commit a3e129e

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ mod stash;
679679
mod status;
680680
mod submodule;
681681
mod tag;
682+
mod tagforeach;
682683
mod time;
683684
mod tree;
684685
mod treebuilder;

src/repo.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::diff::{
1414
use crate::oid_array::OidArray;
1515
use crate::stash::{stash_cb, StashApplyOptions, StashCbData};
1616
use crate::string_array::StringArray;
17+
use crate::tagforeach::{tag_foreach_cb, TagForeachCB, TagForeachData};
1718
use crate::util::{self, path_to_repo_path, Binding};
1819
use crate::CherrypickOptions;
1920
use crate::RevertOptions;
@@ -1731,6 +1732,26 @@ impl Repository {
17311732
}
17321733
}
17331734

1735+
/// iterate over all tags calling `cb` on each.
1736+
/// the callback is provided the tag id and name
1737+
pub fn tag_foreach<T>(&self, cb: T) -> Result<(), Error>
1738+
where
1739+
T: FnMut(Oid, &[u8]) -> bool,
1740+
{
1741+
let mut data = TagForeachData {
1742+
cb: Box::new(cb) as TagForeachCB<'_>,
1743+
};
1744+
1745+
unsafe {
1746+
raw::git_tag_foreach(
1747+
self.raw,
1748+
Some(tag_foreach_cb),
1749+
(&mut data) as *mut _ as *mut _,
1750+
);
1751+
}
1752+
Ok(())
1753+
}
1754+
17341755
/// Updates files in the index and the working tree to match the content of
17351756
/// the commit pointed at by HEAD.
17361757
pub fn checkout_head(&self, opts: Option<&mut CheckoutBuilder<'_>>) -> Result<(), Error> {

src/tagforeach.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! git_tag_foreach support
2+
//! see original: https://libgit2.org/libgit2/#HEAD/group/tag/git_tag_foreach
3+
4+
use crate::{panic, raw, util::Binding, Oid};
5+
use libc::{c_char, c_int};
6+
use raw::git_oid;
7+
use std::ffi::{c_void, CStr};
8+
9+
/// boxed callback type
10+
pub(crate) type TagForeachCB<'a> = Box<dyn FnMut(Oid, &[u8]) -> bool + 'a>;
11+
12+
/// helper type to be able to pass callback to payload
13+
pub(crate) struct TagForeachData<'a> {
14+
/// callback
15+
pub(crate) cb: TagForeachCB<'a>,
16+
}
17+
18+
/// c callback forwarding to rust callback inside `TagForeachData`
19+
/// see original: https://libgit2.org/libgit2/#HEAD/group/callback/git_tag_foreach_cb
20+
pub(crate) extern "C" fn tag_foreach_cb(
21+
name: *const c_char,
22+
oid: *mut git_oid,
23+
payload: *mut c_void,
24+
) -> c_int {
25+
panic::wrap(|| unsafe {
26+
let id: Oid = Binding::from_raw(oid as *const _);
27+
28+
let name = CStr::from_ptr(name);
29+
let name = name.to_bytes();
30+
31+
let payload = &mut *(payload as *mut TagForeachData<'_>);
32+
let cb = &mut payload.cb;
33+
34+
let res = cb(id, name);
35+
36+
if res {
37+
0
38+
} else {
39+
-1
40+
}
41+
})
42+
.unwrap_or(-1)
43+
}
44+
45+
#[cfg(test)]
46+
mod tests {
47+
48+
#[test]
49+
fn smoke() {
50+
let (_td, repo) = crate::test::repo_init();
51+
let head = repo.head().unwrap();
52+
let id = head.target().unwrap();
53+
assert!(repo.find_tag(id).is_err());
54+
55+
let obj = repo.find_object(id, None).unwrap();
56+
let sig = repo.signature().unwrap();
57+
let tag_id = repo.tag("foo", &obj, &sig, "msg", false).unwrap();
58+
59+
let mut tags = Vec::new();
60+
repo.tag_foreach(|id, name| {
61+
tags.push((id, String::from_utf8(name.into()).unwrap()));
62+
true
63+
})
64+
.unwrap();
65+
66+
assert_eq!(tags[0].0, tag_id);
67+
assert_eq!(tags[0].1, "refs/tags/foo");
68+
}
69+
}

0 commit comments

Comments
 (0)