use retain

This commit is contained in:
grimhilt 2023-06-16 18:49:08 +02:00
parent 9956727cc9
commit cbbf185b1a
3 changed files with 19 additions and 35 deletions

View File

@ -3,7 +3,6 @@ use crate::utils::{self, nextsyncignore};
use crate::store; use crate::store;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::io::Write; use std::io::Write;
use glob::glob;
pub struct AddArgs<'a> { pub struct AddArgs<'a> {
pub files: Values<'a>, pub files: Values<'a>,

View File

@ -76,23 +76,14 @@ fn get_staged(objs: &mut Vec<Obj>) -> Vec<Obj> {
} }
} }
let mut to_remove: Vec<usize> = vec![]; objs.retain(|obj| {
let mut index = 0;
for obj in &mut *objs {
dbg!(obj.clone().path.to_str().unwrap());
if indexes.contains(obj.clone().path.to_str().unwrap()) { if indexes.contains(obj.clone().path.to_str().unwrap()) {
staged_objs.push(obj.clone()); staged_objs.push(obj.clone());
to_remove.push(index); false
} else {
true
} }
index += 1; });
}
let mut offset = 0;
for i in to_remove {
objs.remove(i + offset.clone());
offset += 1;
}
staged_objs staged_objs
} }
@ -260,35 +251,29 @@ fn print_staged_object(obj: Obj) {
fn remove_duplicate(hashes: &mut HashMap<String, Obj>, objects: &mut Vec<String>, remove_option: RemoveSide) -> Vec<String> { fn remove_duplicate(hashes: &mut HashMap<String, Obj>, objects: &mut Vec<String>, remove_option: RemoveSide) -> Vec<String> {
let mut hasher = Sha1::new(); let mut hasher = Sha1::new();
let mut to_remove: Vec<usize> = vec![];
let mut i = 0;
let mut duplicate = vec![]; let mut duplicate = vec![];
for object in &mut *objects { objects.retain(|obj| {
// hash the object // hash the object
hasher.input_str(object); hasher.input_str(obj);
let hash = hasher.result_str(); let hash = hasher.result_str();
hasher.reset(); hasher.reset();
// find it on the list of hashes // find it on the list of hashes
if hashes.contains_key(&hash) { if hashes.contains_key(&hash) {
duplicate.push(object.clone()); duplicate.push(obj.clone());
// remove from hashes
if remove_option == RemoveSide::Left || remove_option == RemoveSide::Both { if remove_option == RemoveSide::Left || remove_option == RemoveSide::Both {
hashes.remove(&hash); hashes.remove(&hash);
} }
if remove_option == RemoveSide::Right || remove_option == RemoveSide::Both {
to_remove.push(i);
}
}
i += 1;
}
// remove all objects existing in the list of hashes // remove from objects
i = 0; remove_option != RemoveSide::Right && remove_option != RemoveSide::Both
for index in to_remove { } else {
objects.remove(index-i); true
i += 1; }
} });
duplicate duplicate
} }

View File

@ -1,7 +1,7 @@
use crate::utils::{read, path}; use crate::utils::path;
use regex::Regex; use regex::Regex;
use std::fs::File; use std::fs::File;
use std::io::{Cursor, Lines, BufReader, empty, BufRead}; use std::io::{Cursor, BufReader, BufRead};
fn read_lines() -> Result<Vec<String>, ()> { fn read_lines() -> Result<Vec<String>, ()> {
if let Some(path) = path::nextsyncignore() { if let Some(path) = path::nextsyncignore() {
@ -56,7 +56,7 @@ fn normalize_rule(l: String) -> String {
pub fn ignore_file(path: &String, lines: Vec<String>, ignored_f: &mut Vec<String>) -> bool { pub fn ignore_file(path: &String, lines: Vec<String>, ignored_f: &mut Vec<String>) -> bool {
let mut ignored = false; let mut ignored = false;
for mut line in lines { for line in lines {
if line.starts_with("!") { if line.starts_with("!") {
if !ignored { if !ignored {
continue; continue;