diff --git a/src/_utils/_git_secret_tools.sh b/src/_utils/_git_secret_tools.sh index b900f4b6..3fd0c4a3 100644 --- a/src/_utils/_git_secret_tools.sh +++ b/src/_utils/_git_secret_tools.sh @@ -222,6 +222,11 @@ function _get_record_filename { local filename filename=$(echo "$record" | awk -F: '{print $1}') + # remove / in top level file + if [[ $filename == "/"* ]]; then + filename="${filename:1}" + fi + echo "$filename" } @@ -304,6 +309,12 @@ function _git_normalize_filename { local result result=$(git ls-files --full-name -o "$filename") + + # if file is in the top level, append a / so only that top level file is considered + if [[ $filename != *"/"* ]]; then + result="/$result" + fi + echo "$result" } diff --git a/tests/test_add.bats b/tests/test_add.bats index a533d96c..70d5dbe0 100644 --- a/tests/test_add.bats +++ b/tests/test_add.bats @@ -33,7 +33,7 @@ function teardown { local files_list files_list=$(cat "$path_mappings") - [ "$files_list" = "$filename" ] + [ "$files_list" = "/$filename" ] # Cleaning up: rm "$filename" ".gitignore" @@ -70,7 +70,7 @@ function teardown { echo "content" > "$test_file" local quoted_name - quoted_name=$(printf '%q' "$test_file") + quoted_name=$(printf '%q' "/$test_file") # add -i is now a no-op (See #225) so this tests that -i does nothing. run git secret add -i "$test_file" @@ -114,7 +114,7 @@ function teardown { [ "$status" -eq 0 ] [[ -f "$current_dir/.gitignore" ]] - run file_has_line "$nested_dir/$test_file" "$current_dir/.gitignore" + run file_has_line "/$nested_dir/$test_file" "$current_dir/.gitignore" [ "$output" = '0' ] # .gitignore was not created: @@ -205,7 +205,7 @@ function teardown { local files_list files_list=$(cat "$path_mappings") - [ "$files_list" = "$filename" ] + [ "$files_list" = "/$filename" ] # Cleaning up: rm "$filename" ".gitignore" @@ -268,7 +268,7 @@ function teardown { local files_list files_list=$(cat "$path_mappings") - [ "$files_list" = "$filename" ] + [ "$files_list" = "/$filename" ] # Ensuring the file is correctly git-ignored run git check-ignore "$filename" @@ -276,4 +276,4 @@ function teardown { # Cleaning up: rm "$filename" ".gitignore" -} +} \ No newline at end of file diff --git a/tests/test_hide.bats b/tests/test_hide.bats index 71cd526e..8ff21e53 100644 --- a/tests/test_hide.bats +++ b/tests/test_hide.bats @@ -278,7 +278,7 @@ function teardown { [ "$status" -eq 0 ] # File must be removed: - [ ! -f "$FILE_TO_HIDE" ] + [ ! -f "/$FILE_TO_HIDE" ] } @@ -287,7 +287,7 @@ function teardown { [ "$status" -eq 0 ] # File must be removed: - [ ! -f "$FILE_TO_HIDE" ] + [ ! -f "/$FILE_TO_HIDE" ] # It should be verbose: [[ "$output" == *"removing unencrypted files"* ]] @@ -311,8 +311,8 @@ function teardown { [ "$status" -eq 0 ] # File must be removed: - [ ! -f "$FILE_TO_HIDE" ] - [ ! -f "$second_file" ] + [ ! -f "/$FILE_TO_HIDE" ] + [ ! -f "/$second_file" ] # It should be verbose: [[ "$output" == *"removing unencrypted files"* ]] @@ -330,4 +330,4 @@ function teardown { run git secret hide [ "$status" -eq 0 ] [[ "$output" == *"git-secret: done. 1 of 1 files are hidden."* ]] -} +} \ No newline at end of file