Compare commits
4 Commits
4842a20024
...
a1aeb65600
Author | SHA1 | Date | |
---|---|---|---|
|
a1aeb65600 | ||
|
675b650200 | ||
|
2c593fb254 | ||
|
23908c135c |
@ -10,6 +10,8 @@ pub struct AddArgs<'a> {
|
|||||||
pub force: bool,
|
pub force: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo match deleted files
|
||||||
|
// todo match weird reg expression
|
||||||
pub fn add(args: AddArgs) {
|
pub fn add(args: AddArgs) {
|
||||||
let mut index_file = store::index::open();
|
let mut index_file = store::index::open();
|
||||||
let mut added_files: Vec<String> = vec![];
|
let mut added_files: Vec<String> = vec![];
|
||||||
@ -27,7 +29,6 @@ pub fn add(args: AddArgs) {
|
|||||||
match path.exists() {
|
match path.exists() {
|
||||||
true => {
|
true => {
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
|
|
||||||
added_files.push(String::from(path.to_str().unwrap()));
|
added_files.push(String::from(path.to_str().unwrap()));
|
||||||
add_folder_content(path.to_path_buf(), &mut added_files);
|
add_folder_content(path.to_path_buf(), &mut added_files);
|
||||||
} else {
|
} else {
|
||||||
|
@ -7,7 +7,7 @@ use crate::global::global::{DIR_PATH, set_dir_path};
|
|||||||
use crate::services::api::ApiError;
|
use crate::services::api::ApiError;
|
||||||
use crate::services::req_props::{ReqProps, ObjProps};
|
use crate::services::req_props::{ReqProps, ObjProps};
|
||||||
use crate::services::download_files::DownloadFiles;
|
use crate::services::download_files::DownloadFiles;
|
||||||
use crate::store::object::{self, add_blob, add_tree};
|
use crate::store::object::{tree, blob};
|
||||||
use crate::commands::init;
|
use crate::commands::init;
|
||||||
|
|
||||||
pub fn clone(remote: Values<'_>) {
|
pub fn clone(remote: Values<'_>) {
|
||||||
@ -93,7 +93,7 @@ pub fn clone(remote: Values<'_>) {
|
|||||||
// add tree
|
// add tree
|
||||||
let path_folder = p.strip_prefix(ref_path.clone()).unwrap();
|
let path_folder = p.strip_prefix(ref_path.clone()).unwrap();
|
||||||
let lastmodified = folder.lastmodified.unwrap().timestamp_millis();
|
let lastmodified = folder.lastmodified.unwrap().timestamp_millis();
|
||||||
if let Err(err) = add_tree(&path_folder, &lastmodified.to_string()) {
|
if let Err(err) = tree::add(&path_folder, &lastmodified.to_string()) {
|
||||||
eprintln!("err: saving ref of {} ({})", path_folder.display(), err);
|
eprintln!("err: saving ref of {} ({})", path_folder.display(), err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,7 +125,7 @@ fn download_files(ref_p: PathBuf, files: Vec<ObjProps>, api_props: &ApiProps) {
|
|||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
let relative_p = Path::new(&relative_s);
|
let relative_p = Path::new(&relative_s);
|
||||||
let lastmodified = obj.clone().lastmodified.unwrap().timestamp_millis();
|
let lastmodified = obj.clone().lastmodified.unwrap().timestamp_millis();
|
||||||
if let Err(err) = add_blob(relative_p, &lastmodified.to_string()) {
|
if let Err(err) = blob::add(relative_p, &lastmodified.to_string()) {
|
||||||
eprintln!("err: saving ref of {} ({})", relative_s.clone(), err);
|
eprintln!("err: saving ref of {} ({})", relative_s.clone(), err);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -16,27 +16,27 @@ pub fn init() {
|
|||||||
// .nextsync folder
|
// .nextsync folder
|
||||||
path.push(".nextsync");
|
path.push(".nextsync");
|
||||||
match builder.create(path.clone()) {
|
match builder.create(path.clone()) {
|
||||||
Ok(()) => println!("Directory successfuly created"),
|
Ok(()) => (),
|
||||||
Err(_) => println!("Error: cannot create directory"),
|
Err(_) => println!("Error: cannot create directory"),
|
||||||
};
|
};
|
||||||
|
|
||||||
path.push("objects");
|
path.push("objects");
|
||||||
match builder.create(path.clone()) {
|
match builder.create(path.clone()) {
|
||||||
Ok(()) => println!("Directory successfuly created"),
|
Ok(()) => (),
|
||||||
Err(_) => println!("Error: cannot create directory"),
|
Err(_) => println!("Error: cannot create directory"),
|
||||||
};
|
};
|
||||||
path.pop();
|
path.pop();
|
||||||
|
|
||||||
path.push("HEAD");
|
path.push("HEAD");
|
||||||
match File::create(path.clone()) {
|
match File::create(path.clone()) {
|
||||||
Ok(_) => println!("File successfuly created"),
|
Ok(_) => (),
|
||||||
Err(_) => println!("Error: cannot create .nextsyncignore"),
|
Err(_) => println!("Error: cannot create .nextsyncignore"),
|
||||||
}
|
}
|
||||||
|
|
||||||
path.pop();
|
path.pop();
|
||||||
path.push("index");
|
path.push("index");
|
||||||
match File::create(path.clone()) {
|
match File::create(path.clone()) {
|
||||||
Ok(_) => println!("File successfuly created"),
|
Ok(_) => (),
|
||||||
Err(_) => println!("Error: cannot create .nextsyncignore"),
|
Err(_) => println!("Error: cannot create .nextsyncignore"),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ pub fn init() {
|
|||||||
path.push(".nextsyncignore");
|
path.push(".nextsyncignore");
|
||||||
|
|
||||||
match File::create(path) {
|
match File::create(path) {
|
||||||
Ok(_) => println!("File successfuly created"),
|
Ok(_) => (),
|
||||||
Err(_) => println!("Error: cannot create .nextsyncignore"),
|
Err(_) => println!("Error: cannot create .nextsyncignore"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,18 @@
|
|||||||
|
use std::path::Path;
|
||||||
use crate::services::api::ApiError;
|
use crate::services::api::ApiError;
|
||||||
use crate::services::upload_file::UploadFile;
|
use crate::services::upload_file::UploadFile;
|
||||||
use crate::services::delete_path::DeletePath;
|
use crate::services::delete_path::DeletePath;
|
||||||
use crate::services::req_props::{ReqProps, ObjProps};
|
use crate::services::req_props::{ReqProps, ObjProps};
|
||||||
use crate::store::index;
|
use crate::store::index;
|
||||||
use crate::store::object::{add_blob, rm_blob};
|
use crate::store::object::blob;
|
||||||
use crate::commands::{status, config};
|
use crate::commands::{status, config};
|
||||||
use crate::commands::status::{State, Obj};
|
use crate::commands::status::{State, LocalObj};
|
||||||
use crate::commands::push::push_factory::{PushFactory, PushState};
|
use crate::commands::push::push_factory::{PushFactory, PushState};
|
||||||
|
|
||||||
pub mod push_factory;
|
pub mod push_factory;
|
||||||
pub mod new;
|
pub mod new;
|
||||||
pub mod deleted;
|
//pub mod new_dir;
|
||||||
|
//pub mod deleted;
|
||||||
|
|
||||||
pub fn push() {
|
pub fn push() {
|
||||||
dbg!(status::get_all_staged());
|
dbg!(status::get_all_staged());
|
||||||
@ -19,25 +21,45 @@ pub fn push() {
|
|||||||
Some(r) => r,
|
Some(r) => r,
|
||||||
None => {
|
None => {
|
||||||
eprintln!("fatal: no remote set in configuration");
|
eprintln!("fatal: no remote set in configuration");
|
||||||
std::process::exit(1);
|
//std::process::exit(1);
|
||||||
|
String::from("")
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let staged_objs = status::get_all_staged();
|
let staged_objs = status::get_all_staged();
|
||||||
// todo sort folder first
|
// todo sort folder first
|
||||||
|
|
||||||
|
// path that certify that all its children can be push whithout hesistation
|
||||||
|
// (e.g if remote dir has no changes since last sync all children
|
||||||
|
// can be pushed without verification)
|
||||||
|
let whitelist: Option<&Path> = None;
|
||||||
|
|
||||||
for obj in staged_objs {
|
for obj in staged_objs {
|
||||||
if obj.otype == String::from("tree") {
|
if obj.otype == String::from("tree") {
|
||||||
dbg!("should push folder");
|
//let push_factory = PushFactory.new_dir(obj.clone());
|
||||||
|
//let res = match push_factory.can_push(whitelist.clone()) {
|
||||||
|
// PushState::Valid => push_factory.push(),
|
||||||
|
// PushState::Done => (),
|
||||||
|
// PushState::Conflict => (),
|
||||||
|
// _ => todo!(),
|
||||||
|
//};
|
||||||
|
|
||||||
|
//match res {
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
//dbg!("should push folder");
|
||||||
} else {
|
} else {
|
||||||
let push_factory = PushFactory.new(obj.clone());
|
let push_factory = PushFactory.new(obj.clone());
|
||||||
match push_factory.can_push() {
|
match push_factory.can_push(whitelist.clone()) {
|
||||||
PushState::Valid => push_factory.push(),
|
PushState::Valid => push_factory.push(),
|
||||||
PushState::Done => (),
|
PushState::Done => (),
|
||||||
|
PushState::Conflict => {
|
||||||
|
// download file
|
||||||
|
}
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// read index
|
// read index
|
||||||
// if dir upload dir
|
// if dir upload dir
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,46 +1,24 @@
|
|||||||
|
use std::path::Path;
|
||||||
use crate::services::api::ApiError;
|
use crate::services::api::ApiError;
|
||||||
use crate::services::req_props::ReqProps;
|
use crate::services::req_props::ReqProps;
|
||||||
use crate::services::delete_path::DeletePath;
|
use crate::services::delete_path::DeletePath;
|
||||||
use crate::store::index;
|
use crate::store::index;
|
||||||
use crate::store::object::rm_blob;
|
use crate::store::object::blob;
|
||||||
use crate::commands::status::Obj;
|
use crate::commands::status::LocalObj;
|
||||||
use crate::commands::push::push_factory::{PushState, PushChange};
|
use crate::commands::push::push_factory::{PushState, PushChange, PushFlowState};
|
||||||
|
|
||||||
pub struct Deleted {
|
pub struct Deleted {
|
||||||
pub obj: Obj,
|
pub obj: LocalObj
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PushChange for Deleted {
|
impl PushChange for Deleted {
|
||||||
fn can_push(&self) -> PushState {
|
fn can_push(&self, whitelist: Option<&Path>) -> PushState {
|
||||||
// check if exist on server
|
match self.flow(&self.obj, whitelist) {
|
||||||
let res = ReqProps::new()
|
PushFlowState::Whitelisted => PushState::Done,
|
||||||
.set_url(&self.obj.path.to_str().unwrap())
|
PushFlowState::NotOnRemote => PushState::Done,
|
||||||
.getlastmodified()
|
PushFlowState::RemoteIsNewer => PushState::Conflict,
|
||||||
.send_with_err();
|
PushFlowState::LocalIsNewer => PushState::Valid,
|
||||||
|
PushFlowState::Error => PushState::Error,
|
||||||
let file_infos = match res {
|
|
||||||
Ok(obj) => Ok(Some(obj)),
|
|
||||||
Err(ApiError::IncorrectRequest(err)) => {
|
|
||||||
if err.status() == 404 {
|
|
||||||
Ok(None)
|
|
||||||
} else {
|
|
||||||
Err(())
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(_) => Err(()),
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Ok(infos) = file_infos {
|
|
||||||
if let Some(inf) = infos {
|
|
||||||
// file doesn't exist on remote
|
|
||||||
PushState::Done
|
|
||||||
} else {
|
|
||||||
// todo check date
|
|
||||||
//PushState::Conflict
|
|
||||||
PushState::Valid
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
PushState::Error
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,8 +41,12 @@ impl PushChange for Deleted {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update tree
|
// update tree
|
||||||
rm_blob(&obj.path.clone());
|
blob::rm(&obj.path.clone());
|
||||||
// remove index
|
// remove index
|
||||||
index::rm_line(obj.path.to_str().unwrap());
|
index::rm_line(obj.path.to_str().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn conflict(&self) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,45 +1,23 @@
|
|||||||
|
use std::path::Path;
|
||||||
use crate::services::api::ApiError;
|
use crate::services::api::ApiError;
|
||||||
use crate::services::req_props::ReqProps;
|
|
||||||
use crate::services::upload_file::UploadFile;
|
use crate::services::upload_file::UploadFile;
|
||||||
use crate::store::index;
|
use crate::store::index;
|
||||||
use crate::store::object::add_blob;
|
use crate::store::object::blob;
|
||||||
use crate::commands::status::Obj;
|
use crate::commands::status::LocalObj;
|
||||||
use crate::commands::push::push_factory::{PushState, PushChange, PushFactory};
|
use crate::commands::push::push_factory::{PushState, PushChange, PushFlowState};
|
||||||
|
|
||||||
pub struct New {
|
pub struct New {
|
||||||
pub obj: Obj,
|
pub obj: LocalObj,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PushChange for New {
|
impl PushChange for New {
|
||||||
fn can_push(&self) -> PushState {
|
fn can_push(&self, whitelist: Option<&Path>) -> PushState {
|
||||||
// check if exist on server
|
match self.flow(&self.obj, whitelist) {
|
||||||
let res = ReqProps::new()
|
PushFlowState::Whitelisted => PushState::Valid,
|
||||||
.set_url(&self.obj.path.to_str().unwrap())
|
PushFlowState::NotOnRemote => PushState::Valid,
|
||||||
.getlastmodified()
|
PushFlowState::RemoteIsNewer => PushState::Conflict,
|
||||||
.send_req_single();
|
PushFlowState::LocalIsNewer => PushState::Valid,
|
||||||
|
PushFlowState::Error => PushState::Error,
|
||||||
let file_infos = match res {
|
|
||||||
Ok(obj) => Ok(Some(obj)),
|
|
||||||
Err(ApiError::IncorrectRequest(err)) => {
|
|
||||||
if err.status() == 404 {
|
|
||||||
Ok(None)
|
|
||||||
} else {
|
|
||||||
Err(())
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(_) => Err(()),
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Ok(infos) = file_infos {
|
|
||||||
if let Some(info) = infos {
|
|
||||||
// file doesn't exist on remote
|
|
||||||
PushState::Valid
|
|
||||||
} else {
|
|
||||||
// todo check date
|
|
||||||
PushState::Conflict
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
PushState::Error
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,9 +41,14 @@ impl PushChange for New {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update tree
|
// update tree
|
||||||
add_blob(&obj.path.clone(), "todo_date");
|
blob::add(&obj.path.clone(), "todo_date");
|
||||||
|
|
||||||
// remove index
|
// remove index
|
||||||
index::rm_line(obj.path.to_str().unwrap());
|
index::rm_line(obj.path.to_str().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// download file with .distant at the end
|
||||||
|
fn conflict(&self) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
use crate::commands::status::{State, Obj};
|
use std::path::Path;
|
||||||
|
use crate::commands::status::{State, LocalObj};
|
||||||
|
use crate::services::api::ApiError;
|
||||||
|
use crate::store::object;
|
||||||
|
use crate::services::req_props::{ObjProps, ReqProps};
|
||||||
use crate::commands::push::new::New;
|
use crate::commands::push::new::New;
|
||||||
use crate::commands::push::deleted::Deleted;
|
//use crate::commands::push::new_dir::NewDir;
|
||||||
|
//use crate::commands::push::deleted::Deleted;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum PushState {
|
pub enum PushState {
|
||||||
@ -10,20 +15,88 @@ pub enum PushState {
|
|||||||
Error,
|
Error,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum PushFlowState {
|
||||||
|
Whitelisted,
|
||||||
|
NotOnRemote,
|
||||||
|
RemoteIsNewer,
|
||||||
|
LocalIsNewer,
|
||||||
|
Error,
|
||||||
|
}
|
||||||
|
|
||||||
pub trait PushChange {
|
pub trait PushChange {
|
||||||
fn can_push(&self) -> PushState;
|
fn can_push(&self, whitelist: Option<&Path>) -> PushState;
|
||||||
|
fn try_push(&self, whitelist: Option<&Path>);
|
||||||
fn push(&self);
|
fn push(&self);
|
||||||
|
|
||||||
|
fn is_whitelisted(&self, obj: &LocalObj, path: Option<&Path>) -> bool {
|
||||||
|
match path {
|
||||||
|
Some(p) => obj.path.starts_with(p),
|
||||||
|
None => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flow(&self, obj: &LocalObj, whitelist: Option<&Path>) -> PushFlowState {
|
||||||
|
if self.is_whitelisted(obj, whitelist) {
|
||||||
|
return PushFlowState::Whitelisted;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if exist on server
|
||||||
|
let res = ReqProps::new()
|
||||||
|
.set_url(obj.path.to_str().unwrap())
|
||||||
|
.getlastmodified()
|
||||||
|
.send_req_single();
|
||||||
|
|
||||||
|
let obj_data = match res {
|
||||||
|
Ok(obj) => Ok(Some(obj)),
|
||||||
|
Err(ApiError::IncorrectRequest(err)) => {
|
||||||
|
if err.status() == 404 {
|
||||||
|
Ok(None)
|
||||||
|
} else {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_) => Err(()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let obj_data = match obj_data {
|
||||||
|
Ok(Some(info)) => info,
|
||||||
|
Ok(None) => return PushFlowState::NotOnRemote,
|
||||||
|
Err(_) => return PushFlowState::Error,
|
||||||
|
};
|
||||||
|
|
||||||
|
// check if remote is newest
|
||||||
|
let last_sync_ts = object::get_timestamp(obj.path.to_str().unwrap().to_string()).unwrap();
|
||||||
|
let remote_ts = obj_data.lastmodified.unwrap().timestamp_millis();
|
||||||
|
|
||||||
|
if last_sync_ts < remote_ts {
|
||||||
|
PushFlowState::RemoteIsNewer
|
||||||
|
} else {
|
||||||
|
PushFlowState::LocalIsNewer
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PushFactory;
|
pub struct PushFactory;
|
||||||
|
|
||||||
impl PushFactory {
|
impl PushFactory {
|
||||||
pub fn new(&self, obj: Obj) -> Box<dyn PushChange> {
|
pub fn new(&self, obj: LocalObj) -> Box<dyn PushChange> {
|
||||||
match obj.state {
|
match obj.state {
|
||||||
State::New => Box::new(New { obj: obj.clone() }),
|
State::New => Box::new(New { obj }),
|
||||||
State::Renamed => todo!(),
|
State::Renamed => todo!(),
|
||||||
State::Modified => todo!(),
|
State::Modified => todo!(),
|
||||||
State::Deleted => Box::new(Deleted { obj: obj.clone() }),
|
State::Deleted => todo!(),
|
||||||
|
//State::Deleted => Box::new(Deleted {}),
|
||||||
|
State::Default => todo!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_dir(&self, obj: LocalObj) -> Box<dyn PushChange> {
|
||||||
|
match obj.state {
|
||||||
|
//State::New => Box::new(NewDir {}),
|
||||||
|
State::New => todo!(),
|
||||||
|
State::Renamed => todo!(),
|
||||||
|
State::Modified => todo!(),
|
||||||
|
State::Deleted => todo!(),
|
||||||
State::Default => todo!(),
|
State::Default => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,8 @@ use crypto::digest::Digest;
|
|||||||
use crypto::sha1::Sha1;
|
use crypto::sha1::Sha1;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
use crate::store::{self, object};
|
use crate::store::object::tree;
|
||||||
|
use crate::store::index;
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
enum RemoveSide {
|
enum RemoveSide {
|
||||||
@ -38,14 +39,14 @@ pub fn status() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Obj {
|
pub struct LocalObj {
|
||||||
pub otype: String,
|
pub otype: String,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
pub state: State,
|
pub state: State,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_all_staged() -> Vec<Obj> {
|
pub fn get_all_staged() -> Vec<LocalObj> {
|
||||||
// todo opti getting staged and then finding differences ?
|
// todo opti getting staged and then finding differences ?
|
||||||
// todo opti return folder
|
// todo opti return folder
|
||||||
let (mut new_objs, mut del_objs) = get_diff();
|
let (mut new_objs, mut del_objs) = get_diff();
|
||||||
@ -58,18 +59,17 @@ pub fn get_all_staged() -> Vec<Obj> {
|
|||||||
staged_objs
|
staged_objs
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_renamed(_new_obj: &mut Vec<Obj>, _del_obj: &mut Vec<Obj>) -> Vec<Obj> {
|
fn get_renamed(_new_obj: &mut Vec<LocalObj>, _del_obj: &mut Vec<LocalObj>) -> Vec<LocalObj> {
|
||||||
// get hash of all new obj, compare to hash of all del
|
// get hash of all new obj, compare to hash of all del
|
||||||
let renamed_objs = vec![];
|
let renamed_objs = vec![];
|
||||||
renamed_objs
|
renamed_objs
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_staged(objs: &mut Vec<Obj>) -> Vec<Obj> {
|
fn get_staged(objs: &mut Vec<LocalObj>) -> Vec<LocalObj> {
|
||||||
let mut indexes = HashSet::new();
|
let mut indexes = HashSet::new();
|
||||||
let mut staged_objs: Vec<Obj> = vec![];
|
let mut staged_objs: Vec<LocalObj> = vec![];
|
||||||
|
|
||||||
let nextsync_path = utils::path::nextsync().unwrap();
|
if let Ok(entries) = index::read_line() {
|
||||||
if let Ok(entries) = store::index::read_line(nextsync_path.clone()) {
|
|
||||||
for entry in entries {
|
for entry in entries {
|
||||||
indexes.insert(entry.unwrap());
|
indexes.insert(entry.unwrap());
|
||||||
}
|
}
|
||||||
@ -87,7 +87,7 @@ fn get_staged(objs: &mut Vec<Obj>) -> Vec<Obj> {
|
|||||||
staged_objs
|
staged_objs
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_diff() -> (Vec<Obj>, Vec<Obj>) {
|
fn get_diff() -> (Vec<LocalObj>, Vec<LocalObj>) {
|
||||||
let mut hashes = HashMap::new();
|
let mut hashes = HashMap::new();
|
||||||
let mut objs: Vec<String> = vec![];
|
let mut objs: Vec<String> = vec![];
|
||||||
|
|
||||||
@ -122,7 +122,7 @@ fn get_diff() -> (Vec<Obj>, Vec<Obj>) {
|
|||||||
let obj_path = root.clone().join(cur_path.clone());
|
let obj_path = root.clone().join(cur_path.clone());
|
||||||
|
|
||||||
if obj_path.is_dir() {
|
if obj_path.is_dir() {
|
||||||
if let Some((_, lines)) = object::read_tree(cur_obj.clone()) {
|
if let Some((_, lines)) = tree::read(cur_obj.clone()) {
|
||||||
add_to_hashmap(lines, &mut hashes, cur_path.clone());
|
add_to_hashmap(lines, &mut hashes, cur_path.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,8 +138,8 @@ fn get_diff() -> (Vec<Obj>, Vec<Obj>) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let del_objs: Vec<Obj> = hashes.iter().map(|x| {
|
let del_objs: Vec<LocalObj> = hashes.iter().map(|x| {
|
||||||
Obj {
|
LocalObj {
|
||||||
otype: x.1.otype.clone(),
|
otype: x.1.otype.clone(),
|
||||||
name: x.1.name.clone(),
|
name: x.1.name.clone(),
|
||||||
path: x.1.path.clone(),
|
path: x.1.path.clone(),
|
||||||
@ -147,10 +147,10 @@ fn get_diff() -> (Vec<Obj>, Vec<Obj>) {
|
|||||||
}
|
}
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
let new_objs: Vec<Obj> = objs.iter().map(|x| {
|
let new_objs: Vec<LocalObj> = objs.iter().map(|x| {
|
||||||
let p = PathBuf::from(x.to_string());
|
let p = PathBuf::from(x.to_string());
|
||||||
// todo name
|
// todo name
|
||||||
Obj {
|
LocalObj {
|
||||||
otype: get_type(p.clone()),
|
otype: get_type(p.clone()),
|
||||||
name: x.to_string(),
|
name: x.to_string(),
|
||||||
path: p,
|
path: p,
|
||||||
@ -168,14 +168,14 @@ fn get_type(p: PathBuf) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_to_hashmap(lines: Lines<BufReader<File>>, hashes: &mut HashMap<String, Obj>, path: PathBuf) {
|
fn add_to_hashmap(lines: Lines<BufReader<File>>, hashes: &mut HashMap<String, LocalObj>, path: PathBuf) {
|
||||||
for line in lines {
|
for line in lines {
|
||||||
if let Ok(ip) = line {
|
if let Ok(ip) = line {
|
||||||
if ip.clone().len() > 5 {
|
if ip.clone().len() > 5 {
|
||||||
let (ftype, hash, name) = object::parse_line(ip);
|
let (ftype, hash, name) = tree::parse_line(ip);
|
||||||
let mut p = path.clone();
|
let mut p = path.clone();
|
||||||
p.push(name.clone());
|
p.push(name.clone());
|
||||||
hashes.insert(String::from(hash), Obj{
|
hashes.insert(String::from(hash), LocalObj{
|
||||||
otype: String::from(ftype),
|
otype: String::from(ftype),
|
||||||
name: String::from(name),
|
name: String::from(name),
|
||||||
path: p,
|
path: p,
|
||||||
@ -196,7 +196,7 @@ fn add_to_vec(entries: Vec<PathBuf>, objects: &mut Vec<String>, root: PathBuf) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_status(staged_objs: Vec<Obj>, objs: Vec<Obj>) {
|
fn print_status(staged_objs: Vec<LocalObj>, objs: Vec<LocalObj>) {
|
||||||
dbg!(staged_objs.clone());
|
dbg!(staged_objs.clone());
|
||||||
dbg!(objs.clone());
|
dbg!(objs.clone());
|
||||||
if staged_objs.len() == 0 && objs.len() == 0 {
|
if staged_objs.len() == 0 && objs.len() == 0 {
|
||||||
@ -224,31 +224,31 @@ fn print_status(staged_objs: Vec<Obj>, objs: Vec<Obj>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_object(obj: Obj) {
|
fn print_object(obj: LocalObj) {
|
||||||
if obj.state == State::Deleted {
|
if obj.state == State::Deleted {
|
||||||
println!(" {} {}", String::from("deleted:").red(), obj.name.red());
|
println!(" {} {}", String::from("deleted:").red(), obj.name.red());
|
||||||
} else if obj.state == State::Renamed {
|
} else if obj.state == State::Renamed {
|
||||||
println!(" {} {}", String::from("renamed:").red(), obj.name.red());
|
println!(" {} {}", String::from("renamed:").red(), obj.name.red());
|
||||||
} else if obj.state == State::New {
|
} else if obj.state == State::New {
|
||||||
println!(" {} {}", String::from("new file:").red(), obj.name.red());
|
println!(" {} {}", String::from("new:").red(), obj.name.red());
|
||||||
} else if obj.state == State::Modified {
|
} else if obj.state == State::Modified {
|
||||||
println!(" {} {}", String::from("modified:").red(), obj.name.red());
|
println!(" {} {}", String::from("modified:").red(), obj.name.red());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_staged_object(obj: Obj) {
|
fn print_staged_object(obj: LocalObj) {
|
||||||
if obj.state == State::Deleted {
|
if obj.state == State::Deleted {
|
||||||
println!(" {} {}", String::from("deleted:").green(), obj.name.green());
|
println!(" {} {}", String::from("deleted:").green(), obj.name.green());
|
||||||
} else if obj.state == State::Renamed {
|
} else if obj.state == State::Renamed {
|
||||||
println!(" {} {}", String::from("renamed:").green(), obj.name.green());
|
println!(" {} {}", String::from("renamed:").green(), obj.name.green());
|
||||||
} else if obj.state == State::New {
|
} else if obj.state == State::New {
|
||||||
println!(" {} {}", String::from("new file:").green(), obj.name.green());
|
println!(" {} {}", String::from("new:").green(), obj.name.green());
|
||||||
} else if obj.state == State::Modified {
|
} else if obj.state == State::Modified {
|
||||||
println!(" {} {}", String::from("modified:").green(), obj.name.green());
|
println!(" {} {}", String::from("modified:").green(), obj.name.green());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_duplicate(hashes: &mut HashMap<String, Obj>, objects: &mut Vec<String>, remove_option: RemoveSide) -> Vec<String> {
|
fn remove_duplicate(hashes: &mut HashMap<String, LocalObj>, objects: &mut Vec<String>, remove_option: RemoveSide) -> Vec<String> {
|
||||||
let mut hasher = Sha1::new();
|
let mut hasher = Sha1::new();
|
||||||
let mut duplicate = vec![];
|
let mut duplicate = vec![];
|
||||||
|
|
||||||
@ -306,7 +306,7 @@ mod tests {
|
|||||||
hasher.reset();
|
hasher.reset();
|
||||||
|
|
||||||
let mut hashes = HashMap::new();
|
let mut hashes = HashMap::new();
|
||||||
let default_obj = Obj {
|
let default_obj = LocalObj {
|
||||||
otype: String::from("tree"),
|
otype: String::from("tree"),
|
||||||
name: String::from("test"),
|
name: String::from("test"),
|
||||||
path: PathBuf::from(""),
|
path: PathBuf::from(""),
|
||||||
|
@ -26,7 +26,11 @@ pub fn open() -> File {
|
|||||||
.open(path).expect("Cannot open index file")
|
.open(path).expect("Cannot open index file")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_line(mut path: PathBuf) -> io::Result<io::Lines<io::BufReader<File>>> {
|
pub fn read_line() -> io::Result<io::Lines<io::BufReader<File>>> {
|
||||||
|
let mut path = match path::nextsync() {
|
||||||
|
Some(p) => p,
|
||||||
|
None => todo!(),
|
||||||
|
};
|
||||||
path.push("index");
|
path.push("index");
|
||||||
read::read_lines(path)
|
read::read_lines(path)
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,19 @@
|
|||||||
use std::fs::File;
|
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::fs::{OpenOptions, self};
|
use std::fs::{OpenOptions, self};
|
||||||
use crypto::sha1::Sha1;
|
use crypto::sha1::Sha1;
|
||||||
use crypto::digest::Digest;
|
use crypto::digest::Digest;
|
||||||
use crate::utils::{read, path};
|
use crate::utils::{read, path};
|
||||||
use crate::store::head;
|
|
||||||
|
pub mod tree;
|
||||||
|
pub mod blob;
|
||||||
|
|
||||||
/// Returns (line, hash, name)
|
/// Returns (line, hash, name)
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// Input: /foo/bar
|
/// Input: /foo/bar
|
||||||
/// Result: ("tree hash(/foo/bar) bar", hash(/foo/bar), bar)
|
/// Result: ("tree hash(/foo/bar) bar", hash(/foo/bar), bar)
|
||||||
fn parse_path(path: &Path, is_blob: bool) -> (String, String, String) {
|
pub fn parse_path(path: &Path, is_blob: bool) -> (String, String, String) {
|
||||||
let file_name = path.file_name().unwrap().to_str().unwrap();
|
let file_name = path.file_name().unwrap().to_str().unwrap();
|
||||||
|
|
||||||
let mut hasher = Sha1::new();
|
let mut hasher = Sha1::new();
|
||||||
@ -27,83 +28,6 @@ fn parse_path(path: &Path, is_blob: bool) -> (String, String, String) {
|
|||||||
(line, hash, String::from(file_name))
|
(line, hash, String::from(file_name))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_line(line: String) -> (String, String, String) {
|
|
||||||
let mut split = line.rsplit(' ');
|
|
||||||
if split.clone().count() != 3 {
|
|
||||||
eprintln!("fatal: invalid object(s)");
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
let name = split.next().unwrap();
|
|
||||||
let hash = split.next().unwrap();
|
|
||||||
let ftype = split.next().unwrap();
|
|
||||||
(String::from(ftype), String::from(hash), String::from(name))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_tree(path: &Path, date: &str) -> io::Result<()> {
|
|
||||||
let (line, hash, name) = parse_path(path.clone(), false);
|
|
||||||
|
|
||||||
// add tree reference to parent
|
|
||||||
if path.iter().count() == 1 {
|
|
||||||
head::add_line(line)?;
|
|
||||||
} else {
|
|
||||||
add_node(path.parent().unwrap(), &line)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// create tree object
|
|
||||||
let mut content = name;
|
|
||||||
content.push_str(" ");
|
|
||||||
content.push_str(date);
|
|
||||||
create_object(hash, &content)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn rm_blob(path: &Path) -> io::Result<()> {
|
|
||||||
let (line, hash, _) = 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 blob reference to parent
|
|
||||||
if path.iter().count() == 1 {
|
|
||||||
head::add_line(line)?;
|
|
||||||
} else {
|
|
||||||
add_node(path.parent().unwrap(), &line)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut content = name.clone().to_owned();
|
|
||||||
content.push_str(" ");
|
|
||||||
content.push_str(date);
|
|
||||||
|
|
||||||
// create blob object
|
|
||||||
create_object(hash, &content)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hash_obj(obj: &str) -> (String, String) {
|
fn hash_obj(obj: &str) -> (String, String) {
|
||||||
let mut hasher = Sha1::new();
|
let mut hasher = Sha1::new();
|
||||||
hasher.input_str(obj);
|
hasher.input_str(obj);
|
||||||
@ -124,31 +48,6 @@ fn _object_path(obj: &str) -> PathBuf {
|
|||||||
root
|
root
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_tree(tree: String) -> Option<(String, io::Lines<io::BufReader<File>>)> {
|
|
||||||
let mut obj_p = match path::objects() {
|
|
||||||
Some(path) => path,
|
|
||||||
None => todo!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let (dir, res) = hash_obj(&tree);
|
|
||||||
obj_p.push(dir);
|
|
||||||
obj_p.push(res);
|
|
||||||
|
|
||||||
match read::read_lines(obj_p) {
|
|
||||||
Ok(mut reader) => {
|
|
||||||
let name = match reader.next() {
|
|
||||||
Some(Ok(line)) => line,
|
|
||||||
_ => String::from(""),
|
|
||||||
};
|
|
||||||
Some((name, reader))
|
|
||||||
},
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("error reading tree: {}", err);
|
|
||||||
None
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rm_node(path: &Path, node: &str) -> io::Result<()> {
|
fn rm_node(path: &Path, node: &str) -> io::Result<()> {
|
||||||
let mut root = match path::objects() {
|
let mut root = match path::objects() {
|
||||||
Some(path) => path,
|
Some(path) => path,
|
||||||
@ -188,7 +87,7 @@ fn add_node(path: &Path, node: &str) -> io::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_object(name: String, content: &str) -> io::Result<()> {
|
fn create_obj(name: String, content: &str) -> io::Result<()> {
|
||||||
let mut root = match path::objects() {
|
let mut root = match path::objects() {
|
||||||
Some(path) => path,
|
Some(path) => path,
|
||||||
None => todo!(),
|
None => todo!(),
|
||||||
@ -210,3 +109,35 @@ fn create_object(name: String, content: &str) -> io::Result<()> {
|
|||||||
writeln!(file, "{}", content)?;
|
writeln!(file, "{}", content)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_timestamp(path_s: String) -> Option<i64> {
|
||||||
|
let mut obj_p = match path::objects() {
|
||||||
|
Some(path) => path,
|
||||||
|
None => todo!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let (dir, res) = hash_obj(&path_s);
|
||||||
|
obj_p.push(dir);
|
||||||
|
obj_p.push(res);
|
||||||
|
|
||||||
|
match read::read_lines(obj_p) {
|
||||||
|
Ok(mut reader) => {
|
||||||
|
match reader.next() {
|
||||||
|
Some(Ok(line)) => {
|
||||||
|
let mut data = line.rsplit(' ');
|
||||||
|
if data.clone().count() >= 2 {
|
||||||
|
Some(data.next().unwrap().parse::<i64>().unwrap())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("error reading object: {}", err);
|
||||||
|
None
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
50
src/store/object/blob.rs
Normal file
50
src/store/object/blob.rs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
use std::io::{self};
|
||||||
|
use std::path::Path;
|
||||||
|
use std::fs::{self};
|
||||||
|
use crate::utils::path;
|
||||||
|
use crate::store::head;
|
||||||
|
use crate::store::object::{parse_path, add_node, create_obj, rm_node};
|
||||||
|
|
||||||
|
pub fn add(path: &Path, date: &str) -> io::Result<()> {
|
||||||
|
let (line, hash, name) = parse_path(path.clone(), true);
|
||||||
|
// add blob reference to parent
|
||||||
|
if path.iter().count() == 1 {
|
||||||
|
head::add_line(line)?;
|
||||||
|
} else {
|
||||||
|
add_node(path.parent().unwrap(), &line)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut content = name.clone().to_owned();
|
||||||
|
content.push_str(" ");
|
||||||
|
content.push_str(date);
|
||||||
|
|
||||||
|
// create blob object
|
||||||
|
create_obj(hash, &content)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rm(path: &Path) -> io::Result<()> {
|
||||||
|
let (line, hash, _) = 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(())
|
||||||
|
}
|
63
src/store/object/tree.rs
Normal file
63
src/store/object/tree.rs
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
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};
|
||||||
|
|
||||||
|
pub fn add(path: &Path, date: &str) -> io::Result<()> {
|
||||||
|
let (line, hash, name) = parse_path(path.clone(), false);
|
||||||
|
|
||||||
|
// add tree reference to parent
|
||||||
|
if path.iter().count() == 1 {
|
||||||
|
head::add_line(line)?;
|
||||||
|
} else {
|
||||||
|
add_node(path.parent().unwrap(), &line)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// create tree object
|
||||||
|
let mut content = name;
|
||||||
|
content.push_str(" ");
|
||||||
|
content.push_str(date);
|
||||||
|
create_obj(hash, &content)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read(tree: String) -> Option<(String, io::Lines<io::BufReader<File>>)> {
|
||||||
|
let mut obj_p = match path::objects() {
|
||||||
|
Some(path) => path,
|
||||||
|
None => todo!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let (dir, res) = hash_obj(&tree);
|
||||||
|
obj_p.push(dir);
|
||||||
|
obj_p.push(res);
|
||||||
|
|
||||||
|
match read::read_lines(obj_p) {
|
||||||
|
Ok(mut reader) => {
|
||||||
|
let name = match reader.next() {
|
||||||
|
Some(Ok(line)) => line,
|
||||||
|
_ => String::from(""),
|
||||||
|
};
|
||||||
|
Some((name, reader))
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("error reading tree: {}", err);
|
||||||
|
None
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_line(line: String) -> (String, String, String) {
|
||||||
|
let mut split = line.rsplit(' ');
|
||||||
|
if split.clone().count() != 3 {
|
||||||
|
eprintln!("fatal: invalid object(s)");
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
let name = split.next().unwrap();
|
||||||
|
let hash = split.next().unwrap();
|
||||||
|
let ftype = split.next().unwrap();
|
||||||
|
(String::from(ftype), String::from(hash), String::from(name))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user