save tree objects when cloning
This commit is contained in:
44
src/utils/head.rs
Normal file
44
src/utils/head.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::path::PathBuf;
|
||||
use crate::utils::{read, path};
|
||||
use std::io::{self, Write};
|
||||
|
||||
pub fn _read_only(mut path: PathBuf) -> File {
|
||||
path.push("HEAD");
|
||||
OpenOptions::new()
|
||||
.read(true)
|
||||
.open(path).expect("Cannot open HEAD file")
|
||||
}
|
||||
|
||||
pub fn _open(mut path: PathBuf) -> File {
|
||||
path.push("HEAD");
|
||||
OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(path).expect("Cannot open HEAD file")
|
||||
}
|
||||
|
||||
pub fn _read_line(mut path: PathBuf) -> io::Result<io::Lines<io::BufReader<File>>> {
|
||||
path.push("HEAD");
|
||||
read::read_lines(path)
|
||||
}
|
||||
|
||||
pub fn add_line(line: String) -> io::Result<()> {
|
||||
let mut root = match path::nextsync_root() {
|
||||
Some(path) => path,
|
||||
None => todo!(),
|
||||
};
|
||||
root.push(".nextsync");
|
||||
root.push("HEAD");
|
||||
|
||||
let mut file = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.append(true)
|
||||
.open(root)?;
|
||||
|
||||
writeln!(file, "{}", line)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -4,7 +4,7 @@ use std::path::PathBuf;
|
||||
use crate::utils::read;
|
||||
use std::io;
|
||||
|
||||
pub fn read_only(mut path: PathBuf) -> File {
|
||||
pub fn _read_only(mut path: PathBuf) -> File {
|
||||
path.push("index");
|
||||
OpenOptions::new()
|
||||
.read(true)
|
||||
|
||||
78
src/utils/object.rs
Normal file
78
src/utils/object.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use std::path::Path;
|
||||
use crate::utils::{head, path};
|
||||
use crypto::sha1::Sha1;
|
||||
use crypto::digest::Digest;
|
||||
use std::fs::{OpenOptions, self};
|
||||
use std::io::Write;
|
||||
use std::io;
|
||||
|
||||
pub fn add_tree(path: &Path) {
|
||||
dbg!(path.clone());
|
||||
let file_name = path.file_name().unwrap().to_str().unwrap();
|
||||
let mut hasher = Sha1::new();
|
||||
hasher.input_str(path.clone().to_str().unwrap());
|
||||
let hash = hasher.result_str();
|
||||
let mut line = hash.to_owned();
|
||||
line.push_str(" ");
|
||||
line.push_str(file_name);
|
||||
if path.iter().count() == 1 {
|
||||
dbg!(head::add_line(line));
|
||||
} else {
|
||||
dbg!(add_node(path.parent().unwrap(), &line));
|
||||
}
|
||||
dbg!(add_file(hash, file_name));
|
||||
dbg!(path.iter().count());
|
||||
}
|
||||
|
||||
fn add_node(path: &Path, node: &str) -> io::Result<()> {
|
||||
let mut root = match path::nextsync_root() {
|
||||
Some(path) => path,
|
||||
None => todo!(),
|
||||
};
|
||||
root.push(".nextsync");
|
||||
root.push("objects");
|
||||
|
||||
let mut hasher = Sha1::new();
|
||||
hasher.input_str(path.clone().to_str().unwrap());
|
||||
let hash = hasher.result_str();
|
||||
let (dir, rest) = hash.split_at(2);
|
||||
|
||||
root.push(dir);
|
||||
if !root.exists() {
|
||||
todo!();
|
||||
}
|
||||
root.push(rest);
|
||||
|
||||
let mut file = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.append(true)
|
||||
.open(root)?;
|
||||
|
||||
writeln!(file, "{}", node)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_file(name: String, content: &str) -> io::Result<()> {
|
||||
let mut root = match path::nextsync_root() {
|
||||
Some(path) => path,
|
||||
None => todo!(),
|
||||
};
|
||||
root.push(".nextsync");
|
||||
root.push("objects");
|
||||
let c = name.clone();
|
||||
let (dir, rest) = c.split_at(2);
|
||||
|
||||
root.push(dir);
|
||||
if !root.exists() {
|
||||
fs::create_dir_all(root.clone())?;
|
||||
}
|
||||
root.push(rest);
|
||||
dbg!(root.clone());
|
||||
let mut file = OpenOptions::new()
|
||||
.create_new(true)
|
||||
.write(true)
|
||||
.open(root)?;
|
||||
file.write_all(content.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user