Skip to content

odb: fix OdbReader read return value #1061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/odb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ impl<'repo> io::Read for OdbReader<'repo> {
if res < 0 {
Err(io::Error::new(io::ErrorKind::Other, "Read error"))
} else {
Ok(len)
Ok(res as _)
}
}
}
Expand Down Expand Up @@ -726,4 +726,45 @@ mod tests {
t!(repo.reset(commit1.as_object(), ResetType::Hard, None));
assert!(foo_file.exists());
}

#[test]
fn stream_read() {
// Test for read impl of OdbReader.
const FOO_TEXT: &[u8] = b"this is a test";
let (_td, repo) = crate::test::repo_init();
let p = repo.path().parent().unwrap().join("foo");
std::fs::write(&p, FOO_TEXT).unwrap();
let mut index = repo.index().unwrap();
index.add_path(std::path::Path::new("foo")).unwrap();
let tree_id = index.write_tree().unwrap();
let tree = repo.find_tree(tree_id).unwrap();
let sig = repo.signature().unwrap();
let head_id = repo.refname_to_id("HEAD").unwrap();
let parent = repo.find_commit(head_id).unwrap();
let _commit = repo
.commit(Some("HEAD"), &sig, &sig, "commit", &tree, &[&parent])
.unwrap();

// Try reading from a commit object.
let odb = repo.odb().unwrap();
let oid = repo.refname_to_id("HEAD").unwrap();
let (mut reader, size, ty) = odb.reader(oid).unwrap();
assert!(ty == ObjectType::Commit);
let mut x = [0; 10000];
let r = reader.read(&mut x).unwrap();
assert!(r == size);

// Try reading from a blob. This assumes it is a loose object (packed
// objects can't read).
let commit = repo.find_commit(oid).unwrap();
let tree = commit.tree().unwrap();
let entry = tree.get_name("foo").unwrap();
let (mut reader, size, ty) = odb.reader(entry.id()).unwrap();
assert_eq!(size, FOO_TEXT.len());
assert!(ty == ObjectType::Blob);
let mut x = [0; 10000];
let r = reader.read(&mut x).unwrap();
assert_eq!(r, 14);
assert_eq!(&x[..FOO_TEXT.len()], FOO_TEXT);
}
}