Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 3 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,9 @@ pub fn log(repo: &Repo, object_rev: &str, stdout: &mut dyn io::Write) -> Result<
Object::Commit(commit) => {
let commiter = commit.committer;
let first_line = commit.message.lines().next().unwrap_or("");
writeln!(
stdout,
"{hash} - {first_line} - \"{commiter}\"",
hash = &this_rev[0..6]
)?;
// TODO: resolve all refs to their full hash.
// Now this_rev could be for example a branch name.
writeln!(stdout, "{this_rev:.6} - {first_line} - \"{commiter}\"",)?;
if commit.parent.is_empty() {
return Ok(());
} else {
Expand Down
31 changes: 31 additions & 0 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ impl Object {
}

pub fn from_file(path: &std::path::Path) -> Result<Object> {
if !path.exists() {
return Err(anyhow!("Expected file: {:?} does not exist", path));
}
let data = std::fs::read(path).context("Could not read from file")?;
let mut z = ZlibDecoder::new(&data[..]);
let mut s: Vec<u8> = vec![];
Expand Down Expand Up @@ -224,6 +227,10 @@ impl Object {
pub fn from_rev(repo: &Repo, rev: &str) -> Result<Object> {
let mut candidates: Vec<String> = vec![];

if rev == "HEAD" {
return Object::from_hash(repo, &refs::find_ref("HEAD", repo)?);
}

// Check if this is a hash
if rev.len() >= 4 {
let (short_hash, long_hash) = rev.split_at(2);
Expand Down Expand Up @@ -286,6 +293,8 @@ pub fn hash(s: &[u8]) -> String {

#[cfg(test)]
mod tests {
use tempfile::tempdir;

use crate::object::File;

use super::Blob;
Expand Down Expand Up @@ -411,6 +420,28 @@ parent";
assert_eq!(err, "Incorrect header length");
}

#[test]
fn test_from_file_nonexisting_file() {
let result = Object::from_file(std::path::Path::new("/nonexisting/file"));
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
"Expected file: \"/nonexisting/file\" does not exist"
);
}

#[test]
fn test_from_file_with_invalid_data() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("invalid_object");

std::fs::write(&file_path, "not a valid git object").unwrap();

let result = Object::from_file(&file_path);
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "corrupt deflate stream");
}

#[test]
fn test_blob_hash_is_correct() {
// From https://git-scm.com/book/sv/v2/Git-Internals-Git-Objects
Expand Down
18 changes: 3 additions & 15 deletions tests/git_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,12 @@ mod tests {
use super::*;
use rstest::rstest;

#[rstest]
fn test_cat_file(test_repo: tempfile::TempDir) {
let repo = Repo::new(test_repo.path());
let mut stdout = Vec::new();

good_git::cat_file(
&repo,
"d670460b4b4aece5915caf5c68d12f560a9fe3e4",
&mut stdout,
)
.unwrap();
assert_eq!(stdout, b"test content\n\n");
}

#[rstest]
#[case("d670", b"test content\n\n".to_vec())]
#[case("d67046", b"test content\n\n".to_vec())]
#[case("d670460b4b4aece5915caf5c68d12f560a9fe3e4", b"test content\n\n".to_vec())]
#[case("1234567890", b"more content\nfrom a good client\n".to_vec())]
fn test_cat_file_short_hash(
fn test_cat_file(
test_repo: tempfile::TempDir,
#[case] input: String,
#[case] expected: Vec<u8>,
Expand Down Expand Up @@ -246,6 +233,7 @@ from a good client
#[rstest]
#[case("aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbb")]
#[case("main")] // Points to the same commit
#[case("HEAD")]
fn test_cat_file_commit(test_repo: tempfile::TempDir, #[case] reference: String) {
let repo = Repo::new(test_repo.path());
let mut stdout = Vec::new();
Expand Down