improve getting staged in status

This commit is contained in:
grimhilt 2023-06-29 22:55:19 +02:00
parent da3d605baa
commit 601f176198

View File

@ -29,15 +29,22 @@ pub enum State {
// todo: relative path, filename, get modified // todo: relative path, filename, get modified
// todo: not catch added empty folder // todo: not catch added empty folder
pub fn status() { pub fn status() {
let (mut new_objs, mut del_objs) = get_diff(); let (mut new_objs_hashes, mut del_objs_hashes) = get_diff();
let mut renamed_objs = get_renamed(&mut new_objs, &mut del_objs);
// get copy, modified // get copy, modified
let mut objs = new_objs; let mut staged_objs = get_staged(&mut del_objs_hashes);
objs.append(&mut del_objs); staged_objs.append(&mut get_staged(&mut new_objs_hashes));
objs.append(&mut renamed_objs);
let staged_objs = get_staged(&mut objs); let mut objs: Vec<LocalObj> = del_objs_hashes.iter().map(|x| {
x.1.clone()
}).collect();
for (_, elt) in new_objs_hashes {
objs.push(elt.clone());
}
dbg!(objs.clone());
dbg!(staged_objs.clone());
print_status(staged_objs, objs); print_status(staged_objs, objs);
dbg!(get_all_staged());
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -49,53 +56,45 @@ pub struct LocalObj {
} }
pub fn get_all_staged() -> Vec<LocalObj> { pub fn get_all_staged() -> Vec<LocalObj> {
let (mut new_objs_hashes, mut del_objs_hashes) = get_diff();
// get copy, modified
let mut staged_objs = get_staged(&mut del_objs_hashes);
staged_objs.append(&mut get_staged(&mut new_objs_hashes));
staged_objs.clone()
// todo opti getting staged and then finding differences ? // todo opti getting staged and then finding differences ?
// todo opti return folder
let (mut new_objs, mut del_objs) = get_diff();
let mut renamed_objs = get_renamed(&mut new_objs, &mut del_objs);
// todo get copy, modified
let mut objs = new_objs;
objs.append(&mut del_objs);
objs.append(&mut renamed_objs);
let staged_objs = get_staged(&mut objs);
staged_objs
} }
fn get_renamed(_new_obj: &mut Vec<LocalObj>, _del_obj: &mut Vec<LocalObj>) -> Vec<LocalObj> { fn get_staged(objs_hashes: &mut HashMap<String, LocalObj>) -> Vec<LocalObj> {
// get hash of all new obj, compare to hash of all del let mut lines: Vec<String> = vec![];
let renamed_objs = vec![];
renamed_objs
}
fn get_staged(objs: &mut Vec<LocalObj>) -> Vec<LocalObj> {
let mut indexes = HashSet::new();
let mut staged_objs: Vec<LocalObj> = vec![];
if let Ok(entries) = index::read_line() { if let Ok(entries) = index::read_line() {
for entry in entries { for entry in entries {
indexes.insert(entry.unwrap()); lines.push(entry.unwrap());
} }
} }
dbg!(objs.clone());
let mut tree_to_analyse: Vec<LocalObj> = vec![];
objs.retain(|obj| { let mut hasher = Sha1::new();
if indexes.contains(obj.clone().path.to_str().unwrap()) { let mut staged_objs: Vec<LocalObj> = vec![];
if obj.clone().otype == String::from("tree") {
tree_to_analyse.push(obj.clone()); for obj in lines {
} // hash the object
staged_objs.push(obj.clone()); hasher.input_str(&obj);
false let hash = hasher.result_str();
} else { hasher.reset();
true
} // find it on the list of hashes
}); if objs_hashes.contains_key(&hash) {
staged_objs.push(objs_hashes.get(&hash).unwrap().clone());
objs_hashes.remove(&hash);
}
}
staged_objs staged_objs
} }
fn get_diff() -> (Vec<LocalObj>, Vec<LocalObj>) { fn get_diff() -> (HashMap<String, LocalObj>, HashMap<String, LocalObj>) {
let mut hashes = HashMap::new(); let mut hashes = HashMap::new();
let mut objs: Vec<String> = vec![]; let mut objs: Vec<String> = vec![];
@ -138,29 +137,31 @@ fn get_diff() -> (Vec<LocalObj>, Vec<LocalObj>) {
} }
let del_objs: Vec<LocalObj> = hashes.iter().map(|x| { for (_, elt) in &mut hashes {
LocalObj { elt.state = State::Deleted;
otype: x.1.otype.clone(), }
name: x.1.name.clone(),
path: x.1.path.clone(),
state: State::Deleted
}
}).collect();
let new_objs: Vec<LocalObj> = objs.iter().map(|x| { let mut new_objs_hashes = HashMap::new();
let p = PathBuf::from(x.to_string()); let mut hasher = Sha1::new();
for obj in objs {
// hash the object
hasher.input_str(&obj);
let hash = hasher.result_str();
hasher.reset();
let p = PathBuf::from(obj.to_string());
// todo name // todo name
LocalObj { new_objs_hashes.insert(String::from(hash), LocalObj{
otype: get_type(p.clone()), otype: get_otype(p.clone()),
name: x.to_string(), name: obj.to_string(),
path: p, path: p,
state: State::New state: State::New
} });
}).collect(); }
(new_objs, del_objs)
(new_objs_hashes, hashes)
} }
fn get_type(p: PathBuf) -> String { fn get_otype(p: PathBuf) -> String {
if p.is_dir() { if p.is_dir() {
String::from("tree") String::from("tree")
} else { } else {