Radish alpha
r
rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt
Git libraries for Radicle
Radicle
Git
update docs and add test for commit signature
Han Xu committed 3 years ago
commit 5e7c924fba69607ce646d574026391189989a201
parent aa6459c
5 files changed +26 -5
modified radicle-surf/Cargo.toml
@@ -49,7 +49,6 @@ features = ["serde"]

[dependencies.radicle-std-ext]
version = "0.1.0"
-
path = "../radicle-std-ext"

[dependencies.serde]
version = "1"
modified radicle-surf/src/error.rs
@@ -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 {
modified radicle-surf/src/lib.rs
@@ -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;
modified radicle-surf/src/repo.rs
@@ -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)
modified radicle-surf/t/src/code_browsing.rs
@@ -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());
+
}