From 5c538af081d17b1c1f791914c905d5ad744dc844 Mon Sep 17 00:00:00 2001 From: aerugo Date: Tue, 23 Jul 2024 19:10:09 +0200 Subject: [PATCH] Added flag for case sensitivity (sorry, same branch) --- prelude | 35 ++++++++++++++--------------------- test_prelude.bats | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/prelude b/prelude index 7ae6047..3b2b9cc 100755 --- a/prelude +++ b/prelude @@ -66,6 +66,14 @@ print_manual() { echo } +# Initialize variables +specified_path="" +output_filename="" +tree_pattern="" +git_only=false +case_sensitive=false +exclude_patterns="prelude|.git|.preludeignore|.gitignore" + # Function to check if a command exists command_exists() { command -v "$1" >/dev/null 2>&1 @@ -112,17 +120,15 @@ filter_files() { local file="$1" local match=false - # Enable case-insensitive matching only if not case sensitive - if [ "$case_sensitive" = false ]; then - shopt -s nocasematch - fi - if [ -n "$tree_pattern" ]; then IFS='|' read -ra patterns <<< "$tree_pattern" for pattern in "${patterns[@]}"; do - if [[ $file == $pattern ]]; then - match=true - break + if [ "$case_sensitive" = true ]; then + [[ "$file" == $pattern ]] && match=true && break + else + shopt -s nocasematch + [[ "$file" == $pattern ]] && match=true && break + shopt -u nocasematch fi done else @@ -132,11 +138,6 @@ filter_files() { if [ "$match" = true ] && ( [ -z "$exclude_patterns" ] || ! [[ $file =~ $exclude_patterns ]] ); then echo "$file" fi - - # Disable case-insensitive matching if it was enabled - if [ "$case_sensitive" = false ]; then - shopt -u nocasematch - fi } # Function to validate a pattern @@ -148,14 +149,6 @@ validate_pattern() { fi } -# Initialize variables -specified_path="" -output_filename="" -tree_pattern="" -git_only=false -case_sensitive=false -exclude_patterns="prelude|.git|.preludeignore|.gitignore" - # Parse command line arguments while getopts ":P:F:M:cgC-:" opt; do case ${opt} in diff --git a/test_prelude.bats b/test_prelude.bats index 626eacf..652a2ff 100644 --- a/test_prelude.bats +++ b/test_prelude.bats @@ -289,6 +289,42 @@ teardown() { grep -q "Uncommitted change" output.txt } +@test "Script respects case sensitivity with -M and -c flags" { + touch src/UPPERCASE.txt src/lowercase.txt + run ./prelude -M "*CASE.txt" -c -F output.txt + [ "$status" -eq 0 ] + [[ "$output" == *"src/UPPERCASE.txt"* ]] + [[ "$output" != *"src/lowercase.txt"* ]] +} + +@test "Script ignores case sensitivity with -M flag without -c flag" { + touch src/UPPERCASE.txt src/lowercase.txt + run ./prelude -M "*CASE.txt" -F output.txt + [ "$status" -eq 0 ] + [[ "$output" == *"src/UPPERCASE.txt"* ]] + [[ "$output" == *"src/lowercase.txt"* ]] +} + +@test "Script respects case sensitivity with -M, -c, and -g flags" { + touch src/UPPERCASE.txt src/lowercase.txt + git add src/UPPERCASE.txt src/lowercase.txt + git commit -m "Add case-sensitive files" + run ./prelude -M "*CASE.txt" -c -g -F output.txt + [ "$status" -eq 0 ] + [[ "$output" == *"src/UPPERCASE.txt"* ]] + [[ "$output" != *"src/lowercase.txt"* ]] +} + +@test "Script ignores case sensitivity with -M and -g flags without -c flag" { + touch src/UPPERCASE.txt src/lowercase.txt + git add src/UPPERCASE.txt src/lowercase.txt + git commit -m "Add case-sensitive files" + run ./prelude -M "*CASE.txt" -g -F output.txt + [ "$status" -eq 0 ] + [[ "$output" == *"src/UPPERCASE.txt"* ]] + [[ "$output" == *"src/lowercase.txt"* ]] +} + @test "Script handles merge conflicts" { git checkout -b test-branch echo "Branch change" > src/test.txt