diff --git a/src/commands/status.rs b/src/commands/status.rs index db82329..753e186 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; use colored::Colorize; use std::path::PathBuf; use std::path::Path; -use std::io; +use std::io::{self, Lines, BufReader}; use crate::utils::{self, object}; #[derive(PartialEq)] @@ -33,25 +33,13 @@ pub fn status() { next_sync_path.push(".nextsync"); if let Ok(lines) = read_head(next_sync_path.clone()) { - for line in lines { - if let Ok(ip) = line { - dbg!(ip.clone().len()); - if ip.clone().len() > 5 { - let (ftype, hash, name) = object::parse_line(ip); - hashes.insert(String::from(hash), String::from(name)); - } - } - } + add_to_hashmap(lines, &mut hashes); } if let Ok(entries) = utils::read::read_folder(root.clone()) { - for entry in entries { - if !is_nextsync_config(entry.clone()) { - let object_path = entry.strip_prefix(root.clone()).unwrap(); - objects.push(String::from(object_path.to_str().unwrap())); - } - } + add_to_vec(entries, &mut objects, root.clone()); } + let mut obj_to_analyse = find_missing_elements(&mut hashes, &mut objects, RemoveSide::Both); dbg!(obj_to_analyse.clone()); @@ -61,23 +49,11 @@ pub fn status() { if obj_path.is_dir() { if let Some((_, lines)) = object::read_tree(cur_obj.clone()) { - 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)); - } - } - } + add_to_hashmap(lines, &mut hashes); } if let Ok(entries) = utils::read::read_folder(obj_path.clone()) { - for entry in entries { - if !is_nextsync_config(entry.clone()) { - let object_path = entry.strip_prefix(root.clone()).unwrap(); - objects.push(String::from(object_path.to_str().unwrap())); - } - } + add_to_vec(entries, &mut objects, root.clone()); } let diff = find_missing_elements(&mut hashes, &mut objects, RemoveSide::Both); @@ -102,6 +78,27 @@ pub fn status() { dbg!(objects); } +fn add_to_hashmap(lines: Lines>, hashes: &mut HashMap) { + 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)); + } + } + } +} + +fn add_to_vec(entries: Vec, objects: &mut Vec, root: PathBuf) { + for entry in entries { + if !is_nextsync_config(entry.clone()) { + let object_path = entry.strip_prefix(root.clone()).unwrap(); + objects.push(String::from(object_path.to_str().unwrap())); + } + } + +} + 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 {