mirror of
https://seed.radicle.dev/rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt.git
synced 2026-10-25 14:20:31 +08:00
Merge remote-tracking branch 'origin/update-doc'
* origin/update-doc: update docs and add test for commit signature Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
use crate::{commit, diff, fs, glob, namespace, refs, repo};
|
||||
use thiserror::Error;
|
||||
|
||||
/// The crate level error type that wraps up module level error types.
|
||||
#[derive(Debug, Error)]
|
||||
#[non_exhaustive]
|
||||
pub enum Error {
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
pub use git_ref_format;
|
||||
|
||||
/// Represents an object id in Git. Re-exported from `radicle-git-ext`.
|
||||
pub use radicle_git_ext::Oid;
|
||||
pub type Oid = radicle_git_ext::Oid;
|
||||
|
||||
pub mod blob;
|
||||
pub mod diff;
|
||||
|
||||
@@ -375,19 +375,22 @@ impl Repository {
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// `commit_oid` - The object ID of the commit
|
||||
/// `field` - the name of the header field containing the signature block;
|
||||
/// pass `None` to extract the default 'gpgsig'
|
||||
pub fn extract_signature(
|
||||
&self,
|
||||
commit_oid: &Oid,
|
||||
commit: impl ToCommit,
|
||||
field: Option<&str>,
|
||||
) -> Result<Option<Signature>, Error> {
|
||||
// Match is necessary here because according to the documentation for
|
||||
// git_commit_extract_signature at
|
||||
// https://libgit2.org/libgit2/#HEAD/group/commit/git_commit_extract_signature
|
||||
// the return value for a commit without a signature will be GIT_ENOTFOUND
|
||||
match self.inner.extract_signature(commit_oid, field) {
|
||||
let commit = commit
|
||||
.to_commit(self)
|
||||
.map_err(|e| Error::ToCommit(e.into()))?;
|
||||
|
||||
match self.inner.extract_signature(&commit.id, field) {
|
||||
Err(error) => {
|
||||
if error.code() == git2::ErrorCode::NotFound {
|
||||
Ok(None)
|
||||
|
||||
@@ -80,3 +80,21 @@ fn test_commit_history() {
|
||||
|
||||
assert_eq!(h1.head().id, h2.head().id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_commit_signature() {
|
||||
let repo = Repository::open(GIT_PLATINUM).unwrap();
|
||||
let commit_with_signature = "e24124b7538658220b5aaf3b6ef53758f0a106dc";
|
||||
let signature = repo.extract_signature(commit_with_signature, None).unwrap();
|
||||
assert!(signature.is_some());
|
||||
|
||||
let commit_without_signature = "80bacafba303bf0cdf6142921f430ff265f25095";
|
||||
let signature = repo
|
||||
.extract_signature(commit_without_signature, None)
|
||||
.unwrap();
|
||||
assert!(signature.is_none());
|
||||
|
||||
let commit_nonexist = "8080808080";
|
||||
let signature = repo.extract_signature(commit_nonexist, None);
|
||||
assert!(signature.is_err());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user