Files
uv/crates/puffin-git/src/sha.rs
T
Charlie Marsh 5ae3a8b1cb Restructure Git cache to include package name (#588)
## Summary

This PR modifies the Git wheel cache to: (1) use a shorter version of
the SHA, to save space; and (2) include the package name, for
consistency with all other buckets.

I considered removing the URL hash entirely, and _just_ using the SHA,
which would be even _more_ consistent with other buckets. But if we
remove the URL, then we won't have separate directories for
subdirectories (which are part of the URL).

Before:

<img width="1035" alt="Screen Shot 2023-12-07 at 7 23 42 PM"
src="https://github.com/astral-sh/puffin/assets/1309177/86afce67-682f-464f-9ba1-0b60d5b7f19f">

After:

<img width="1232" alt="Screen Shot 2023-12-07 at 8 09 23 PM"
src="https://github.com/astral-sh/puffin/assets/1309177/eda42a19-974f-47fe-8c83-54a602ddfd2d">
2023-12-07 20:17:41 -05:00

39 lines
902 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)]
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)?))
}
}