rgit/src/base.rs

55 lines
1.4 KiB
Rust
Raw Normal View History

2020-10-05 19:49:44 +02:00
use std::fs;
2020-10-05 20:32:48 +02:00
#[path = "data.rs"]
mod data;
2020-10-05 19:49:44 +02:00
2020-10-13 23:35:19 +02:00
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;
2020-10-05 19:49:44 +02:00
2020-10-13 23:35:19 +02:00
let it = fs::read_dir(&directory).unwrap();
for entry in it {
2020-10-05 19:49:44 +02:00
let item = entry.unwrap();
let metadata = item.metadata().unwrap();
2020-10-13 23:35:19 +02:00
name = item.file_name();
2020-10-05 19:49:44 +02:00
let full = format!("{}/{}", directory, name.to_str().unwrap());
2020-10-05 20:04:11 +02:00
if is_ignored(&full) {
continue;
}
2020-10-05 19:49:44 +02:00
if metadata.is_file() {
2020-10-13 23:35:19 +02:00
type_ = "blob".to_owned();
oid = data::hash_object(&fs::read(&full).unwrap(), type_.clone());
println!("{} {}", oid, full);
2020-10-05 19:49:44 +02:00
} else if metadata.is_dir() {
2020-10-13 23:35:19 +02:00
type_ = "tree".to_owned();
oid = write_tree(full);
} else {
panic!("What is this?");
2020-10-05 19:49:44 +02:00
}
2020-10-13 23:35:19 +02:00
entries.push((
name.to_str().unwrap().to_owned().clone(),
oid.clone(),
type_.clone(),
));
2020-10-05 19:49:44 +02:00
}
2020-10-13 23:35:19 +02:00
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());
2020-10-05 19:49:44 +02:00
}
2020-10-05 20:04:11 +02:00
fn is_ignored(path: &String) -> bool {
if path.contains(".rgit") {
true
} else {
false
}
}