Skip to content

Commit 3eb498a

Browse files
alexanderkjallalexcrichton
authored andcommitted
added bindings for git_commit_create_buffer
1 parent 6db5851 commit 3eb498a

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

libgit2-sys/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,6 +2359,17 @@ extern "C" {
23592359
parent_count: size_t,
23602360
parents: *mut *const git_commit,
23612361
) -> c_int;
2362+
pub fn git_commit_create_buffer(
2363+
out: *mut git_buf,
2364+
repo: *mut git_repository,
2365+
author: *const git_signature,
2366+
committer: *const git_signature,
2367+
message_encoding: *const c_char,
2368+
message: *const c_char,
2369+
tree: *const git_tree,
2370+
parent_count: size_t,
2371+
parents: *mut *const git_commit,
2372+
) -> c_int;
23622373
pub fn git_commit_header_field(
23632374
out: *mut git_buf,
23642375
commit: *const git_commit,

src/repo.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,40 @@ impl Repository {
10891089
}
10901090
}
10911091

1092+
/// Create a commit object and return that as a string.
1093+
///
1094+
/// This string is suitable to be passed to the `commit_signed` function,
1095+
/// the arguments behave the same as in the `commit` function.
1096+
pub fn commit_create_buffer(
1097+
&self,
1098+
author: &Signature<'_>,
1099+
committer: &Signature<'_>,
1100+
message: &str,
1101+
tree: &Tree<'_>,
1102+
parents: &[&Commit<'_>],
1103+
) -> Result<String, Error> {
1104+
let mut parent_ptrs = parents
1105+
.iter()
1106+
.map(|p| p.raw() as *const raw::git_commit)
1107+
.collect::<Vec<_>>();
1108+
let message = CString::new(message)?;
1109+
let buf = Buf::new();
1110+
unsafe {
1111+
try_call!(raw::git_commit_create_buffer(
1112+
buf.raw(),
1113+
self.raw(),
1114+
author.raw(),
1115+
committer.raw(),
1116+
ptr::null(),
1117+
message,
1118+
tree.raw(),
1119+
parents.len() as size_t,
1120+
parent_ptrs.as_mut_ptr()
1121+
));
1122+
Ok(str::from_utf8(&buf).unwrap().to_string())
1123+
}
1124+
}
1125+
10921126
/// Create a commit object from the given buffer and signature
10931127
///
10941128
/// Given the unsigned commit object's contents, its signature and the

0 commit comments

Comments
 (0)