step eleven: write tree objects

This commit is contained in:
Gonçalo Valério 2020-10-13 22:35:19 +01:00
parent cbf02e6bc2
commit c93915acf7
2 changed files with 30 additions and 9 deletions

View File

@ -2,26 +2,47 @@ use std::fs;
#[path = "data.rs"]
mod data;
pub fn write_tree(directory: String) {
let entries = fs::read_dir(&directory).unwrap();
pub fn write_tree(directory: String) -> String {
let mut entries: Vec<(String, String, String)> = vec![];
let mut name;
let mut type_: String;
let mut oid: String;
for entry in entries {
let it = fs::read_dir(&directory).unwrap();
for entry in it {
let item = entry.unwrap();
let metadata = item.metadata().unwrap();
let name = item.file_name();
name = item.file_name();
let full = format!("{}/{}", directory, name.to_str().unwrap());
if is_ignored(&full) {
continue;
}
if metadata.is_file() {
let hash = data::hash_object(&fs::read(&full).unwrap(), "blob".to_owned());
println!("{} {}", hash, full);
type_ = "blob".to_owned();
oid = data::hash_object(&fs::read(&full).unwrap(), type_.clone());
println!("{} {}", oid, full);
} else if metadata.is_dir() {
write_tree(full);
type_ = "tree".to_owned();
oid = write_tree(full);
} else {
panic!("What is this?");
}
entries.push((
name.to_str().unwrap().to_owned().clone(),
oid.clone(),
type_.clone(),
));
}
entries.sort();
let mut tree = String::new();
for entry in entries {
tree.push_str(&format!("{} {} {}\n", entry.2, entry.1, entry.0));
}
return data::hash_object(&tree.into_bytes(), "tree".to_owned());
}
fn is_ignored(path: &String) -> bool {

View File

@ -61,5 +61,5 @@ fn cat_file(matches: ArgMatches) {
}
fn write_tree() {
base::write_tree(".".to_owned())
println!("{}", base::write_tree(".".to_owned()));
}