Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanups and renames #784

Merged
merged 3 commits into from
Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/_utils/_git_secret_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fi
: "${TMPDIR:=/tmp}"

# AWK scripts:
# shellcheck disable=2016
# shellcheck disable=SC2016
AWK_FSDB_HAS_RECORD='
BEGIN { FS=":"; OFS=":"; cnt=0; }
{
Expand All @@ -45,7 +45,7 @@ BEGIN { FS=":"; OFS=":"; cnt=0; }
END { if ( cnt > 0 ) print "0"; else print "1"; }
'

# shellcheck disable=2016
# shellcheck disable=SC2016
AWK_FSDB_RM_RECORD='
BEGIN { FS=":"; OFS=":"; }
{
Expand All @@ -56,15 +56,15 @@ BEGIN { FS=":"; OFS=":"; }
}
'

# shellcheck disable=2016
# shellcheck disable=SC2016
AWK_FSDB_CLEAR_HASHES='
BEGIN { FS=":"; OFS=":"; }
{
print $1,"";
}
'

# shellcheck disable=2016
# shellcheck disable=SC2016
AWK_GPG_VER_CHECK='
/^gpg/{
version=$3
Expand Down Expand Up @@ -103,6 +103,7 @@ GPG_VER_MIN_21="$($SECRETS_GPG_COMMAND --version | gawk "$AWK_GPG_VER_CHECK")"

# Bash:

# echos 0 if function exists, otherwise non-zero
function _function_exists {
local function_name="$1" # required

Expand Down Expand Up @@ -306,7 +307,7 @@ function _maybe_create_gitignore {
# This function creates '.gitignore' if it was missing.

local full_path
full_path=$(_append_root_path '.gitignore')
full_path=$(_prepend_root_path '.gitignore')

if [[ ! -f "$full_path" ]]; then
touch "$full_path"
Expand All @@ -323,7 +324,7 @@ function _add_ignored_file {
_maybe_create_gitignore

local full_path
full_path=$(_append_root_path '.gitignore')
full_path=$(_prepend_root_path '.gitignore')

printf '%q\n' "$filename" >> "$full_path"
}
Expand Down Expand Up @@ -366,7 +367,7 @@ function _get_git_root_path {

# Relative paths:

function _append_root_path {
function _prepend_root_path {
# This function adds root path to any other path.

local path="$1" # required
Expand All @@ -380,11 +381,11 @@ function _append_root_path {

# if passed a name like 'filename.txt', returns a full path in the repo
# For #710: if we are in a subdir, fixup the path with the subdir
function _append_relative_root_path {
function _prepend_relative_root_path {
local path="$1" # required

local full_path
full_path=$(_append_root_path "$path")
full_path=$(_prepend_root_path "$path")

local subdir
subdir=$(git rev-parse --show-prefix) # get the subdir of repo, like "subdir/"
Expand All @@ -396,27 +397,27 @@ function _append_relative_root_path {
}

function _get_secrets_dir {
_append_root_path "${_SECRETS_DIR}"
_prepend_root_path "${_SECRETS_DIR}"
}


function _get_secrets_dir_keys {
_append_root_path "${_SECRETS_DIR_KEYS}"
_prepend_root_path "${_SECRETS_DIR_KEYS}"
}


function _get_secrets_dir_path {
_append_root_path "${_SECRETS_DIR_PATHS}"
_prepend_root_path "${_SECRETS_DIR_PATHS}"
}


function _get_secrets_dir_keys_trustdb {
_append_root_path "${_SECRETS_DIR_KEYS_TRUSTDB}"
_prepend_root_path "${_SECRETS_DIR_KEYS_TRUSTDB}"
}


function _get_secrets_dir_paths_mapping {
_append_root_path "${_SECRETS_DIR_PATHS_MAPPING}"
_prepend_root_path "${_SECRETS_DIR_PATHS_MAPPING}"
}


Expand Down Expand Up @@ -475,7 +476,7 @@ function _find_and_clean {
local root
root=$(_get_git_root_path)

# shellcheck disable=2086
# shellcheck disable=SC2086
find "$root" -path "$pattern" -type f -print0 | xargs -0 rm -f$verbose_opt
}

Expand Down Expand Up @@ -710,15 +711,15 @@ function _get_users_in_gpg_keyring {
result=$($SECRETS_GPG_COMMAND "${args[@]}" --no-permission-warning --list-public-keys --with-colon --fixed-list-mode | \
gawk -F: '$1=="uid"' )

local emails
emails=$(_extract_emails_from_gpg_output "$result")

# For #508 / #552: warn user if gpg indicates keys are one of:
# i=invalid, d=disabled, r=revoked, e=expired, n=not valid
# See https://github.com/gpg/gnupg/blob/master/doc/DETAILS#field-2---validity # for more on gpg 'validity codes'.
local invalid_lines
invalid_lines=$(echo "$result" | gawk -F: '$2=="i" || $2=="d" || $2=="r" || $2=="e" || $2=="n"')

local emails
emails=$(_extract_emails_from_gpg_output "$result")

local emails_with_invalid_keys
emails_with_invalid_keys=$(_extract_emails_from_gpg_output "$invalid_lines")

Expand Down
2 changes: 1 addition & 1 deletion src/_utils/_git_secret_tools_freebsd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# support for freebsd. Mostly the same as MacOS.


# shellcheck disable=1117
# shellcheck disable=SC1117
function __replace_in_file_freebsd {
sed -i.bak "s/^\($1[[:space:]]*=[[:space:]]*\).*\$/\1$2/" "$3"
}
Expand Down
2 changes: 1 addition & 1 deletion src/_utils/_git_secret_tools_linux.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash


# shellcheck disable=1117
# shellcheck disable=SC1117
function __replace_in_file_linux {
sed -i.bak "s/^\($1\s*=\s*\).*\$/\1$2/" "$3"
}
Expand Down
2 changes: 1 addition & 1 deletion src/_utils/_git_secret_tools_osx.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash


# shellcheck disable=1117
# shellcheck disable=SC1117
function __replace_in_file_osx {
sed -i.bak "s/^\($1[[:space:]]*=[[:space:]]*\).*\$/\1$2/" "$3"
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/git_secret_add.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function add {
local path # absolute path
local normalized_path # relative to the .git dir
normalized_path=$(_git_normalize_filename "$item")
path=$(_append_root_path "$normalized_path")
path=$(_prepend_root_path "$normalized_path")

# check that the file is not tracked
local in_git
Expand Down
2 changes: 1 addition & 1 deletion src/commands/git_secret_cat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function cat {
local path

filename=$(_get_record_filename "$line")
path=$(_append_relative_root_path "$filename") # this uses the _relative version because of #710
path=$(_prepend_relative_root_path "$filename") # this uses the _relative version because of #710

# The parameters are: filename, write-to-file, force, homedir, passphrase
_decrypt "$path" "0" "0" "$homedir" "$passphrase"
Expand Down
4 changes: 2 additions & 2 deletions src/commands/git_secret_changes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ function changes {
_abort "cannot find encrypted version of file: $filename"
fi
if [[ -n "$normalized_path" ]]; then
path=$(_append_root_path "$normalized_path")
path=$(_prepend_root_path "$normalized_path")
else
# Path was already normalized
path=$(_append_root_path "$filename")
path=$(_prepend_root_path "$filename")
fi

if [[ ! -f "$path" ]]; then
Expand Down
2 changes: 1 addition & 1 deletion src/commands/git_secret_clean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
function clean {
OPTIND=1

# shellcheck disable=2034
# shellcheck disable=SC2034
while getopts 'vh' opt; do
case "$opt" in
v) _SECRETS_VERBOSE=1;;
Expand Down
6 changes: 3 additions & 3 deletions src/commands/git_secret_hide.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# shellcheck disable=2016
# shellcheck disable=SC2016
AWK_FSDB_UPDATE_HASH='
BEGIN { FS=":"; OFS=":"; }
{
Expand Down Expand Up @@ -149,8 +149,8 @@ function hide {

local input_path
local output_path
input_path=$(_append_root_path "$filename")
output_path=$(_append_root_path "$encrypted_filename")
input_path=$(_prepend_root_path "$filename")
output_path=$(_prepend_root_path "$encrypted_filename")

# Checking that file is valid:
if [[ ! -f "$input_path" ]]; then
Expand Down
4 changes: 2 additions & 2 deletions src/commands/git_secret_init.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# shellcheck disable=2016
# shellcheck disable=SC2016
AWK_ADD_TO_GITIGNORE='
BEGIN {
cnt=0
Expand Down Expand Up @@ -33,7 +33,7 @@ function gitignore_add_pattern {
local gitignore_file_path

pattern="$1"
gitignore_file_path=$(_append_root_path '.gitignore')
gitignore_file_path=$(_prepend_root_path '.gitignore')

_maybe_create_gitignore
_gawk_inplace -v pattern="$pattern" "'$AWK_ADD_TO_GITIGNORE'" "$gitignore_file_path"
Expand Down
2 changes: 1 addition & 1 deletion src/commands/git_secret_remove.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function remove {
local path # absolute path
local normalized_path # relative to .git folder
normalized_path=$(_git_normalize_filename "$item")
path=$(_append_root_path "$normalized_path")
path=$(_prepend_root_path "$normalized_path")

# Checking if file exists:
if [[ ! -f "$path" ]]; then
Expand Down
2 changes: 1 addition & 1 deletion src/commands/git_secret_reveal.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function reveal {
local filename
local path
filename=$(_get_record_filename "$line")
path=$(_append_relative_root_path "$filename") # this uses the _relative version because of #710
path=$(_prepend_relative_root_path "$filename") # this uses the _relative version because of #710

if [[ "$filename" == *"$SECRETS_EXTENSION" ]]; then
_abort "cannot decrypt to secret version of file: $filename"
Expand Down
4 changes: 2 additions & 2 deletions src/commands/git_secret_tell.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# shellcheck disable=2016
# shellcheck disable=SC2016
AWK_GPG_KEY_CNT='
BEGIN { cnt=0; OFS=":"; FS=":"; }
flag=0; $1 == "pub" { cnt++ }
Expand Down Expand Up @@ -77,7 +77,7 @@ function tell {
start_key_cnt=$(get_gpg_key_count)
for email in "${emails[@]}"; do
_temporary_file # note that `_temporary_file` will export `temporary_filename` var.
# shellcheck disable=2154
# shellcheck disable=SC2154
local keyfile="$temporary_filename"

# 3>&- closes fd 3 for bats, see https://github.com/bats-core/bats-core#file-descriptor-3-read-this-if-bats-hangs
Expand Down
2 changes: 1 addition & 1 deletion src/version.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

# shellcheck disable=2034
# shellcheck disable=SC2034
GITSECRET_VERSION='0.5.0-alpha1'
12 changes: 6 additions & 6 deletions tests/test_changes.bats
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function teardown {

# Testing that output has both filename and changes:
local fullpath
fullpath=$(_append_root_path "$FILE_TO_HIDE")
fullpath=$(_prepend_root_path "$FILE_TO_HIDE")
[[ "$output" == *"changes in $fullpath"* ]]
[[ "$output" == *"hidden content юникод"* ]]
[[ "$output" == *"+$new_content"* ]]
Expand Down Expand Up @@ -102,7 +102,7 @@ function teardown {

# Testing that output has both filename and changes:
local fullpath
fullpath=$(_append_root_path "$FILE_TO_HIDE")
fullpath=$(_prepend_root_path "$FILE_TO_HIDE")
[[ "$output" == *"changes in $fullpath"* ]]
[[ "$output" == *"-$FILE_CONTENTS"* ]]
[[ "$output" == *"+$new_content"* ]]
Expand Down Expand Up @@ -137,13 +137,13 @@ function teardown {

# Testing that output has both filename and changes:
local fullpath
fullpath=$(_append_root_path "$FILE_TO_HIDE")
fullpath=$(_prepend_root_path "$FILE_TO_HIDE")

[[ "$output" == *"changes in $fullpath"* ]]
[[ "$output" == *"+$new_content"* ]]

local second_path
second_path=$(_append_root_path "$SECOND_FILE_TO_HIDE")
second_path=$(_prepend_root_path "$SECOND_FILE_TO_HIDE")
[[ "$output" == *"changes in $second_path"* ]]
[[ "$output" == *"+$second_new_content"* ]]
}
Expand All @@ -165,12 +165,12 @@ function teardown {

# Testing that output has both filename and changes:
local fullpath
fullpath=$(_append_root_path "$FILE_TO_HIDE")
fullpath=$(_prepend_root_path "$FILE_TO_HIDE")
[[ "$output" == *"changes in $fullpath"* ]]
[[ "$output" == *"+$new_content"* ]]

local second_path
second_path=$(_append_root_path "$SECOND_FILE_TO_HIDE")
second_path=$(_prepend_root_path "$SECOND_FILE_TO_HIDE")
[[ "$output" == *"changes in $second_path"* ]]
[[ "$output" == *"+$second_new_content"* ]]
}
Expand Down