Skip to content

Commit b480c4f

Browse files
committed
[ci] Fix CI issues for multi-hash support
Signed-off-by: jackieismpc <[email protected]>
1 parent ecf0156 commit b480c4f

File tree

11 files changed

+59
-55
lines changed

11 files changed

+59
-55
lines changed

BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,6 @@ rust_library(
7373
"//third-party/rust/crates/tracing/0.1.41:tracing",
7474
"//third-party/rust/crates/uuid/1.18.1:uuid",
7575
"//third-party/rust/crates/zstd-sys/2.0.16+zstd.1.5.7:zstd-sys",
76+
"//third-party/rust/crates/sha2/0.10.9:sha2",
7677
],
7778
)

src/internal/index.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ use std::path::{Path, PathBuf};
99
use std::time::{SystemTime, UNIX_EPOCH};
1010

1111
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
12-
use sha1::{Digest, Sha1};
1312

1413
use crate::errors::GitError;
1514
use crate::hash::{ObjectHash, get_hash_kind};
1615
use crate::internal::pack::wrapper::Wrapper;
17-
use crate::utils::{self, Hashalgorithm};
16+
use crate::utils::{self, HashAlgorithm};
1817

1918
#[derive(PartialEq, Eq, Debug, Clone)]
2019
pub struct Time {
@@ -281,15 +280,16 @@ impl Index {
281280
.insert((entry.name.clone(), entry.flags.stage), entry);
282281

283282
// 1-8 nul bytes as necessary to pad the entry to a multiple of eight bytes
284-
// while keeping the name NUL-terminated. // so at least 1 byte nul
283+
// while keeping the name NUL-terminated.
285284
let hash_len = get_hash_kind().size();
286-
let padding = (8 - ((hash_len + 2 + name_len) % 8)) % 8; // 2 for flags
285+
let entry_len = hash_len + 2 + name_len;
286+
let padding = 1 + ((8 - ((entry_len + 1) % 8)) % 8); // at least 1 byte nul
287287
utils::read_bytes(file, padding)?;
288288
}
289289

290290
// Extensions
291291
while file.bytes_read() + get_hash_kind().size() < total_size as usize {
292-
// The remaining 20 bytes must be checksum
292+
// The remaining bytes must be the pack checksum (size = get_hash_kind().size())
293293
let sign = utils::read_bytes(file, 4)?;
294294
println!(
295295
"{:?}",
@@ -321,7 +321,7 @@ impl Index {
321321

322322
pub fn to_file(&self, path: impl AsRef<Path>) -> Result<(), GitError> {
323323
let mut file = File::create(path)?;
324-
let mut hash = Sha1::new();
324+
let mut hash = HashAlgorithm::new();
325325

326326
let mut header = Vec::new();
327327
header.write_all(b"DIRC")?;
@@ -342,21 +342,23 @@ impl Index {
342342
entry_bytes.write_u32::<BigEndian>(entry.uid)?;
343343
entry_bytes.write_u32::<BigEndian>(entry.gid)?;
344344
entry_bytes.write_u32::<BigEndian>(entry.size)?;
345-
entry_bytes.write_all(&entry.hash.as_ref())?;
345+
entry_bytes.write_all(entry.hash.as_ref())?;
346346
entry_bytes.write_u16::<BigEndian>((&entry.flags).try_into().unwrap())?;
347347
entry_bytes.write_all(entry.name.as_bytes())?;
348-
let padding = 8 - ((22 + entry.name.len()) % 8);
348+
let hash_len = get_hash_kind().size();
349+
let entry_len = hash_len + 2 + entry.name.len();
350+
let padding = 1 + ((8 - ((entry_len + 1) % 8)) % 8); // at least 1 byte nul
349351
entry_bytes.write_all(&vec![0; padding])?;
350-
351352
file.write_all(&entry_bytes)?;
352353
hash.update(&entry_bytes);
353354
}
354355

355356
// Extensions
356357

357358
// check sum
358-
let file_hash: [u8; 20] = hash.finalize().into();
359-
file.write_all(&file_hash)?;
359+
let file_hash =
360+
ObjectHash::from_bytes(&hash.finalize()).map_err(GitError::InvalidIndexFile)?;
361+
file.write_all(file_hash.as_ref())?;
360362
Ok(())
361363
}
362364

@@ -384,7 +386,7 @@ impl Index {
384386

385387
// re-calculate SHA1/SHA256
386388
let mut file = File::open(&abs_path)?;
387-
let mut hasher = Hashalgorithm::new();
389+
let mut hasher = HashAlgorithm::new();
388390
io::copy(&mut file, &mut hasher)?;
389391
let new_hash = ObjectHash::from_bytes(&hasher.finalize()).unwrap();
390392

src/internal/object/commit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ signed sha256 commit for test"#;
360360
assert_eq!(commit.author.name, "jackieismpc");
361361
assert_eq!(commit.author.email, "[email protected]");
362362
assert_eq!(commit.committer.name, "jackieismpc");
363-
// check message contentmust contains gpgsig-sha256 and content
363+
// // check message content (must contain gpgsig-sha256 and content)
364364
assert!(commit.message.contains("-----BEGIN PGP SIGNATURE-----"));
365365
assert!(commit.message.contains("-----END PGP SIGNATURE-----"));
366366
assert!(commit.message.contains("signed sha256 commit for test"));

src/internal/object/tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ mod tests {
413413

414414
let bytes = tree_item.to_data();
415415

416-
// 一个小 helper:把十六进制字符串转成字节序列
416+
// a small helper: convert hex string to byte sequence
417417
fn hex_to_bytes(s: &str) -> Vec<u8> {
418418
assert!(s.len() % 2 == 0);
419419
let mut out = Vec::with_capacity(s.len() / 2);
@@ -424,7 +424,7 @@ mod tests {
424424
out
425425
}
426426

427-
// 期望的编码:`"100644 hello-world\0" + <32字节的blob哈希>`
427+
// expected encoding: `"100644 hello-world\0" + <32-byte blob hash>`
428428
let mut expected = b"100644 hello-world\0".to_vec();
429429
expected.extend_from_slice(&hex_to_bytes(
430430
"2cf8d83d9ee29543b34a87727421fdecb7e3f3a183d337639025de576db9ebb4",

src/internal/pack/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl Caches {
8989

9090
/// generate the temp file path, hex string of the hash
9191
fn generate_temp_path(&self, tmp_path: &Path, hash: ObjectHash) -> PathBuf {
92-
// This is enough for the original path, 2 chars directory, 40/64 chars hash, and extra slashes
92+
// Reserve capacity for base path, 2-char subdir, hex hash string, and separators
9393
let mut path =
9494
PathBuf::with_capacity(self.tmp_path.capacity() + hash.to_string().len() + 5);
9595
path.push(tmp_path);

src/internal/pack/cache_object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,11 @@ mod test {
478478
);
479479
assert!(r.is_err());
480480
if let Err(lru_mem::TryInsertError::WouldEjectLru { .. }) = r {
481-
// 匹配到指定错误,不需要额外操作
481+
// match the specified error, no extra action needed
482482
} else {
483483
panic!("Expected WouldEjectLru error");
484484
}
485-
// 使用不同的键插入b,这样a会被驱逐
485+
// insert b with a different key, so a will be ejected
486486
let r = cache.insert(
487487
hash_b.to_string(),
488488
ArcWrapper::new(Arc::new(b.clone()), Arc::new(AtomicBool::new(true)), None),

src/internal/pack/decode.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ impl Pack {
309309
})
310310
}
311311
ObjectType::HashDelta => {
312-
// Read 20/32 bytes to get the reference object SHA1/SHA256 hash
313-
let ref_sha1 = ObjectHash::from_stream(pack).unwrap();
312+
// Read hash bytes to get the reference object hash(size depends on hash kind,e.g.,20 for SHA1,32 for SHA256)
313+
let ref_sha = ObjectHash::from_stream(pack).unwrap();
314314
// Offset is incremented by 20/32 bytes
315315
*offset += get_hash_kind().size();
316316

@@ -321,7 +321,7 @@ impl Pack {
321321
let (_, final_size) = utils::read_delta_object_size(&mut reader)?;
322322

323323
Ok(CacheObject {
324-
info: CacheObjectInfo::HashDelta(ref_sha1, final_size),
324+
info: CacheObjectInfo::HashDelta(ref_sha, final_size),
325325
offset: init_offset,
326326
data_decompressed: data,
327327
mem_recorder: None,
@@ -881,7 +881,7 @@ mod tests {
881881
let mut buffered = BufReader::new(f);
882882
let mut p = Pack::new(
883883
Some(20),
884-
Some(1024 * 1024 * 1024 * 2),
884+
Some(1024 * 1024 * 1024 * 1), //try to avoid dead lock on CI servers with low memory
885885
Some(tmp.clone()),
886886
true,
887887
);
@@ -933,7 +933,7 @@ mod tests {
933933
let stream = ReaderStream::new(f).map_err(axum::Error::new);
934934
let p = Pack::new(
935935
Some(20),
936-
Some(1024 * 1024 * 1024 * 4),
936+
Some(1024 * 1024 * 1024 * 1),
937937
Some(tmp.clone()),
938938
true,
939939
);
@@ -997,7 +997,7 @@ mod tests {
997997
let buffered = BufReader::new(f);
998998
let p = Pack::new(
999999
Some(20),
1000-
Some(1024 * 1024 * 1024 * 2),
1000+
Some(1024 * 1024 * 1024 * 1),
10011001
Some(tmp.clone()),
10021002
true,
10031003
);

src/internal/pack/encode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::internal::object::types::ObjectType;
88
use crate::time_it;
99
use crate::zstdelta;
1010
use crate::{
11-
errors::GitError, hash::ObjectHash, internal::pack::entry::Entry, utils::Hashalgorithm,
11+
errors::GitError, hash::ObjectHash, internal::pack::entry::Entry, utils::HashAlgorithm,
1212
};
1313
use ahash::AHasher;
1414
use flate2::write::ZlibEncoder;
@@ -32,7 +32,7 @@ pub struct PackEncoder {
3232
// window: VecDeque<(Entry, usize)>, // entry and offset
3333
sender: Option<mpsc::Sender<Vec<u8>>>,
3434
inner_offset: usize, // offset of current entry
35-
inner_hash: Hashalgorithm, // introduce different hash algorithm
35+
inner_hash: HashAlgorithm, // introduce different hash algorithm
3636
final_hash: Option<ObjectHash>,
3737
start_encoding: bool,
3838
}
@@ -186,8 +186,8 @@ impl PackEncoder {
186186
process_index: 0,
187187
// window: VecDeque::with_capacity(window_size),
188188
sender: Some(sender),
189-
inner_offset: 12, // 12 bytes header
190-
inner_hash: Hashalgorithm::new(), // introduce different hash algorithm
189+
inner_offset: 12, // start after 12 bytes pack header(signature + version + object count).
190+
inner_hash: HashAlgorithm::new(), // introduce different hash algorithm
191191
final_hash: None,
192192
start_encoding: false,
193193
}

src/internal/pack/wrapper.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::{self, BufRead, Read};
33
use sha1::{Digest, Sha1};
44

55
use crate::hash::{HashKind, ObjectHash, get_hash_kind};
6-
6+
use crate::utils::HashAlgorithm;
77
/// [`Wrapper`] is a wrapper around a reader that also computes the SHA1/ SHA256 hash of the data read.
88
///
99
/// It is designed to work with any reader that implements `BufRead`.
@@ -13,15 +13,9 @@ use crate::hash::{HashKind, ObjectHash, get_hash_kind};
1313
/// * `hash`: The hash state.
1414
/// * `count_hash`: A flag to indicate whether to compute the hash while reading.
1515
///
16-
/// we abstract the hasher to support both SHA1 and SHA256 Now.
17-
#[derive(Clone)]
18-
enum Hasher {
19-
Sha1(Sha1),
20-
Sha256(sha2::Sha256),
21-
}
2216
pub struct Wrapper<R> {
2317
inner: R,
24-
hash: Hasher,
18+
hash: HashAlgorithm,
2519
bytes_read: usize,
2620
}
2721

@@ -38,8 +32,8 @@ where
3832
Self {
3933
inner,
4034
hash: match get_hash_kind() {
41-
HashKind::Sha1 => Hasher::Sha1(Sha1::new()),
42-
HashKind::Sha256 => Hasher::Sha256(sha2::Sha256::new()),
35+
HashKind::Sha1 => HashAlgorithm::Sha1(Sha1::new()),
36+
HashKind::Sha256 => HashAlgorithm::Sha256(sha2::Sha256::new()),
4337
}, // Initialize a new SHA1/ SHA256 hasher
4438
bytes_read: 0,
4539
}
@@ -54,11 +48,11 @@ where
5448
/// This is a clone of the internal hash state finalized into a SHA1/ SHA256 hash.
5549
pub fn final_hash(&self) -> ObjectHash {
5650
match &self.hash.clone() {
57-
Hasher::Sha1(hasher) => {
51+
HashAlgorithm::Sha1(hasher) => {
5852
let re: [u8; 20] = hasher.clone().finalize().into(); // Clone, finalize, and convert the hash into bytes
5953
ObjectHash::from_bytes(&re).unwrap()
6054
}
61-
Hasher::Sha256(hasher) => {
55+
HashAlgorithm::Sha256(hasher) => {
6256
let re: [u8; 32] = hasher.clone().finalize().into(); // Clone, finalize, and convert the hash into bytes
6357
ObjectHash::from_bytes(&re).unwrap()
6458
}
@@ -82,8 +76,8 @@ where
8276
fn consume(&mut self, amt: usize) {
8377
let buffer = self.inner.fill_buf().expect("Failed to fill buffer");
8478
match &mut self.hash {
85-
Hasher::Sha1(hasher) => hasher.update(&buffer[..amt]), // Update SHA1 hash with the data being consumed
86-
Hasher::Sha256(hasher) => hasher.update(&buffer[..amt]), // Update SHA256 hash with the data being consumed
79+
HashAlgorithm::Sha1(hasher) => hasher.update(&buffer[..amt]), // Update SHA1 hash with the data being consumed
80+
HashAlgorithm::Sha256(hasher) => hasher.update(&buffer[..amt]), // Update SHA256 hash with the data being consumed
8781
}
8882
self.inner.consume(amt); // Consume the data from the inner reader
8983
self.bytes_read += amt;
@@ -105,8 +99,8 @@ where
10599
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
106100
let o = self.inner.read(buf)?; // Read data into the buffer
107101
match &mut self.hash {
108-
Hasher::Sha1(hasher) => hasher.update(&buf[..o]), // Update SHA1 hash with the data being read
109-
Hasher::Sha256(hasher) => hasher.update(&buf[..o]), // Update SHA256 hash with the data being read
102+
HashAlgorithm::Sha1(hasher) => hasher.update(&buf[..o]), // Update SHA1 hash with the data being read
103+
HashAlgorithm::Sha256(hasher) => hasher.update(&buf[..o]), // Update SHA256 hash with the data being read
110104
}
111105
self.bytes_read += o;
112106
Ok(o) // Return the number of bytes read

src/protocol/smart.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ where
298298
for command in &mut self.command_list {
299299
if command.ref_type == RefTypeEnum::Tag {
300300
// Just update if refs type is tag
301-
// Convert ze to None for old hash
301+
// Convert zero_id to None for old hash
302302
let old_hash = if command.old_hash == self.zero_id {
303303
None
304304
} else {

0 commit comments

Comments
 (0)