Skip to content

Commit

Permalink
Added flag for case sensitivity (sorry, same branch)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerugo committed Jul 23, 2024
1 parent ac2611d commit 5c538af
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
35 changes: 14 additions & 21 deletions prelude
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
36 changes: 36 additions & 0 deletions test_prelude.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5c538af

Please sign in to comment.