From 33a151d579ad63402873bae927e5b1c7a3df1b19 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Thu, 8 Jun 2023 15:11:44 +0200 Subject: [PATCH] add option for path for status command --- src/commands.rs | 1 + src/commands/push.rs | 6 ++++ src/commands/status.rs | 71 ++++++++++++++++++++++++++++-------------- src/main.rs | 18 +++++++++-- src/utils/path.rs | 25 ++++++++++----- 5 files changed, 88 insertions(+), 33 deletions(-) create mode 100644 src/commands/push.rs diff --git a/src/commands.rs b/src/commands.rs index 7514331..fe0ae79 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -3,3 +3,4 @@ pub mod status; pub mod add; pub mod reset; pub mod clone; +pub mod push; diff --git a/src/commands/push.rs b/src/commands/push.rs new file mode 100644 index 0000000..1addb4b --- /dev/null +++ b/src/commands/push.rs @@ -0,0 +1,6 @@ +use crate::commands::status; + +pub fn push() { + dbg!(status::get_diff()); + let (staged_obj, new_obj, del_obj) = status::get_diff(); +} diff --git a/src/commands/status.rs b/src/commands/status.rs index 753e186..d9cd524 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -17,9 +17,24 @@ enum RemoveSide { // todo: relative path, filename, get modified pub fn status() { + let (staged_objs, new_objs, del_objs) = get_diff(); + dbg!(get_diff()); + print_status(staged_objs.clone(), del_objs.iter().map(|x| x.name.to_owned()).collect(), new_objs.clone()); +} + +#[derive(Debug)] +pub struct Obj { + otype: String, + name: String, + path: PathBuf, +} + +// todo next time +// add relative path in command and in obj +pub fn get_diff() -> (Vec, Vec, Vec) { let mut hashes = HashMap::new(); - let mut objects: Vec = vec![]; - let mut staged_objects: Vec = vec![]; + let mut objs: Vec = vec![]; + let mut staged_objs: Vec = vec![]; let root = match utils::path::nextsync_root() { Some(path) => path, @@ -29,18 +44,22 @@ pub fn status() { } }; - let mut next_sync_path = root.clone(); - next_sync_path.push(".nextsync"); + dbg!(utils::path::current()); + let nextsync_path = utils::path::nextsync().unwrap(); + let current_p = utils::path::current().unwrap(); + let mut dist_path = current_p.strip_prefix(root.clone()); + dbg!(dist_path.clone()); + - if let Ok(lines) = read_head(next_sync_path.clone()) { - add_to_hashmap(lines, &mut hashes); + if let Ok(lines) = read_head(nextsync_path.clone()) { + add_to_hashmap(lines, &mut hashes, root.clone()); } if let Ok(entries) = utils::read::read_folder(root.clone()) { - add_to_vec(entries, &mut objects, root.clone()); + add_to_vec(entries, &mut objs, root.clone()); } - let mut obj_to_analyse = find_missing_elements(&mut hashes, &mut objects, RemoveSide::Both); + let mut obj_to_analyse = remove_duplicate(&mut hashes, &mut objs, RemoveSide::Both); dbg!(obj_to_analyse.clone()); while obj_to_analyse.len() > 0 { @@ -49,14 +68,14 @@ pub fn status() { if obj_path.is_dir() { if let Some((_, lines)) = object::read_tree(cur_obj.clone()) { - add_to_hashmap(lines, &mut hashes); + add_to_hashmap(lines, &mut hashes, obj_path.clone()); } if let Ok(entries) = utils::read::read_folder(obj_path.clone()) { - add_to_vec(entries, &mut objects, root.clone()); + add_to_vec(entries, &mut objs, root.clone()); } - let diff = find_missing_elements(&mut hashes, &mut objects, RemoveSide::Both); + let diff = remove_duplicate(&mut hashes, &mut objs, RemoveSide::Both); obj_to_analyse.append(&mut diff.clone()); } else { // todo look for change @@ -64,26 +83,31 @@ pub fn status() { } - if let Ok(entries) = utils::index::read_line(next_sync_path.clone()) { + if let Ok(entries) = utils::index::read_line(nextsync_path.clone()) { for entry in entries { // todo hash this - staged_objects.push(String::from(entry.unwrap())); + staged_objs.push(String::from(entry.unwrap())); } } - // print - let del_objs = hashes.clone().iter().map(|x| String::from(x.1)).collect(); - print_status(staged_objects.clone(), del_objs, objects.clone()); - dbg!(hashes); - dbg!(objects); + let del_objs: Vec = hashes.iter().map(|x| { + Obj {otype: x.1.otype.clone(), name: x.1.name.clone(), path: x.1.path.clone()} + }).collect(); + (staged_objs.clone(), objs.clone(), del_objs) } -fn add_to_hashmap(lines: Lines>, hashes: &mut HashMap) { +fn add_to_hashmap(lines: Lines>, hashes: &mut HashMap, path: PathBuf) { for line in lines { if let Ok(ip) = line { if ip.clone().len() > 5 { let (ftype, hash, name) = object::parse_line(ip); - hashes.insert(String::from(hash), String::from(name)); + let mut p = path.clone(); + p.push(name.clone()); + hashes.insert(String::from(hash), Obj{ + otype: String::from(ftype), + name: String::from(name), + path: p, + }); } } } @@ -100,7 +124,6 @@ fn add_to_vec(entries: Vec, objects: &mut Vec, root: PathBuf) { } fn print_status(staged_objs: Vec, del_objs: Vec, new_objs: Vec) { - if staged_objs.len() == 0 && del_objs.len() == 0 && new_objs.len() == 0 { println!("Nothing to push, working tree clean"); return; @@ -128,7 +151,7 @@ fn print_status(staged_objs: Vec, del_objs: Vec, new_objs: Vec, objects: &mut Vec, remove_option: RemoveSide) -> Vec { +fn remove_duplicate(hashes: &mut HashMap, objects: &mut Vec, remove_option: RemoveSide) -> Vec { let mut hasher = Sha1::new(); let mut to_remove: Vec = vec![]; let mut i = 0; @@ -177,7 +200,7 @@ mod tests { use super::*; #[test] - fn test_find_missing_elements() { + fn test_remove_duplicate() { let mut hasher = Sha1::new(); hasher.input_str("file1"); let hash1 = hasher.result_str(); @@ -200,7 +223,7 @@ mod tests { objects.push(String::from("file1")); objects.push(String::from("file2")); objects.push(String::from("file3")); - find_missing_elements(&mut hashes, &mut objects, RemoveSide::Both); + remove_duplicate(&mut hashes, &mut objects, RemoveSide::Both); dbg!(hashes.clone()); dbg!(objects.clone()); assert_eq!(hashes.contains(&hash4), true); diff --git a/src/main.rs b/src/main.rs index 598d04b..0ae3af0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,8 +18,17 @@ fn main() { .value_name("DIRECTORY") ) ) - .subcommand(SubCommand::with_name("status")) + .subcommand( + SubCommand::with_name("status") + .arg( + Arg::with_name("directory") + .required(false) + .takes_value(true) + .value_name("DIRECTORY") + ) + ) .subcommand(SubCommand::with_name("reset")) + .subcommand(SubCommand::with_name("push")) .subcommand( SubCommand::with_name("clone") .arg( @@ -53,7 +62,10 @@ fn main() { global::global::set_dir_path(String::from(val.clone().next().unwrap())); } commands::init::init(); - } else if let Some(_) = matches.subcommand_matches("status") { + } else if let Some(matches) = matches.subcommand_matches("status") { + if let Some(val) = matches.values_of("directory") { + global::global::set_dir_path(String::from(val.clone().next().unwrap())); + } commands::status::status(); } else if let Some(matches) = matches.subcommand_matches("add") { if let Some(files) = matches.values_of("files") { @@ -68,6 +80,8 @@ fn main() { if let Some(remote) = matches.values_of("remote") { commands::clone::clone(remote); } + } else if let Some(matches) = matches.subcommand_matches("push") { + commands::push::push(); } } diff --git a/src/utils/path.rs b/src/utils/path.rs index 1eb8d3c..de183c7 100644 --- a/src/utils/path.rs +++ b/src/utils/path.rs @@ -3,26 +3,28 @@ use std::path::{PathBuf, Path}; use crate::global::global::DIR_PATH; use std::fs::canonicalize; -pub fn nextsync_root() -> Option { +pub fn current() -> Option { let d = DIR_PATH.lock().unwrap(); - let mut path = match d.clone() { + match d.clone() { Some(dir) => { let tmp = PathBuf::from(dir).to_owned(); if tmp.is_absolute() { - tmp + Some(tmp) } else { let current_dir = env::current_dir().ok()?; let abs = current_dir.join(tmp); let canonicalized_path = canonicalize(abs).ok()?; - canonicalized_path + Some(canonicalized_path) } }, - None => env::current_dir().ok()?, - }; + None => Some(env::current_dir().ok()?), + } +} - dbg!(path.clone()); +pub fn nextsync_root() -> Option { + let mut path = current()?; let root = loop { path.push(".nextsync"); @@ -39,6 +41,15 @@ pub fn nextsync_root() -> Option { root } + +pub fn nextsync() -> Option { + if let Some(mut path) = nextsync_root() { + path.push(".nextsync"); + return Some(path); + } + None +} + pub fn objects() -> Option { if let Some(mut path) = nextsync_root() { path.push(".nextsync");