Radish alpha
r
rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt
Git libraries for Radicle
Radicle
Git
Merge remote-tracking branch 'origin/blob_ref'
Fintan Halpenny committed 2 years ago
commit 71392e70a92aec0a4a45104e2e3de157bda3073a
parent ab791d5
2 files changed +36 -1
modified radicle-surf/src/blob.rs
@@ -95,7 +95,13 @@ impl<'a> Blob<BlobRef<'a>> {

/// Represents a blob with borrowed content bytes.
pub struct BlobRef<'a> {
-
    inner: git2::Blob<'a>,
+
    pub(crate) inner: git2::Blob<'a>,
+
}
+

+
impl<'a> BlobRef<'a> {
+
    pub fn id(&self) -> Oid {
+
        self.inner.id().into()
+
    }
}

impl AsRef<[u8]> for BlobRef<'_> {
@@ -138,3 +144,26 @@ where
        state.end()
    }
}
+

+
#[cfg(feature = "serde")]
+
impl<'a> Serialize for BlobRef<'a> {
+
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+
    where
+
        S: Serializer,
+
    {
+
        const FIELDS: usize = 3;
+
        let mut state = serializer.serialize_struct("BlobRef", FIELDS)?;
+
        state.serialize_field("id", &self.id())?;
+
        state.serialize_field("binary", &self.inner.is_binary())?;
+

+
        let bytes = self.as_ref();
+
        match std::str::from_utf8(bytes) {
+
            Ok(s) => state.serialize_field("content", s)?,
+
            Err(_) => {
+
                let encoded = base64::encode(bytes);
+
                state.serialize_field("content", &encoded)?
+
            },
+
        };
+
        state.end()
+
    }
+
}
modified radicle-surf/src/repo.rs
@@ -301,6 +301,12 @@ impl Repository {
        Ok(Blob::<BlobRef<'a>>::new(file.id(), git2_blob, last_commit))
    }

+
    pub fn blob_ref(&self, oid: Oid) -> Result<BlobRef<'_>, Error> {
+
        Ok(BlobRef {
+
            inner: self.find_blob(oid)?,
+
        })
+
    }
+

    /// Returns the last commit, if exists, for a `path` in the history of
    /// `rev`.
    pub fn last_commit<P, C>(&self, path: &P, rev: C) -> Result<Option<Commit>, Error>