add remove line

This commit is contained in:
grimhilt 2023-06-15 17:32:57 +02:00
parent b00266a93e
commit 35adfab983
4 changed files with 95 additions and 7 deletions

View File

@ -43,3 +43,15 @@ pub fn add_line(line: String) -> io::Result<()> {
writeln!(file, "{}", line)?;
Ok(())
}
pub fn rm_line(line: &str) -> io::Result<()> {
let mut root = match path::nextsync_root() {
Some(path) => path,
None => todo!(),
};
root.push(".nextsync");
root.push("HEAD");
read::rm_line(root, line)?;
Ok(())
}

View File

@ -1,7 +1,7 @@
use std::fs::OpenOptions;
use std::fs::File;
use std::path::PathBuf;
use crate::utils::read;
use crate::utils::{read, path};
use std::io;
pub fn _read_only(mut path: PathBuf) -> File {
@ -25,3 +25,15 @@ pub fn read_line(mut path: PathBuf) -> io::Result<io::Lines<io::BufReader<File>>
path.push("index");
read::read_lines(path)
}
pub fn rm_line(line: &str) -> io::Result<()> {
let mut root = match path::nextsync_root() {
Some(path) => path,
None => todo!(),
};
root.push(".nextsync");
root.push("index");
read::rm_line(root, line)?;
Ok(())
}

View File

@ -19,7 +19,7 @@ fn parse_path(path: &Path, is_blob: bool) -> (String, String, String) {
hasher.input_str(path.clone().to_str().unwrap());
let hash = hasher.result_str();
let mut line = String::from(if is_blob { "tree" } else { "blob" });
let mut line = String::from(if is_blob { "blob" } else { "tree" });
line.push_str(" ");
line.push_str(&hash);
line.push_str(" ");
@ -56,10 +56,36 @@ pub fn add_tree(path: &Path) -> io::Result<()> {
Ok(())
}
pub fn rm_blob(path: &Path) -> io::Result<()> {
let (line, hash, name) = parse_path(path.clone(), true);
// remove blob reference to parent
if path.iter().count() == 1 {
head::rm_line(&line)?;
} else {
rm_node(path.parent().unwrap(), &line)?;
}
// remove blob object
let mut root = match path::objects() {
Some(path) => path,
None => todo!(),
};
let c = hash.clone();
let (dir, rest) = c.split_at(2);
root.push(dir);
root.push(rest);
fs::remove_file(root)?;
Ok(())
}
pub fn add_blob(path: &Path, date: &str) -> io::Result<()> {
let (line, hash, name) = parse_path(path.clone(), true);
// add tree reference to parent
// add blob reference to parent
if path.iter().count() == 1 {
head::add_line(line)?;
} else {
@ -125,6 +151,21 @@ pub fn read_tree(tree: String) -> Option<(String, io::Lines<io::BufReader<File>>
}
fn rm_node(path: &Path, node: &str) -> io::Result<()> {
let mut root = match path::objects() {
Some(path) => path,
None => todo!(),
};
let (dir, rest) = hash_obj(path.clone().to_str().unwrap());
root.push(dir);
root.push(rest);
read::rm_line(root, node)?;
Ok(())
}
fn add_node(path: &Path, node: &str) -> io::Result<()> {
let mut root = match path::objects() {
Some(path) => path,

View File

@ -1,9 +1,7 @@
use std::path::{Path, PathBuf};
use std::io::{self, BufRead};
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader, Write};
use std::fs::{self, File, OpenOptions};
// The output is wrapped in a Result to allow matching on errors
// Returns an Iterator to the Reader of the lines of the file.
pub fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
@ -18,3 +16,28 @@ pub fn read_folder(path: PathBuf) -> io::Result<Vec<PathBuf>> {
entries.sort();
Ok(entries)
}
pub fn rm_line(path: PathBuf, line_to_del: &str) -> io::Result<()> {
let file = File::open(path.clone())?;
let reader = BufReader::new(&file);
let mut temp_file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(format!("{}_temp", path.display()))?;
for line in reader.lines() {
let l = line?;
if l.trim() != line_to_del.trim() {
writeln!(temp_file, "{}", l)?;
}
}
drop(file);
drop(temp_file);
fs::remove_file(path.clone())?;
fs::rename(format!("{}_temp", path.display()), path)?;
Ok(())
}