dcb0fb74c5
- Add advertise_refs functionality for Git protocol communication - Implement archive service with TAR/ZIP format support and streaming - Create blame service for Git file annotation with line tracking - Add branch management including create, delete, rename and compare operations - Implement merge checking with conflict detection and fast-forward handling - Add cherry-pick functionality for applying commits between branches - Integrate gix library for Git repository operations and object handling - Add comprehensive test suite covering all Git operations - Implement proper error handling and repository validation - Add pagination support for large result sets - Create protobuf definitions for all Git operations and data structures - Add build system for gRPC code generation and dependency management
54 lines
1.4 KiB
Rust
54 lines
1.4 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
/// The null OID representing "no object" in git protocol.
|
|
pub const ZERO_OID: &str = "0000000000000000000000000000000000000000";
|
|
|
|
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
|
|
pub struct ObjectId(pub String);
|
|
|
|
impl ObjectId {
|
|
pub fn new(hex: impl AsRef<str>) -> Self {
|
|
Self(hex.as_ref().to_lowercase())
|
|
}
|
|
pub fn as_str(&self) -> &str {
|
|
&self.0
|
|
}
|
|
}
|
|
impl std::fmt::Display for ObjectId {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{}", self.0)
|
|
}
|
|
}
|
|
|
|
impl AsRef<str> for ObjectId {
|
|
fn as_ref(&self) -> &str {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
pub fn hex_to_bytes(hex: &str) -> Result<Vec<u8>, crate::error::GitError> {
|
|
let hex = hex.trim();
|
|
if !hex.len().is_multiple_of(2) {
|
|
return Err(crate::error::GitError::InvalidOid(
|
|
"hex oid has odd length".into(),
|
|
));
|
|
}
|
|
|
|
(0..hex.len())
|
|
.step_by(2)
|
|
.map(|idx| {
|
|
u8::from_str_radix(&hex[idx..idx + 2], 16)
|
|
.map_err(|e| crate::error::GitError::InvalidOid(e.to_string()))
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
impl TryFrom<&ObjectId> for gix::hash::ObjectId {
|
|
type Error = crate::error::GitError;
|
|
|
|
fn try_from(id: &ObjectId) -> Result<Self, Self::Error> {
|
|
gix::hash::ObjectId::from_hex(id.as_str().as_bytes())
|
|
.map_err(|e| crate::error::GitError::InvalidOid(format!("invalid hex oid: {e}")))
|
|
}
|
|
}
|