remove objects

This commit is contained in:
grimhilt 2023-06-29 23:52:19 +02:00
parent f812ad411e
commit 45f8d486d8
2 changed files with 48 additions and 2 deletions

View File

@ -45,6 +45,15 @@ fn _object_path(obj: &str) -> PathBuf {
root
}
fn rm(hash: &str) -> io::Result<()> {
let mut root = path::objects();
let (dir, rest) = hash.split_at(2);
root.push(dir);
root.push(rest);
fs::remove_file(root)?;
Ok(())
}
fn rm_node(path: &Path, node: &str) -> io::Result<()> {
let mut root = path::objects();
let (dir, rest) = hash_obj(path.clone().to_str().unwrap());

View File

@ -3,7 +3,7 @@ use std::io::{self};
use std::path::Path;
use crate::utils::{read, path};
use crate::store::head;
use crate::store::object::{parse_path, hash_obj, add_node, create_obj};
use crate::store::object::{self, parse_path, hash_obj, add_node, create_obj};
pub fn add(path: &Path, date: &str) -> io::Result<()> {
let (line, hash, name) = parse_path(path.clone(), false);
@ -24,6 +24,43 @@ pub fn add(path: &Path, date: &str) -> io::Result<()> {
Ok(())
}
pub fn rm(path: &Path) -> io::Result<()> {
let (_, lines) = read(path.to_path_buf().to_str().unwrap().to_string()).unwrap();
for line in lines {
let (ftype, hash, _) = parse_line(line.unwrap());
if ftype == String::from("blob") {
object::rm(&hash);
} else {
rm_hash(hash);
}
}
Ok(())
}
fn rm_hash(hash: String) {
let mut obj_p = path::objects();
let (dir, res) = hash.split_at(2);
obj_p.push(dir);
obj_p.push(res);
match read::read_lines(obj_p) {
Ok(mut reader) => {
reader.next();
for line in reader {
let (ftype, hash, _) = parse_line(line.unwrap());
if ftype == String::from("blob") {
object::rm(&hash);
} else {
rm_hash(hash);
}
}
},
Err(err) => {
eprintln!("error reading tree: {}", err);
},
}
}
pub fn read(tree: String) -> Option<(String, io::Lines<io::BufReader<File>>)> {
let mut obj_p = path::objects();
@ -57,4 +94,4 @@ pub fn parse_line(line: String) -> (String, String, String) {
let hash = split.next().unwrap();
let ftype = split.next().unwrap();
(String::from(ftype), String::from(hash), String::from(name))
}
}