feat(add): start to add file and improve nsobject
This commit is contained in:
parent
c7bde2c8d5
commit
c5d7135d99
@ -1,10 +1,6 @@
|
|||||||
use crate::config::config::Config;
|
use crate::config::config::Config;
|
||||||
use crate::store::ignorer::Ignorer;
|
use crate::store::{ignorer::Ignorer, nsobject::NsObject};
|
||||||
// use crate::store::object::object::{Obj, ObjMethods};
|
use std::fs;
|
||||||
// use crate::store::{self, object::Object};
|
|
||||||
// use crate::utils::nextsyncignore::{self, ignore_file};
|
|
||||||
// use crate::utils::path::{normalize_relative, path_buf_to_string, repo_root};
|
|
||||||
// use crate::utils::{self, path};
|
|
||||||
// use glob::glob;
|
// use glob::glob;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
@ -26,6 +22,25 @@ pub fn exec(args: AddArgs, config: Config) {
|
|||||||
return add_dir(config.get_root_unsafe(), &mut ignorer);
|
return add_dir(config.get_root_unsafe(), &mut ignorer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for obj_to_add in args.files.iter() {
|
||||||
|
let mut path_to_add = config.execution_path.clone();
|
||||||
|
path_to_add.push(PathBuf::from(obj_to_add));
|
||||||
|
|
||||||
|
if path_to_add.exists() {
|
||||||
|
if path_to_add.is_dir() {
|
||||||
|
add_dir(&path_to_add, &mut ignorer);
|
||||||
|
} else {
|
||||||
|
add_to_index(&path_to_add);
|
||||||
|
}
|
||||||
|
} else if NsObject::from_local_path(&path_to_add).exists() {
|
||||||
|
add_to_index(&path_to_add);
|
||||||
|
} else {
|
||||||
|
// try globbing
|
||||||
|
// todo!()
|
||||||
|
// else panic
|
||||||
|
panic!("fatal: pathspec '{}' did not match any files", obj_to_add);
|
||||||
|
}
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
for all files
|
for all files
|
||||||
if globbing
|
if globbing
|
||||||
@ -45,15 +60,38 @@ pub fn exec(args: AddArgs, config: Config) {
|
|||||||
/// * `dir`: the directory to add from
|
/// * `dir`: the directory to add from
|
||||||
/// * `force`: true if we should apply the nextsyncignore's rules
|
/// * `force`: true if we should apply the nextsyncignore's rules
|
||||||
fn add_dir(dir: &PathBuf, ignorer: &mut Ignorer) {
|
fn add_dir(dir: &PathBuf, ignorer: &mut Ignorer) {
|
||||||
/*
|
let entries = match fs::read_dir(dir) {
|
||||||
for elements present in dir {
|
Ok(entries) => entries,
|
||||||
if !force && should_ignore() {
|
Err(err) => {
|
||||||
|
eprintln!("Failed to read {} ({err})", dir.display());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Read locals files and folders
|
||||||
|
for entry in entries {
|
||||||
|
let entry = match entry {
|
||||||
|
Ok(entry) => entry,
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Failed to read entry {err}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ignore object if it is ns config file or nsignored
|
||||||
|
if ignorer.should_ignore(&entry.path()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if is dir
|
|
||||||
add_dir(new_dir, force)
|
|
||||||
else
|
|
||||||
|
|
||||||
|
if entry.path().is_dir() {
|
||||||
|
add_dir(&entry.path(), ignorer);
|
||||||
|
add_to_index(&entry.path());
|
||||||
|
} else {
|
||||||
|
add_to_index(&entry.path());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
*/
|
}
|
||||||
|
|
||||||
|
fn add_to_index(path: &PathBuf) {
|
||||||
|
println!("{}", path.display());
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,7 @@ fn compare_dir(pool: ThreadPool, root_path: &PathBuf, path: &PathBuf, res: Arc<O
|
|||||||
let repo_relative_entry = path::to_repo_relative(&entry.path(), root_path);
|
let repo_relative_entry = path::to_repo_relative(&entry.path(), root_path);
|
||||||
let local_obj = Obj::from_local_path(&repo_relative_entry);
|
let local_obj = Obj::from_local_path(&repo_relative_entry);
|
||||||
|
|
||||||
if entry.path().is_dir() {
|
if entry.path().is_dir() {
|
||||||
if entry.path().ends_with(".nextsync") {
|
if entry.path().ends_with(".nextsync") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -34,10 +34,14 @@ impl Ignorer {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_config_file(&self, path: &PathBuf) -> bool {
|
||||||
|
path.ends_with(".nextsync")
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns whether a file/dir is ignored or not
|
/// Returns whether a file/dir is ignored or not
|
||||||
///
|
///
|
||||||
/// * `path`:
|
/// * `path`:
|
||||||
fn is_ignored(&mut self, path: PathBuf) -> bool {
|
fn is_ignored(&mut self, path: &PathBuf) -> bool {
|
||||||
is_file_ignored(path.to_str().unwrap(), self.get_rules())
|
is_file_ignored(path.to_str().unwrap(), self.get_rules())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,7 +51,7 @@ impl Ignorer {
|
|||||||
/// `active_nsignore`.
|
/// `active_nsignore`.
|
||||||
///
|
///
|
||||||
/// * `path`:
|
/// * `path`:
|
||||||
pub fn should_ignore(&mut self, path: PathBuf) -> bool {
|
pub fn should_ignore(&mut self, path: &PathBuf) -> bool {
|
||||||
self.use_nsignore && self.is_ignored(path)
|
self.is_config_file(path) || (self.use_nsignore && self.is_ignored(path))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,43 @@
|
|||||||
use crate::store::object::{ObjType, Obj, ObjMetadata};
|
use crate::store::object::{Obj, ObjMetadata, ObjType};
|
||||||
|
use crypto::digest::Digest;
|
||||||
|
use crypto::sha1::Sha1;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::OnceLock;
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
type NsObjectChilds = Vec<Box<NsObject>>;
|
type NsObjectChilds = Vec<Box<NsObject>>;
|
||||||
|
|
||||||
|
struct NsObjectPath {
|
||||||
|
path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&str> for NsObjectPath {
|
||||||
|
fn from(hash: &str) -> Self {
|
||||||
|
let (dir, res) = hash.split_at(2);
|
||||||
|
|
||||||
|
let mut ns_obj_path = PathBuf::new();
|
||||||
|
ns_obj_path.push(dir);
|
||||||
|
ns_obj_path.push(res);
|
||||||
|
|
||||||
|
NsObjectPath { path: ns_obj_path }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&PathBuf> for NsObjectPath {
|
||||||
|
fn from(obj_path: &PathBuf) -> Self {
|
||||||
|
let mut hasher = Sha1::new();
|
||||||
|
hasher.input_str(
|
||||||
|
obj_path
|
||||||
|
.to_str()
|
||||||
|
.expect("Cannot contains non UTF-8 char in path"),
|
||||||
|
);
|
||||||
|
NsObjectPath::from(hasher.result_str().as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct NsObject {
|
pub struct NsObject {
|
||||||
pub obj_type: ObjType,
|
pub obj_type: ObjType,
|
||||||
obj_path: OnceLock<PathBuf>,
|
obj_path: OnceLock<PathBuf>,
|
||||||
|
nsobj_path: OnceLock<PathBuf>,
|
||||||
childs: OnceLock<NsObjectChilds>,
|
childs: OnceLock<NsObjectChilds>,
|
||||||
index: usize,
|
index: usize,
|
||||||
}
|
}
|
||||||
@ -16,6 +47,7 @@ impl NsObject {
|
|||||||
NsObject {
|
NsObject {
|
||||||
obj_type: ObjType::Obj,
|
obj_type: ObjType::Obj,
|
||||||
obj_path: OnceLock::from(path.to_path_buf()),
|
obj_path: OnceLock::from(path.to_path_buf()),
|
||||||
|
nsobj_path: OnceLock::new(),
|
||||||
childs: OnceLock::new(),
|
childs: OnceLock::new(),
|
||||||
index: 0,
|
index: 0,
|
||||||
}
|
}
|
||||||
@ -25,11 +57,27 @@ impl NsObject {
|
|||||||
NsObject {
|
NsObject {
|
||||||
obj_type: ObjType::Obj,
|
obj_type: ObjType::Obj,
|
||||||
obj_path: OnceLock::new(),
|
obj_path: OnceLock::new(),
|
||||||
|
nsobj_path: OnceLock::from(NsObjectPath::from(hash).path),
|
||||||
childs: OnceLock::new(),
|
childs: OnceLock::new(),
|
||||||
index: 0,
|
index: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_obj_path(&self) -> &PathBuf {
|
||||||
|
self.obj_path.get_or_init(|| todo!())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_nsobj_path(&self) -> &PathBuf {
|
||||||
|
self.nsobj_path
|
||||||
|
.get_or_init(|| NsObjectPath::from(self.get_obj_path()).path)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the existence of the nsobj in the store
|
||||||
|
pub fn exists(&self) -> bool {
|
||||||
|
println!("{}", self.get_nsobj_path().display());
|
||||||
|
self.get_nsobj_path().exists()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_childs(&self) -> &NsObjectChilds {
|
pub fn get_childs(&self) -> &NsObjectChilds {
|
||||||
self.childs.get_or_init(|| {
|
self.childs.get_or_init(|| {
|
||||||
if self.obj_type != ObjType::Tree {
|
if self.obj_type != ObjType::Tree {
|
||||||
@ -40,12 +88,6 @@ impl NsObject {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_obj_path(&self) -> &PathBuf {
|
|
||||||
self.obj_path.get_or_init(|| {
|
|
||||||
todo!()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_file(&self) -> bool {
|
pub fn is_file(&self) -> bool {
|
||||||
self.obj_type == ObjType::Blob
|
self.obj_type == ObjType::Blob
|
||||||
}
|
}
|
||||||
@ -60,6 +102,24 @@ impl NsObject {
|
|||||||
obj
|
obj
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Saving the nsobj in the store
|
||||||
|
///
|
||||||
|
/// first line consists of all size fixed datas (obj_type, size, modified)
|
||||||
|
/// next lines are the size variable datas
|
||||||
|
///
|
||||||
|
/// * if it is a Tree obj after an empty line there will be the definition
|
||||||
|
/// of its subobjs (one line by subobj) *
|
||||||
|
/// obj_type + hash
|
||||||
|
pub fn save(&self) -> Result<(), ()> {
|
||||||
|
if !self.get_obj_path().exists {
|
||||||
|
// delete current obj
|
||||||
|
// delete reference on parent
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_metadata(&self) -> Option<ObjMetadata> {
|
pub fn get_metadata(&self) -> Option<ObjMetadata> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
@ -67,7 +127,7 @@ impl NsObject {
|
|||||||
pub fn iter(&self) -> NsObjectIter<'_> {
|
pub fn iter(&self) -> NsObjectIter<'_> {
|
||||||
NsObjectIter {
|
NsObjectIter {
|
||||||
nsobject: self,
|
nsobject: self,
|
||||||
index: 0
|
index: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,7 +144,7 @@ impl<'a> Iterator for NsObjectIter<'a> {
|
|||||||
self.index += 1;
|
self.index += 1;
|
||||||
match self.nsobject.get_childs().get(self.index - 1) {
|
match self.nsobject.get_childs().get(self.index - 1) {
|
||||||
None => None,
|
None => None,
|
||||||
Some(obj) => Some(&**obj)
|
Some(obj) => Some(&**obj),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user