mirror of
https://seed.radicle.dev/rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt.git
synced 2026-10-18 19:10:31 +08:00
git-ext, surf: Use git2::ObjectFormat
This commit is contained in:
committed by
Fintan Halpenny
parent
8efd733c20
commit
c92d2898a8
@@ -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)?;
|
||||
|
||||
@@ -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::*;
|
||||
|
||||
@@ -409,6 +409,10 @@ impl Repository {
|
||||
|
||||
Ok(contained_branches)
|
||||
}
|
||||
|
||||
pub fn object_format(&self) -> git2::ObjectFormat {
|
||||
self.inner.object_format()
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user