git-ext, surf: Use git2::ObjectFormat

This commit is contained in:
Lorenz Leutgeb
2026-06-22 11:31:07 +01:00
committed by Fintan Halpenny
parent 8efd733c20
commit c92d2898a8
4 changed files with 28 additions and 10 deletions
+13 -5
View File
@@ -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<Self, error::Parse> {
let from_str_ext = |s: &str| Oid::from_str_ext(s, format);
fn from_str(buffer: &str) -> Result<Self, Self::Err> {
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, Self::Err> {
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)?;
+6
View File
@@ -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<Self, git2::Error> {
Ok(Self(git2::Oid::from_str_ext(s, format)?))
}
}
#[cfg(feature = "serde")]
mod serde_impls {
use super::*;
+4
View File
@@ -409,6 +409,10 @@ impl Repository {
Ok(contained_branches)
}
pub fn object_format(&self) -> git2::ObjectFormat {
self.inner.object_format()
}
}
////////////////////////////////////////////////////////////
+5 -5
View File
@@ -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, Self::Error> {
Oid::from_str(self)
fn object_id(&self, repo: &Repository) -> Result<Oid, Self::Error> {
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, Self::Error> {
Oid::from_str(self)
fn object_id(&self, repo: &Repository) -> Result<Oid, Self::Error> {
Oid::from_str_ext(self, repo.object_format())
}
}