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
24 changes: 5 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
fs,
io::{self, Write},
};
use std::{fs, io};

use anyhow::Result;
use object::Object;
Expand Down Expand Up @@ -39,21 +36,10 @@ pub fn hash_object(
let mut data = Vec::new();
object.read_to_end(&mut data)?;
let blob = object::Blob::new(data);
let hash = blob.hash();

if let HashObjectMode::Write(repo) = mode {
let dir = &repo.git_dir().join("objects").join(&hash[0..2]);
let file_path = dir.join(&hash[2..]);
let mut data = Vec::new();
let mut writer = flate2::write::ZlibEncoder::new(&mut data, flate2::Compression::default());
writer.write_all(b"blob ")?;
writer.write_all(blob.content.len().to_string().as_bytes())?;
writer.write_all(b"\0")?;
writer.write_all(&blob.content)?;
drop(writer);
fs::create_dir_all(dir)?;
fs::write(file_path, data)?;
}
let hash = match mode {
HashObjectMode::HashOnly => blob.hash(),
HashObjectMode::Write(repo) => object::write_object(repo, "blob", &blob.content)?,
};

writeln!(stdout, "{hash}")?;
Ok(())
Expand Down
41 changes: 41 additions & 0 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,25 @@ pub fn hash(s: &[u8]) -> String {
hex::encode(hasher.finalize())
}

/// Writes an object to the git repository.
pub fn write_object(repo: &Repo, object_type: &str, content: &[u8]) -> Result<String> {
let mut data = format!("{object_type} {}\0", content.len()).into_bytes();
data.extend(content);
let hash = hash(&data);

let dir = repo.git_dir().join("objects").join(&hash[0..2]);
let file_path = dir.join(&hash[2..]);
if file_path.exists() {
return Ok(hash);
}

let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
encoder.write_all(&data)?;
fs::create_dir_all(&dir)?;
fs::write(file_path, encoder.finish()?)?;
Ok(hash)
}

#[cfg(test)]
mod tests {
use tempfile::tempdir;
Expand All @@ -300,6 +319,8 @@ mod tests {
use super::Mode;
use super::Object;
use super::hash;
use super::write_object;
use crate::repo::Repo;
#[test]
fn test_object_parse_header() {
assert_eq!(
Expand Down Expand Up @@ -454,4 +475,24 @@ parent";
let s = b"blob 16\0what is up, doc?";
assert_eq!(hash(s), "bd9dbf5aae1a3862dd1526723246b20206e5fc37");
}

#[test]
fn test_write_object() {
let dir = tempdir().unwrap();
let repo = Repo::new(dir.path());

let hash = write_object(&repo, "blob", b"what is up, doc?").unwrap();
assert_eq!(hash, "bd9dbf5aae1a3862dd1526723246b20206e5fc37");

// Writing the same object should not do anything.
assert_eq!(
write_object(&repo, "blob", b"what is up, doc?").unwrap(),
hash
);

let Object::Blob(blob) = Object::from_hash(&repo, &hash).unwrap() else {
panic!("Expected a Blob");
};
assert_eq!(blob.content, b"what is up, doc?");
}
}
Loading