From c92d2898a84b0d35f5a36d6af8c1b51757fd6436 Mon Sep 17 00:00:00 2001 From: Lorenz Leutgeb Date: Sun, 26 Apr 2026 01:39:17 +0200 Subject: [PATCH] git-ext, surf: Use `git2::ObjectFormat` --- radicle-git-ext/src/commit.rs | 18 +++++++++++++----- radicle-git-ext/src/oid.rs | 6 ++++++ radicle-surf/src/repo.rs | 4 ++++ radicle-surf/src/revision.rs | 10 +++++----- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/radicle-git-ext/src/commit.rs b/radicle-git-ext/src/commit.rs index 99b8866..73d26d5 100644 --- a/radicle-git-ext/src/commit.rs +++ b/radicle-git-ext/src/commit.rs @@ -291,10 +291,10 @@ impl TryFrom<&[u8]> for Commit { } } -impl FromStr for Commit { - type Err = error::Parse; +impl Commit { + pub fn from_str_ext(buffer: &str, format: git2::ObjectFormat) -> Result { + let from_str_ext = |s: &str| Oid::from_str_ext(s, format); - fn from_str(buffer: &str) -> Result { let (header, message) = buffer .split_once("\n\n") .ok_or(error::Parse::InvalidFormat)?; @@ -303,7 +303,7 @@ impl FromStr for Commit { let tree = match lines.next() { Some(tree) => tree .strip_prefix("tree ") - .map(git2::Oid::from_str) + .map(from_str_ext) .transpose() .map_err(|err| error::Parse::InvalidHeader { header: "tree", @@ -333,7 +333,7 @@ impl FromStr for Commit { if let Some((name, value)) = line.split_once(' ') { match name { - "parent" => parents.push(git2::Oid::from_str(value).map_err(|err| { + "parent" => parents.push(from_str_ext(value).map_err(|err| { error::Parse::InvalidHeader { header: "parent", err, @@ -368,6 +368,14 @@ impl FromStr for Commit { } } +impl FromStr for Commit { + type Err = error::Parse; + + fn from_str(buffer: &str) -> Result { + Self::from_str_ext(buffer, git2::ObjectFormat::Sha1) + } +} + impl fmt::Display for Commit { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { writeln!(f, "tree {}", self.tree)?; diff --git a/radicle-git-ext/src/oid.rs b/radicle-git-ext/src/oid.rs index 8022746..dceb6e8 100644 --- a/radicle-git-ext/src/oid.rs +++ b/radicle-git-ext/src/oid.rs @@ -9,6 +9,12 @@ use std::{ #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct Oid(git2::Oid); +impl Oid { + pub fn from_str_ext(s: &str, format: git2::ObjectFormat) -> Result { + Ok(Self(git2::Oid::from_str_ext(s, format)?)) + } +} + #[cfg(feature = "serde")] mod serde_impls { use super::*; diff --git a/radicle-surf/src/repo.rs b/radicle-surf/src/repo.rs index b71a7d0..162d248 100644 --- a/radicle-surf/src/repo.rs +++ b/radicle-surf/src/repo.rs @@ -409,6 +409,10 @@ impl Repository { Ok(contained_branches) } + + pub fn object_format(&self) -> git2::ObjectFormat { + self.inner.object_format() + } } //////////////////////////////////////////////////////////// diff --git a/radicle-surf/src/revision.rs b/radicle-surf/src/revision.rs index 099c974..4e34f63 100644 --- a/radicle-surf/src/revision.rs +++ b/radicle-surf/src/revision.rs @@ -1,4 +1,4 @@ -use std::{convert::Infallible, str::FromStr}; +use std::convert::Infallible; use git_ext::{ ref_format::{Qualified, RefString}, @@ -52,8 +52,8 @@ impl Revision for Oid { impl Revision for &str { type Error = git2::Error; - fn object_id(&self, _repo: &Repository) -> Result { - Oid::from_str(self) + fn object_id(&self, repo: &Repository) -> Result { + Oid::from_str_ext(self, repo.object_format()) } } @@ -77,8 +77,8 @@ impl Revision for Tag { impl Revision for String { type Error = git2::Error; - fn object_id(&self, _repo: &Repository) -> Result { - Oid::from_str(self) + fn object_id(&self, repo: &Repository) -> Result { + Oid::from_str_ext(self, repo.object_format()) } }