Skip to content

Commit 2358d2c

Browse files
author
Stephan Dilly
authored
added git_repository_mergehead_foreach (#707)
1 parent 1b1499a commit 2358d2c

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

libgit2-sys/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,6 +1889,9 @@ pub struct git_worktree_prune_options {
18891889

18901890
pub const GIT_WORKTREE_PRUNE_OPTIONS_VERSION: c_uint = 1;
18911891

1892+
pub type git_repository_mergehead_foreach_cb =
1893+
Option<extern "C" fn(oid: *const git_oid, payload: *mut c_void) -> c_int>;
1894+
18921895
extern "C" {
18931896
// threads
18941897
pub fn git_libgit2_init() -> c_int;
@@ -1981,6 +1984,11 @@ extern "C" {
19811984
repo: *mut git_repository,
19821985
recurse_submodules: c_int,
19831986
) -> c_int;
1987+
pub fn git_repository_mergehead_foreach(
1988+
repo: *mut git_repository,
1989+
callback: git_repository_mergehead_foreach_cb,
1990+
payload: *mut c_void,
1991+
) -> c_int;
19841992
pub fn git_ignore_add_rule(repo: *mut git_repository, rules: *const c_char) -> c_int;
19851993
pub fn git_ignore_clear_internal_rules(repo: *mut git_repository) -> c_int;
19861994
pub fn git_ignore_path_is_ignored(

src/repo.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::build::{CheckoutBuilder, RepoBuilder};
1111
use crate::diff::{
1212
binary_cb_c, file_cb_c, hunk_cb_c, line_cb_c, BinaryCb, DiffCallbacks, FileCb, HunkCb, LineCb,
1313
};
14-
use crate::mailmap::Mailmap;
1514
use crate::oid_array::OidArray;
1615
use crate::stash::{stash_cb, StashApplyOptions, StashCbData};
1716
use crate::string_array::StringArray;
@@ -20,6 +19,7 @@ use crate::util::{self, path_to_repo_path, Binding};
2019
use crate::worktree::{Worktree, WorktreeAddOptions};
2120
use crate::CherrypickOptions;
2221
use crate::RevertOptions;
22+
use crate::{mailmap::Mailmap, panic};
2323
use crate::{
2424
raw, AttrCheckFlags, Buf, Error, Object, Remote, RepositoryOpenFlags, RepositoryState, Revspec,
2525
StashFlags,
@@ -35,6 +35,29 @@ use crate::{Describe, IntoCString, Reflog, RepositoryInitMode, RevparseMode};
3535
use crate::{DescribeOptions, Diff, DiffOptions, Odb, PackBuilder, TreeBuilder};
3636
use crate::{Note, Notes, ObjectType, Revwalk, Status, StatusOptions, Statuses, Tag, Transaction};
3737

38+
type MergeheadForeachCb<'a> = dyn FnMut(&Oid) -> bool + 'a;
39+
40+
struct MergeheadForeachCbData<'a> {
41+
callback: &'a mut MergeheadForeachCb<'a>,
42+
}
43+
44+
extern "C" fn mergehead_foreach_cb(oid: *const raw::git_oid, payload: *mut c_void) -> c_int {
45+
panic::wrap(|| unsafe {
46+
let data = &mut *(payload as *mut MergeheadForeachCbData<'_>);
47+
let res = {
48+
let callback = &mut data.callback;
49+
callback(&Binding::from_raw(oid))
50+
};
51+
52+
if res {
53+
0
54+
} else {
55+
1
56+
}
57+
})
58+
.unwrap_or(1)
59+
}
60+
3861
/// An owned git repository, representing all state associated with the
3962
/// underlying filesystem.
4063
///
@@ -2972,6 +2995,26 @@ impl Repository {
29722995
Ok(Binding::from_raw(ret))
29732996
}
29742997
}
2998+
2999+
/// If a merge is in progress, invoke 'callback' for each commit ID in the
3000+
/// * MERGE_HEAD file.
3001+
pub fn mergehead_foreach<C>(&mut self, mut callback: C) -> Result<(), Error>
3002+
where
3003+
C: FnMut(&Oid) -> bool,
3004+
{
3005+
unsafe {
3006+
let mut data = MergeheadForeachCbData {
3007+
callback: &mut callback,
3008+
};
3009+
let cb: raw::git_repository_mergehead_foreach_cb = Some(mergehead_foreach_cb);
3010+
try_call!(raw::git_repository_mergehead_foreach(
3011+
self.raw(),
3012+
cb,
3013+
&mut data as *mut _ as *mut _
3014+
));
3015+
Ok(())
3016+
}
3017+
}
29753018
}
29763019

29773020
impl Binding for Repository {

0 commit comments

Comments
 (0)