uv/crates/uv-git/src/sha.rs
Charlie Marsh 0d512be46c
Support lossless serialization for Git dependencies in lockfile (#3630)
## Summary

This PR adds lossless deserialization for `GitSourceDist` distributions
in the lockfile. Specifically, we now properly preserve the requested
revision, the subdirectory, and the precise Git commit SHA.

## Test Plan

`cargo test`
2024-05-17 21:23:40 +00:00

38 lines
940 B
Rust

use std::str::FromStr;
/// A complete Git SHA, i.e., a 40-character hexadecimal representation of a Git commit.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GitSha(git2::Oid);
impl GitSha {
/// Convert the SHA to a truncated representation, i.e., the first 16 characters of the SHA.
pub fn to_short_string(&self) -> String {
self.0.to_string()[0..16].to_string()
}
}
impl From<GitSha> for git2::Oid {
fn from(value: GitSha) -> Self {
value.0
}
}
impl From<git2::Oid> for GitSha {
fn from(value: git2::Oid) -> Self {
Self(value)
}
}
impl std::fmt::Display for GitSha {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromStr for GitSha {
type Err = git2::Error;
fn from_str(value: &str) -> Result<Self, Self::Err> {
Ok(Self(git2::Oid::from_str(value)?))
}
}