Adds new strategy on working with the relative paths.

Changes:
1. When adding new file path is normalized in respect to both CWD and repo root
2. All commands now know about that
3. Tests are fixed

Refs #86, #85, #83
pull/90/head
sobolevn 8 years ago
parent 8a1fe788f8
commit 763e130b8d
No known key found for this signature in database
GPG Key ID: FF672D568AE3C73E

@ -144,8 +144,18 @@ function _show_manual_for {
function _check_ignore {
local filename="$1" # required
git check-ignore --no-index -q "$filename";
echo $?
local result
result="$(git check-ignore --no-index -q "$filename" > /dev/null 2>&1; echo $?)";
echo "$result"
}
function _git_normalize_filename {
local filename="$1" # required
local result
result=$(git ls-files --full-name -o "$filename")
echo "$result"
}
@ -165,12 +175,13 @@ function _add_ignored_file {
# This function adds a line with the filename into the '.gitgnore' file.
# It also creates '.gitignore' if it's not there
local filename="$1" # required
_maybe_create_gitignore
local full_path
full_path=$(_append_root_path '.gitignore')
local filename="$1" # required
echo "$filename" >> "$full_path"
}
@ -267,8 +278,11 @@ function _find_and_clean {
# optional:
local verbose=${2:-""} # can be empty or should be equal to "v"
local root
root=$(_get_git_root_path)
# shellcheck disable=2086
find . -name "$pattern" -type f -print0 | xargs -0 rm -f$verbose
find "$root" -name "$pattern" -type f -print0 | xargs -0 rm -f$verbose
}

@ -18,22 +18,30 @@ function add {
_user_required
# Checking if all files are ignored:
# Checking if all files are correct (ignored and inside the repo):
local not_ignored=()
local items=( "$@" )
# Checking if all files in options are ignored:
for item in "${items[@]}"; do
# Checking if all files in options are ignored:
if [[ ! -f "$item" ]]; then
local path # absolute path
local normalized_path # relative to the .git dir
normalized_path=$(_git_normalize_filename "$item")
path=$(_append_root_path "$normalized_path")
# Checking that file is valid:
if [[ ! -f "$path" ]]; then
_abort "$item is not a file."
fi
# Checking that it is ignored:
local ignored
ignored=$(_check_ignore "$item")
if [[ ! "$ignored" -eq 0 ]]; then
# Collect unignored files.
not_ignored+=("$item")
ignored=$(_check_ignore "$path")
if [[ "$ignored" -ne 0 ]]; then
# Collect unignored files:
not_ignored+=("$normalized_path")
fi
done
@ -65,13 +73,16 @@ function add {
path_mappings=$(_get_secrets_dir_paths_mapping)
for item in "${items[@]}"; do
local path
path=$(_git_normalize_filename "$item")
# Adding files into system, skipping duplicates.
local already_in
already_in=$(_file_has_line "$item" "$path_mappings")
already_in=$(_file_has_line "$path" "$path_mappings")
if [[ "$already_in" -eq 1 ]]; then
echo "$item" >> "$path_mappings"
echo "$path" >> "$path_mappings"
fi
done
echo "${#@} items added."
echo "${#@} item(s) added."
}

@ -21,7 +21,7 @@ function changes {
_user_required
local filenames="$*"
if [[ -z $filenames ]]; then
if [[ -z "$filenames" ]]; then
# Checking if no filenames are passed, show diff for all files.
filenames=$(_list_all_added_files)
fi
@ -33,14 +33,25 @@ function changes {
local decrypted
local diff_result
local path # absolute path
local normalized_path # relative to the .git dir
normalized_path=$(_git_normalize_filename "$filename")
if [[ ! -z "$normalized_path" ]]; then
path=$(_append_root_path "$normalized_path")
else
# Path was already normalized
path=$(_append_root_path "$filename")
fi
# Now we have all the data required:
decrypted=$(_decrypt "$filename" "0" "0" "$homedir" "$passphrase")
decrypted=$(_decrypt "$path" "0" "0" "$homedir" "$passphrase")
# Let's diff the result:
diff_result=$(diff -u <(echo "$decrypted") "$filename") || true
diff_result=$(diff -u <(echo "$decrypted") "$path") || true
# There was a bug in the previous version, since `diff` returns
# exit code `1` when the files are different.
echo "changes in ${filename}:"
echo "changes in ${path}:"
echo "${diff_result}"
done
}

@ -16,17 +16,17 @@ function _optional_delete {
local verbose=${2:-""}
if [[ $delete -eq 1 ]]; then
local path_mappings
path_mappings=$(_get_secrets_dir_paths_mapping)
# We use custom formating here:
if [[ ! -z "$verbose" ]]; then
echo && echo 'removing unencrypted files:'
fi
local path_mappings
path_mappings=$(_get_secrets_dir_paths_mapping)
while read -r line; do
# So the formating would not be repeated several times here:
_find_and_clean "*$line" "$verbose"
_find_and_clean "$line" "$verbose"
done < "$path_mappings"
if [[ ! -z "$verbose" ]]; then
@ -81,9 +81,14 @@ function hide {
local gpg_local
gpg_local=$(_get_gpg_local)
local input_path
local output_path
input_path=$(_append_root_path "$line")
output_path=$(_append_root_path "$encrypted_filename")
# shellcheck disable=2086
$gpg_local --use-agent --yes --trust-model=always --encrypt \
$recipients -o "$encrypted_filename" "$line"
$recipients -o "$output_path" "$input_path"
counter=$((counter+1))
done < "$path_mappings"

@ -26,20 +26,29 @@ function remove {
path_mappings=$(_get_secrets_dir_paths_mapping)
for item in "$@"; do
if [[ ! -f "$item" ]]; then
local path # absolute path
local normalized_path # relative to .git folder
normalized_path=$(_git_normalize_filename "$item")
path=$(_append_root_path "$normalized_path")
echo "$item -> $normalized_path -> $path"
# Checking if file exists:
if [[ ! -f "$path" ]]; then
_abort "$item is not a file."
fi
_delete_line "$item" "$path_mappings"
# Deleting it from path mappings:
_delete_line "$normalized_path" "$path_mappings"
rm -f "${path_mappings}.bak" # not all systems create '.bak'
if [[ "$clean" == 1 ]]; then
# Optional clean:
if [[ "$clean" -eq 1 ]]; then
local encrypted_filename
encrypted_filename=$(_get_encrypted_filename "$item")
encrypted_filename=$(_get_encrypted_filename "$path")
rm -f "$encrypted_filename"
rm "$encrypted_filename" # fail on error
fi
done
echo 'removed from index.'

@ -32,8 +32,11 @@ function reveal {
local counter=0
while read -r line; do
local path
path=$(_append_root_path "$line")
# The parameters are: filename, write-to-file, force, homedir, passphrase
_decrypt "$line" "1" "$force" "$homedir" "$passphrase"
_decrypt "$path" "1" "$force" "$homedir" "$passphrase"
counter=$((counter+1))
done < "$path_mappings"

@ -20,6 +20,7 @@ function teardown {
@test "run 'add' normally" {
# Preparations:
local filename="local_file"
echo "content" > "$filename"
echo "$filename" > ".gitignore"
@ -27,41 +28,42 @@ function teardown {
run git secret add "$filename"
[ "$status" -eq 0 ]
rm -f "$filename" ".gitignore"
# Ensuring that path mappings was set correctly:
local path_mappings
path_mappings=$(_get_secrets_dir_paths_mapping)
local files_list=$(cat "$path_mappings")
[ "$files_list" = "$filename" ]
# Cleaning up:
rm "$filename" ".gitignore"
}
@test "run 'add' for unignored file" {
local TEST_FILE='test_file'
touch "$TEST_FILE"
echo "content" > "$TEST_FILE"
local test_file='test_file'
touch "$test_file"
echo "content" > "$test_file"
run git secret add "$TEST_FILE"
run git secret add "$test_file"
[ "$status" -eq 1 ]
rm "$TEST_FILE"
rm "$test_file"
}
@test "run 'add' for unignored file with '-i'" {
local TEST_FILE='test_file.auto_ignore'
touch "$TEST_FILE"
echo "content" > "$TEST_FILE"
local test_file='test_file.auto_ignore'
touch "$test_file"
echo "content" > "$test_file"
run git secret add -i "$TEST_FILE"
run git secret add -i "$test_file"
[ "$status" -eq 0 ]
run _file_has_line "$TEST_FILE" ".gitignore"
run _file_has_line "$test_file" ".gitignore"
[ "$status" -eq 0 ]
rm "$TEST_FILE"
rm "$test_file"
}
@ -81,15 +83,15 @@ function teardown {
mkdir -p "$nested_dir"
cd "$nested_dir"
local TEST_FILE='test_file.auto_ignore'
touch "$TEST_FILE"
echo "content" > "$TEST_FILE"
local test_file='test_file.auto_ignore'
touch "$test_file"
echo "content" > "$test_file"
# Test commands:
run git secret add -i "$TEST_FILE"
run git secret add -i "$test_file"
[ "$status" -eq 0 ]
run _file_has_line "$TEST_FILE" "../.gitignore"
run _file_has_line "$test_file" "../.gitignore"
[ "$status" -eq 0 ]
# .gitignore was not created:
@ -101,36 +103,75 @@ function teardown {
}
@test "run 'add' for relative path" {
if [[ "$BATS_RUNNING_FROM_GIT" -eq 1 ]]; then
skip "this test is skiped while 'git commmit'"
fi
# Prepations:
local root='test_dir'
local node="$root/node"
local sibling="$root/sibling"
local test_file="$node/test_file"
local current_dir=$(pwd)
mkdir -p "$node"
mkdir -p "$sibling"
echo "content" > "$test_file"
echo "$test_file" > ".gitignore"
cd "$sibling"
# Testing:
run git secret add "../node/test_file"
[ "$status" -eq 0 ]
[[ "$output" == *"1 item(s) added."* ]]
# Testing mappings content:
local path_mappings
path_mappings=$(_get_secrets_dir_paths_mapping)
local files_list=$(cat "$path_mappings")
[ "$files_list" = "$test_file" ]
# Cleaning up:
cd "$current_dir"
rm -r "$root"
}
@test "run 'add' for file in subfolder" {
local TEST_FILE='test_file'
local TEST_DIR='test_dir'
# Preparations:
local test_file='test_file'
local test_dir='test_dir'
mkdir -p "$TEST_DIR"
touch "$TEST_DIR/$TEST_FILE"
echo "content" > "$TEST_DIR/$TEST_FILE"
echo "$TEST_DIR/$TEST_FILE" > ".gitignore"
mkdir -p "$test_dir"
touch "$test_dir/$test_file"
echo "content" > "$test_dir/$test_file"
echo "$test_dir/$test_file" > ".gitignore"
run git secret add "$TEST_DIR/$TEST_FILE"
# Testing:
run git secret add "$test_dir/$test_file"
[ "$status" -eq 0 ]
[[ "$output" == *"1 items added."* ]]
[[ "$output" == *"1 item(s) added."* ]]
rm -r "$TEST_DIR"
# Cleaning up:
rm -r "$test_dir"
}
@test "run 'add' twice for one file" {
# Preparations:
local filename="local_file"
echo "content" > "$filename"
echo "$filename" > ".gitignore"
# Testing:
run git secret add "$filename"
run git secret add "$filename"
[ "$status" -eq 0 ]
echo "$output"
echo "dance"
[ "$output" = "1 items added." ]
rm "$filename" ".gitignore"
[ "$output" = "1 item(s) added." ]
# Ensuring that path mappings was set correctly:
local path_mappings
@ -138,10 +179,14 @@ function teardown {
local files_list=$(cat "$path_mappings")
[ "$files_list" = "$filename" ]
# Cleaning up:
rm "$filename" ".gitignore"
}
@test "run 'add' for multiple files" {
# Preparations:
local filename1="local_file1"
echo "content1" > "$filename1"
echo "$filename1" > ".gitignore"
@ -150,9 +195,11 @@ function teardown {
echo "content2" > "$filename2"
echo "$filename2" >> ".gitignore"
# Testing:
run git secret add "$filename1" "$filename2"
[ "$status" -eq 0 ]
[ "$output" = "2 items added." ]
[ "$output" = "2 item(s) added." ]
# Cleaning up:
rm "$filename1" "$filename2" ".gitignore"
}

@ -39,7 +39,8 @@ function teardown {
[ "$status" -eq 0 ]
# Testing that output has both filename and changes:
[[ "$output" == *"changes in $FILE_TO_HIDE"* ]]
local fullpath=$(_append_root_path "$FILE_TO_HIDE")
[[ "$output" == *"changes in $fullpath"* ]]
[[ "$output" == *"+$new_content"* ]]
}
@ -53,7 +54,8 @@ function teardown {
[ "$status" -eq 0 ]
# Testing that output has both filename and changes:
[[ "$output" == *"changes in $FILE_TO_HIDE"* ]]
local fullpath=$(_append_root_path "$FILE_TO_HIDE")
[[ "$output" == *"changes in $fullpath"* ]]
[[ "$output" == *"-$FILE_CONTENTS"* ]]
[[ "$output" == *"+$new_content"* ]]
}
@ -78,11 +80,13 @@ function teardown {
[ "$status" -eq 0 ]
# Testing that output has both filename and changes:
[[ "$output" == *"changes in $FILE_TO_HIDE"* ]]
local fullpath=$(_append_root_path "$FILE_TO_HIDE")
[[ "$output" == *"changes in $fullpath"* ]]
[[ "$output" == *"+$new_content"* ]]
[[ "$output" == *"changes in $SECOND_FILE_TO_HIDE"* ]]
[[ "$output" == *"$second_file_to_hide"* ]]
local second_path=$(_append_root_path "$SECOND_FILE_TO_HIDE")
[[ "$output" == *"changes in $second_path"* ]]
[[ "$output" == *"+$second_new_content"* ]]
}
@ -99,9 +103,11 @@ function teardown {
[ "$status" -eq 0 ]
# Testing that output has both filename and changes:
[[ "$output" == *"changes in $FILE_TO_HIDE"* ]]
local fullpath=$(_append_root_path "$FILE_TO_HIDE")
[[ "$output" == *"changes in $fullpath"* ]]
[[ "$output" == *"+$new_content"* ]]
[[ "$output" == *"changes in $SECOND_FILE_TO_HIDE"* ]]
[[ "$output" == *"+$second_file_to_hide"* ]]
local second_path=$(_append_root_path "$SECOND_FILE_TO_HIDE")
[[ "$output" == *"changes in $second_path"* ]]
[[ "$output" == *"+$second_new_content"* ]]
}

@ -75,6 +75,8 @@ function teardown {
run git secret hide -d
[ "$status" -eq 0 ]
ls && pwd
# File must be removed:
[ ! -f "$FILE_TO_HIDE" ]
}
@ -84,6 +86,8 @@ function teardown {
run git secret hide -v -d
[ "$status" -eq 0 ]
ls && pwd
# File must be removed:
[ ! -f "$FILE_TO_HIDE" ]

@ -5,9 +5,6 @@ load _test_base
FIRST_FILE="file_to_hide1"
SECOND_FILE="file_to_hide2"
FOLDER="somedir"
FILE_IN_FOLDER="${FOLDER}/file_to_hide3"
function setup {
install_fixture_key "$TEST_DEFAULT_USER"
@ -24,7 +21,6 @@ function setup {
function teardown {
rm "$FIRST_FILE" "$SECOND_FILE"
rm -r "$FOLDER"
uninstall_fixture_key "$TEST_DEFAULT_USER"
unset_current_state
@ -46,6 +42,10 @@ function _has_line {
run git secret remove "$SECOND_FILE"
[ "$status" -eq 0 ]
# Test output:
[[ "$output" == *"removed from index."* ]]
[[ "$output" == *"ensure that files: [$SECOND_FILE] are now not ignored."* ]]
# Mapping should not contain the second file:
local mapping_contains=$(_has_line "$SECOND_FILE")
[ "$mapping_contains" -eq 1 ]
@ -87,27 +87,32 @@ function _has_line {
# see https://github.com/sobolevn/git-secret/issues/23
# Prepartions:
mkdir -p "$FOLDER"
set_state_secret_add "$FILE_IN_FOLDER" "somecontent3"
local folder="somedir"
local file_in_folder="$folder/file_to_hide3"
mkdir -p "$folder"
set_state_secret_add "$file_in_folder" "somecontent3"
set_state_secret_hide # runing hide again to hide new data
# Now it should remove filename with slashes from the mapping:
run git secret remove "$FILE_IN_FOLDER"
run git secret remove "$file_in_folder"
[ "$status" -eq 0 ]
local mapping_contains=$(_has_line "$FILE_IN_FOLDER")
local mapping_contains=$(_has_line "$file_in_folder")
[ "$mapping_contains" -eq 1 ]
local enctypted_file=$(_get_encrypted_filename $FILE_IN_FOLDER)
local enctypted_file=$(_get_encrypted_filename $file_in_folder)
[ -f "$enctypted_file" ]
# Cleaning up:
rm -r "$folder"
}
@test "run 'remove' with '-c'" {
git secret hide
set_state_secret_hide
run git secret remove -c "$SECOND_FILE"
echo "$output"
[ "$status" -eq 0 ]
local mapping_contains=$(_has_line "$SECOND_FILE")
@ -115,6 +120,8 @@ function _has_line {
local first_enctypted_file=$(_get_encrypted_filename $FIRST_FILE)
local second_enctypted_file=$(_get_encrypted_filename $SECOND_FILE)
echo "$output"
echo "$first_enctypted_file and $second_enctypted_file"
[ -f "$first_enctypted_file" ]
[ ! -f "$second_enctypted_file" ]

Loading…
Cancel
Save