Skip to content

Commit 7d262f7

Browse files
committed
remove hex encoding of git object IDs
1 parent b436fff commit 7d262f7

File tree

4 files changed

+19
-27
lines changed

4 files changed

+19
-27
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/uv-git/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ uv-fs = { workspace = true }
1919
anyhow = { workspace = true }
2020
thiserror = { workspace = true }
2121
cargo-util = { workspace = true }
22-
hex = { workspace = true }
2322
reqwest = { workspace = true, features = ["blocking"] }
2423
tokio = { workspace = true }
2524
tracing = { workspace = true }

crates/uv-git/src/git.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ impl GitRepository {
185185
.cwd(&self.path)
186186
.exec_with_streaming(&mut silent, &mut silent, true)?;
187187

188-
Ok(GitOid::from_hex(&result.stdout)?)
188+
let mut result = String::from_utf8(result.stdout)?;
189+
result.truncate(result.trim_end().len());
190+
Ok(result.parse()?)
189191
}
190192
}
191193

crates/uv-git/src/sha.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,40 +44,18 @@ impl FromStr for GitSha {
4444
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4545
pub struct GitOid {
4646
len: usize,
47-
bytes: [u8; 20],
47+
bytes: [u8; 40],
4848
}
4949

5050
impl GitOid {
5151
/// Return the string representation of an object ID.
5252
pub(crate) fn as_str(&self) -> &str {
5353
str::from_utf8(&self.bytes[..self.len]).unwrap()
5454
}
55-
56-
/// Parse an object ID from a hex string.
57-
pub(crate) fn from_hex(s: &[u8]) -> Result<Self, OidParseError> {
58-
if s.is_empty() {
59-
return Err(OidParseError::Empty);
60-
}
61-
62-
if s.len() > 40 {
63-
return Err(OidParseError::TooLong);
64-
}
65-
66-
// TODO: support odd length strings
67-
let mut out = [0; 20];
68-
hex::decode_to_slice(s, &mut out[..s.len() / 2])?;
69-
70-
Ok(GitOid {
71-
len: s.len(),
72-
bytes: out,
73-
})
74-
}
7555
}
7656

7757
#[derive(Debug, Error, PartialEq)]
7858
pub enum OidParseError {
79-
#[error("Failed to parse hex object ID: `{0}`")]
80-
HexError(#[from] hex::FromHexError),
8159
#[error("Object ID can be at most 40 hex characters")]
8260
TooLong,
8361
#[error("Object ID cannot be parsed from empty string")]
@@ -88,7 +66,21 @@ impl FromStr for GitOid {
8866
type Err = OidParseError;
8967

9068
fn from_str(s: &str) -> Result<Self, Self::Err> {
91-
GitOid::from_hex(s.as_bytes())
69+
if s.is_empty() {
70+
return Err(OidParseError::Empty);
71+
}
72+
73+
if s.len() > 40 {
74+
return Err(OidParseError::TooLong);
75+
}
76+
77+
let mut out = [0; 40];
78+
out[..s.len()].copy_from_slice(s.as_bytes());
79+
80+
Ok(GitOid {
81+
len: s.len(),
82+
bytes: out,
83+
})
9284
}
9385
}
9486

0 commit comments

Comments
 (0)