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

use anyhow::Result;
use object::Object;
Expand All @@ -22,12 +25,35 @@ pub fn init_repo(repo: &Repo, branch_name: &str) -> Result<()> {
Ok(())
}

pub fn hash_object(object: &mut dyn io::Read, stdout: &mut dyn io::Write) -> Result<()> {
pub enum HashObjectMode<'a> {
HashOnly,
Write(&'a Repo),
}

pub fn hash_object(
mode: HashObjectMode,
object: &mut dyn io::Read,
stdout: &mut dyn io::Write,
) -> Result<()> {
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)?;
}

writeln!(stdout, "{hash}")?;
Ok(())
}
Expand Down Expand Up @@ -116,7 +142,12 @@ mod tests {
let mut stdout = Vec::new();

// From https://git-scm.com/book/sv/v2/Git-Internals-Git-Objects
hash_object(&mut "test content\n".as_bytes(), &mut stdout).unwrap();
hash_object(
HashObjectMode::HashOnly,
&mut "test content\n".as_bytes(),
&mut stdout,
)
.unwrap();
assert_eq!(stdout, b"d670460b4b4aece5915caf5c68d12f560a9fe3e4\n");
}
}
18 changes: 16 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ struct InitArgs {

#[derive(Args)]
struct HashObjectArgs {
/// Write the object into the object database.
#[arg(short)]
write: bool,

/// Read the object from stdin instead of from a file.
#[arg(long)]
stdin: bool,
Expand All @@ -65,15 +69,25 @@ fn main() -> Result<()> {
good_git::init_repo(&Repo::new(&init_args.path), &init_args.branch)?;
}
Commands::HashObject(hash_object_args) => {
let repo = Repo::from_dir(Path::new("."));
let mode = if hash_object_args.write {
good_git::HashObjectMode::Write(
repo.as_ref()
.ok_or_else(|| anyhow!("Could not find a valid git repository"))?,
)
} else {
good_git::HashObjectMode::HashOnly
};

if hash_object_args.stdin {
hash_object(&mut io::stdin(), &mut io::stdout())?;
hash_object(mode, &mut io::stdin(), &mut io::stdout())?;
} else {
let f = hash_object_args
.file
.clone()
.expect("<file> is required when --stdin isn't set");
let f = fs::File::open(f)?;
hash_object(&mut io::BufReader::new(f), &mut io::stdout())?;
hash_object(mode, &mut io::BufReader::new(f), &mut io::stdout())?;
}
}
Commands::CatFile(cat_file_args) => {
Expand Down
4 changes: 2 additions & 2 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ impl Blob {
Blob { content }
}

pub fn hash(self) -> String {
pub fn hash(&self) -> String {
let size = self.content.len();
let data = format!("blob {size}\0");
let mut data = data.as_bytes().to_vec();
data.extend(self.content);
data.extend(self.content.as_slice());

hash(&data)
}
Expand Down
22 changes: 22 additions & 0 deletions tests/git_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,26 @@ aaaaaa - This is a good commit - \"Alice <bye@alice.test>\"
",
);
}

#[rstest]
fn test_hash_object_w(test_repo: tempfile::TempDir) {
// From https://git-scm.com/book/sv/v2/Git-Internals-Git-Objects
let repo = Repo::new(test_repo.path());
let mut stdout = Vec::new();

good_git::hash_object(
good_git::HashObjectMode::Write(&repo),
&mut "test content\n".as_bytes(),
&mut stdout,
)
.unwrap();

const EXPECTED_HASH: &str = "d670460b4b4aece5915caf5c68d12f560a9fe3e4\n";
assert_eq!(stdout, EXPECTED_HASH.as_bytes());
stdout.clear();

good_git::cat_file(&repo, EXPECTED_HASH.trim(), &mut stdout).unwrap();

assert_eq!(stdout, b"test content\n\n");
}
}