From 53b103af9e1515a854eb26f8c136f766b2849cad Mon Sep 17 00:00:00 2001 From: grimhilt Date: Sat, 28 Oct 2023 22:12:27 +0200 Subject: [PATCH] fix add -A --- src/commands/add.rs | 41 +++++++++++++++++---------------- src/commands/status.rs | 8 ++----- src/services/request_manager.rs | 5 ---- src/utils/path.rs | 4 ++++ tests/add/file.sh | 26 ++++++++++++++++----- tests/main.sh | 15 ++++++------ 6 files changed, 54 insertions(+), 45 deletions(-) diff --git a/src/commands/add.rs b/src/commands/add.rs index c1e7c72..66021f5 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -5,7 +5,7 @@ use clap::Values; use glob::glob; use crate::store::index; use crate::store::{self, object::Object}; -use crate::utils; +use crate::utils::{self, path}; use crate::utils::nextsyncignore::{self, ignore_file}; use crate::utils::path::{normalize_relative, repo_root, path_buf_to_string}; @@ -19,17 +19,21 @@ pub struct AddArgs<'a> { // todo match deleted files pub fn add(args: AddArgs) { - // write all modification in the index - if args.all { - write_all(); - return; - } + + let mut pattern: String; + let file_vec: Vec<&str> = match args.all { + true => { + pattern = path_buf_to_string(repo_root()); + pattern.push_str("/*"); + vec![&pattern] + }, + false => args.files.unwrap().collect(), + }; let mut added_files: Vec = vec![]; let mut ignored_f = vec![]; let rules = nextsyncignore::get_rules(); - let file_vec: Vec<&str> = args.files.unwrap().collect(); for file in file_vec { let f = match normalize_relative(file) { Ok(f) => f, @@ -58,6 +62,9 @@ pub fn add(args: AddArgs) { added_files.push(String::from(f)); } else { for entry in try_globbing(path) { + if path::is_nextsync_config(entry.clone()) { + continue; + } if !args.force && ignore_file(&path_buf_to_string(entry.clone()), rules.clone(), &mut ignored_f) { continue; } @@ -125,22 +132,16 @@ fn add_folder_content(path: PathBuf, added_files: &mut Vec) { if let Ok(entries) = utils::read::read_folder(folder.clone()) { for entry in entries { let path_entry = PathBuf::from(entry); - if path_entry.is_dir() { - folders.push(path_entry.clone()); + if !path::is_nextsync_config(path_entry.clone()) + { + if path_entry.is_dir() { + folders.push(path_entry.clone()); + } + added_files.push(path_buf_to_string(path_entry.strip_prefix(repo_root()).unwrap().to_path_buf())); + } - added_files.push(String::from(path_entry.to_str().unwrap())); } } } } -fn write_all() { - let objs = get_all_objs(); - let mut index_file = OpenOptions::new() - .write(true) - .create(true) - .open(index::path()).expect("Cannot open index file"); - for obj in objs { - let _ = writeln!(index_file, "{}", path_buf_to_string(obj.path.clone())); - } -} diff --git a/src/commands/status.rs b/src/commands/status.rs index 7434d7f..021c6e2 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -95,7 +95,7 @@ fn should_retain(hasher: &mut Sha1, key: String, obj: LocalObj, move_copy_hashes // todo deal with directories if obj.path.is_dir() { - return false; + return true; } let mut blob = Blob::new(obj.path.clone()); let mut flag = true; @@ -329,7 +329,7 @@ fn add_to_hashmap(lines: Lines>, hashes: &mut HashMap, objects: &mut Vec, root: PathBuf) { for entry in entries { - if !is_nextsync_config(entry.clone()) { + if !path::is_nextsync_config(entry.clone()) { let object_path = entry.strip_prefix(root.clone()).unwrap(); objects.push(String::from(object_path.to_str().unwrap())); } @@ -441,10 +441,6 @@ fn remove_duplicate(hashes: &mut HashMap, objects: &mut Vec bool { - path.ends_with(".nextsync") -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/services/request_manager.rs b/src/services/request_manager.rs index 3919835..16506b3 100644 --- a/src/services/request_manager.rs +++ b/src/services/request_manager.rs @@ -76,9 +76,4 @@ impl RequestManager { self.token.clone().unwrap() } - - pub fn create_request() - { - - } } diff --git a/src/utils/path.rs b/src/utils/path.rs index 2d29ad4..830dac5 100644 --- a/src/utils/path.rs +++ b/src/utils/path.rs @@ -104,6 +104,10 @@ pub fn repo_root() -> PathBuf { } } +pub fn is_nextsync_config(path: PathBuf) -> bool { + path.ends_with(".nextsync") || path.starts_with(".nextsync") +} + pub fn nextsync() -> PathBuf { let mut path = repo_root(); path.push(".nextsync"); diff --git a/tests/add/file.sh b/tests/add/file.sh index c8bc202..9f75b96 100755 --- a/tests/add/file.sh +++ b/tests/add/file.sh @@ -1,5 +1,7 @@ #!/bin/sh +source ./utils.sh + nb_tests=0 TEST_SUITE_NAME="add/file/" @@ -13,7 +15,7 @@ get_exe() { } setup_env() { [ ! -v exe ] && get_exe - path=$(mktemp -d) + path=$(mktd) cd $path } @@ -64,7 +66,7 @@ add_regex() { add_test "regex" "titi riri" "./*" "new: riri\nnew: titi" } -add_subdir() { +add_file_subdir() { nb_tests=$((nb_tests + 1)) setup_env $exe init @@ -72,7 +74,19 @@ add_subdir() { touch dir/toto $exe add "./dir/toto" res=$($exe status --nostyle) - add_cmp "subdir" "new: dir/toto" + add_cmp "file_subdir" "new: dir/toto" +} + +add_whole_subdir() { + nb_tests=$((nb_tests + 1)) + setup_env + $exe init + mkdir dir + touch dir/toto + touch dir/roro + $exe add "dir" + res=$($exe status --nostyle) + add_cmp "whole_subdir" "new: dir/roro\nnew: dir/toto\nnew: dir" } add_subdir_regex() { @@ -106,8 +120,7 @@ add_all() { touch dir/toto dir/roro lolo $exe add -A res=$($exe status --nostyle) - add/file/all: Output differ: - add_cmp "all" "new: dir/roro\nnew: dir/toto\nnew: lolo\nnew: .nextsyncignore" + add_cmp "all" "new: .nextsyncignore\nnew: dir/roro\nnew: dir/toto\nnew: dir\nnew: lolo" } #test nextsyncignore @@ -119,7 +132,8 @@ add_basics add_space add_multiple add_regex -add_subdir +add_file_subdir +add_whole_subdir add_subdir_regex add_duplication add_duplication_subdir diff --git a/tests/main.sh b/tests/main.sh index dca5b9f..5fb1a09 100755 --- a/tests/main.sh +++ b/tests/main.sh @@ -1,14 +1,11 @@ #!/bin/sh +source ./utils.sh + # Getting all tests -TESTS=$(find -name "*.sh" -not -name "main.sh") +TESTS=$(find -mindepth 2 -name "*.sh") if [ $# -ne 0 ]; then - TESTS=$(find -name "*$1*" -not -name "main.sh") - tests="" - for obj in $TESTS; do - [ -d $obj ] && tests+=$(find -path "$obj/*.sh" -not -name "main.sh") - done; - TESTS=$tests + TESTS=$(find -mindepth 2 -path "*$1*") fi # Executing tests @@ -18,7 +15,7 @@ for test in $TESTS; do #nb_tests=$((nb_tests + 1)) # run file - tmp_stderr=$(mktemp) + tmp_stderr=$(mktf) nb_tests_tmp=$($test 2>"$tmp_stderr") exit_code=$? capture_stderr=$(<"$tmp_stderr") @@ -41,4 +38,6 @@ for test in $TESTS; do fi done; +#rm -rf /tmp/*_nextsync + echo -e "\nRan $nb_tests tests ($((nb_tests - nb_success)) Failed)"