Skip to content

Commit ca6672b

Browse files
Add binding for git_repository_fetchhead_foreach (#738)
* Add binding for git_repository_fetchhead_foreach * Assert pointers are not null in fetchhead_foreach_cb * Pass through remote url in bytes in `fetchhead_foreach` * Document parameters for `fetchhead_foreach` * Fix formatting of docs for `mergehead_foreach`
1 parent 97091c3 commit ca6672b

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

Diff for: libgit2-sys/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,10 @@ pub const GIT_WORKTREE_PRUNE_OPTIONS_VERSION: c_uint = 1;
18921892
pub type git_repository_mergehead_foreach_cb =
18931893
Option<extern "C" fn(oid: *const git_oid, payload: *mut c_void) -> c_int>;
18941894

1895+
pub type git_repository_fetchhead_foreach_cb = Option<
1896+
extern "C" fn(*const c_char, *const c_char, *const git_oid, c_uint, *mut c_void) -> c_int,
1897+
>;
1898+
18951899
git_enum! {
18961900
pub enum git_trace_level_t {
18971901
/* No tracing will be performed. */
@@ -2016,6 +2020,11 @@ extern "C" {
20162020
callback: git_repository_mergehead_foreach_cb,
20172021
payload: *mut c_void,
20182022
) -> c_int;
2023+
pub fn git_repository_fetchhead_foreach(
2024+
repo: *mut git_repository,
2025+
callback: git_repository_fetchhead_foreach_cb,
2026+
payload: *mut c_void,
2027+
) -> c_int;
20192028
pub fn git_ignore_add_rule(repo: *mut git_repository, rules: *const c_char) -> c_int;
20202029
pub fn git_ignore_clear_internal_rules(repo: *mut git_repository) -> c_int;
20212030
pub fn git_ignore_path_is_ignored(

Diff for: src/repo.rs

+65-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ use crate::{DescribeOptions, Diff, DiffOptions, Odb, PackBuilder, TreeBuilder};
3636
use crate::{Note, Notes, ObjectType, Revwalk, Status, StatusOptions, Statuses, Tag, Transaction};
3737

3838
type MergeheadForeachCb<'a> = dyn FnMut(&Oid) -> bool + 'a;
39+
type FetchheadForeachCb<'a> = dyn FnMut(&str, &[u8], &Oid, bool) -> bool + 'a;
40+
41+
struct FetchheadForeachCbData<'a> {
42+
callback: &'a mut FetchheadForeachCb<'a>,
43+
}
3944

4045
struct MergeheadForeachCbData<'a> {
4146
callback: &'a mut MergeheadForeachCb<'a>,
@@ -58,6 +63,39 @@ extern "C" fn mergehead_foreach_cb(oid: *const raw::git_oid, payload: *mut c_voi
5863
.unwrap_or(1)
5964
}
6065

66+
extern "C" fn fetchhead_foreach_cb(
67+
ref_name: *const c_char,
68+
remote_url: *const c_char,
69+
oid: *const raw::git_oid,
70+
is_merge: c_uint,
71+
payload: *mut c_void,
72+
) -> c_int {
73+
panic::wrap(|| unsafe {
74+
let data = &mut *(payload as *mut FetchheadForeachCbData<'_>);
75+
let res = {
76+
let callback = &mut data.callback;
77+
78+
assert!(!ref_name.is_null());
79+
assert!(!remote_url.is_null());
80+
assert!(!oid.is_null());
81+
82+
let ref_name = str::from_utf8(CStr::from_ptr(ref_name).to_bytes()).unwrap();
83+
let remote_url = CStr::from_ptr(remote_url).to_bytes();
84+
let oid = Binding::from_raw(oid);
85+
let is_merge = is_merge == 1;
86+
87+
callback(&ref_name, remote_url, &oid, is_merge)
88+
};
89+
90+
if res {
91+
0
92+
} else {
93+
1
94+
}
95+
})
96+
.unwrap_or(1)
97+
}
98+
6199
/// An owned git repository, representing all state associated with the
62100
/// underlying filesystem.
63101
///
@@ -3017,7 +3055,7 @@ impl Repository {
30173055
}
30183056

30193057
/// If a merge is in progress, invoke 'callback' for each commit ID in the
3020-
/// * MERGE_HEAD file.
3058+
/// MERGE_HEAD file.
30213059
pub fn mergehead_foreach<C>(&mut self, mut callback: C) -> Result<(), Error>
30223060
where
30233061
C: FnMut(&Oid) -> bool,
@@ -3035,6 +3073,32 @@ impl Repository {
30353073
Ok(())
30363074
}
30373075
}
3076+
3077+
/// Invoke 'callback' for each entry in the given FETCH_HEAD file.
3078+
///
3079+
/// `callback` will be called with with following arguments:
3080+
///
3081+
/// - `&str`: the reference name
3082+
/// - `&[u8]`: the remote url
3083+
/// - `&Oid`: the reference target OID
3084+
/// - `bool`: was the reference the result of a merge
3085+
pub fn fetchhead_foreach<C>(&self, mut callback: C) -> Result<(), Error>
3086+
where
3087+
C: FnMut(&str, &[u8], &Oid, bool) -> bool,
3088+
{
3089+
unsafe {
3090+
let mut data = FetchheadForeachCbData {
3091+
callback: &mut callback,
3092+
};
3093+
let cb: raw::git_repository_fetchhead_foreach_cb = Some(fetchhead_foreach_cb);
3094+
try_call!(raw::git_repository_fetchhead_foreach(
3095+
self.raw(),
3096+
cb,
3097+
&mut data as *mut _ as *mut _
3098+
));
3099+
Ok(())
3100+
}
3101+
}
30383102
}
30393103

30403104
impl Binding for Repository {

0 commit comments

Comments
 (0)