Skip to content

Commit 31d3ff0

Browse files
archer-321joshtriplett
authored andcommitted
Add binding for git_commit_body
This patch adds a binding for git_commit_body to retrieve the commit message without the summary paragraph. Additionally, this patch updates the test suite to test commits with a body paragraph. While the commit body was previously available via Commit::message, users would have to reimplement libgit2's behaviour to extract the commit body from it.
1 parent 9d33858 commit 31d3ff0

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

libgit2-sys/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2747,6 +2747,7 @@ extern "C" {
27472747
pub fn git_commit_parentcount(commit: *const git_commit) -> c_uint;
27482748
pub fn git_commit_raw_header(commit: *const git_commit) -> *const c_char;
27492749
pub fn git_commit_summary(commit: *mut git_commit) -> *const c_char;
2750+
pub fn git_commit_body(commit: *mut git_commit) -> *const c_char;
27502751
pub fn git_commit_time(commit: *const git_commit) -> git_time_t;
27512752
pub fn git_commit_time_offset(commit: *const git_commit) -> c_int;
27522753
pub fn git_commit_tree(tree_out: *mut *mut git_tree, commit: *const git_commit) -> c_int;

src/commit.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,29 @@ impl<'repo> Commit<'repo> {
145145
unsafe { crate::opt_bytes(self, raw::git_commit_summary(self.raw)) }
146146
}
147147

148+
/// Get the long "body" of the git commit message.
149+
///
150+
/// The returned message is the body of the commit, comprising everything
151+
/// but the first paragraph of the message. Leading and trailing whitespaces
152+
/// are trimmed.
153+
///
154+
/// `None` may be returned if an error occurs or if the summary is not valid
155+
/// utf-8.
156+
pub fn body(&self) -> Option<&str> {
157+
self.body_bytes().and_then(|s| str::from_utf8(s).ok())
158+
}
159+
160+
/// Get the long "body" of the git commit message.
161+
///
162+
/// The returned message is the body of the commit, comprising everything
163+
/// but the first paragraph of the message. Leading and trailing whitespaces
164+
/// are trimmed.
165+
///
166+
/// `None` may be returned if an error occurs.
167+
pub fn body_bytes(&self) -> Option<&[u8]> {
168+
unsafe { crate::opt_bytes(self, raw::git_commit_body(self.raw)) }
169+
}
170+
148171
/// Get the commit time (i.e. committer time) of a commit.
149172
///
150173
/// The first element of the tuple is the time, in seconds, since the epoch.
@@ -399,12 +422,14 @@ mod tests {
399422
let head = repo.head().unwrap();
400423
let target = head.target().unwrap();
401424
let commit = repo.find_commit(target).unwrap();
402-
assert_eq!(commit.message(), Some("initial"));
425+
assert_eq!(commit.message(), Some("initial\n\nbody"));
426+
assert_eq!(commit.body(), Some("body"));
403427
assert_eq!(commit.id(), target);
404428
commit.message_raw().unwrap();
405429
commit.raw_header().unwrap();
406430
commit.message_encoding();
407431
commit.summary().unwrap();
432+
commit.body().unwrap();
408433
commit.tree_id();
409434
commit.tree().unwrap();
410435
assert_eq!(commit.parents().count(), 0);

src/remote.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ mod tests {
956956
let repo = Repository::clone(&url, td3.path()).unwrap();
957957
let commit = repo.head().unwrap().target().unwrap();
958958
let commit = repo.find_commit(commit).unwrap();
959-
assert_eq!(commit.message(), Some("initial"));
959+
assert_eq!(commit.message(), Some("initial\n\nbody"));
960960
}
961961

962962
#[test]

src/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn repo_init() -> (TempDir, Repository) {
3131

3232
let tree = repo.find_tree(id).unwrap();
3333
let sig = repo.signature().unwrap();
34-
repo.commit(Some("HEAD"), &sig, &sig, "initial", &tree, &[])
34+
repo.commit(Some("HEAD"), &sig, &sig, "initial\n\nbody", &tree, &[])
3535
.unwrap();
3636
}
3737
(td, repo)

0 commit comments

Comments
 (0)