Skip to content

Add binding for git_commit_body #835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2747,6 +2747,7 @@ extern "C" {
pub fn git_commit_parentcount(commit: *const git_commit) -> c_uint;
pub fn git_commit_raw_header(commit: *const git_commit) -> *const c_char;
pub fn git_commit_summary(commit: *mut git_commit) -> *const c_char;
pub fn git_commit_body(commit: *mut git_commit) -> *const c_char;
pub fn git_commit_time(commit: *const git_commit) -> git_time_t;
pub fn git_commit_time_offset(commit: *const git_commit) -> c_int;
pub fn git_commit_tree(tree_out: *mut *mut git_tree, commit: *const git_commit) -> c_int;
Expand Down
27 changes: 26 additions & 1 deletion src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,29 @@ impl<'repo> Commit<'repo> {
unsafe { crate::opt_bytes(self, raw::git_commit_summary(self.raw)) }
}

/// Get the long "body" of the git commit message.
///
/// The returned message is the body of the commit, comprising everything
/// but the first paragraph of the message. Leading and trailing whitespaces
/// are trimmed.
///
/// `None` may be returned if an error occurs or if the summary is not valid
/// utf-8.
pub fn body(&self) -> Option<&str> {
self.body_bytes().and_then(|s| str::from_utf8(s).ok())
}

/// Get the long "body" of the git commit message.
///
/// The returned message is the body of the commit, comprising everything
/// but the first paragraph of the message. Leading and trailing whitespaces
/// are trimmed.
///
/// `None` may be returned if an error occurs.
pub fn body_bytes(&self) -> Option<&[u8]> {
unsafe { crate::opt_bytes(self, raw::git_commit_body(self.raw)) }
}

/// Get the commit time (i.e. committer time) of a commit.
///
/// The first element of the tuple is the time, in seconds, since the epoch.
Expand Down Expand Up @@ -399,12 +422,14 @@ mod tests {
let head = repo.head().unwrap();
let target = head.target().unwrap();
let commit = repo.find_commit(target).unwrap();
assert_eq!(commit.message(), Some("initial"));
assert_eq!(commit.message(), Some("initial\n\nbody"));
assert_eq!(commit.body(), Some("body"));
assert_eq!(commit.id(), target);
commit.message_raw().unwrap();
commit.raw_header().unwrap();
commit.message_encoding();
commit.summary().unwrap();
commit.body().unwrap();
commit.tree_id();
commit.tree().unwrap();
assert_eq!(commit.parents().count(), 0);
Expand Down
2 changes: 1 addition & 1 deletion src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ mod tests {
let repo = Repository::clone(&url, td3.path()).unwrap();
let commit = repo.head().unwrap().target().unwrap();
let commit = repo.find_commit(commit).unwrap();
assert_eq!(commit.message(), Some("initial"));
assert_eq!(commit.message(), Some("initial\n\nbody"));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn repo_init() -> (TempDir, Repository) {

let tree = repo.find_tree(id).unwrap();
let sig = repo.signature().unwrap();
repo.commit(Some("HEAD"), &sig, &sig, "initial", &tree, &[])
repo.commit(Some("HEAD"), &sig, &sig, "initial\n\nbody", &tree, &[])
.unwrap();
}
(td, repo)
Expand Down