git-secret/src/commands/git_secret_remove.sh
Josh Rabinowitz 549cd9aa24
Issue 706 decrypt errors (#831)
* check for encrypted version of file before decrypting, for #706
* improve error messages, verbose output and non-verbose output
* in tests, prefix output from git init with 'git: '
* 'clean' options only remove added files, for #833
* update changelog
2022-04-19 17:14:55 -04:00

65 lines
1.4 KiB
Bash

#!/usr/bin/env bash
function remove {
local clean=0
OPTIND=1
while getopts 'ch' opt; do
case "$opt" in
c) clean=1;;
h) _show_manual_for 'remove';;
*) _invalid_option_for 'remove';;
esac
done
shift $((OPTIND-1))
[ "$1" = '--' ] && shift
# Validate if user exists:
_user_required
# Command logic:
local path_mappings
path_mappings=$(_get_secrets_dir_paths_mapping)
for item in "$@"; do
local path # absolute path
local normalized_path # relative to .git folder
normalized_path=$(_git_normalize_filename "$item")
path=$(_prepend_root_path "$normalized_path")
# Checking if file exists:
if [[ ! -f "$path" ]]; then
_abort "file not found: $item"
fi
# Deleting it from path mappings:
# Remove record from fsdb with matching key
local key
key="$normalized_path"
fsdb="$path_mappings"
_fsdb_rm_record "$key" "$fsdb"
rm -f "${path_mappings}.bak" # not all systems create '.bak'
# Optional clean:
if [[ "$clean" -eq 1 ]]; then
local encrypted_filename
encrypted_filename=$(_get_encrypted_filename "$path")
rm "$encrypted_filename" # fail on error
if [[ -n "$_SECRETS_VERBOSE" ]]; then
_message "deleted: $encrypted_filename"
fi
fi
done
echo 'git-secret: removed from index.'
echo "git-secret: ensure that files: [$*] are now not ignored."
}