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

use strict bash #53

Merged
merged 3 commits into from
May 26, 2019
Merged
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
47 changes: 24 additions & 23 deletions transcrypt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
set -euo pipefail

#
# transcrypt - https://github.com/elasticdog/transcrypt
Expand Down Expand Up @@ -151,7 +152,7 @@ validate_cipher() {
printf '"%s" is not a valid cipher; choose one of the following:\n\n' "$cipher"
$list_cipher_commands | column -c 80
printf '\n'
unset cipher
cipher=''
else
die 1 '"%s" is not a valid cipher; see `%s`' "$cipher" "$list_cipher_commands"
fi
Expand Down Expand Up @@ -199,7 +200,7 @@ get_password() {
else
printf 'Password: '
read -r password
[[ ! $password ]] && printf 'no password was specified\n'
[[ $password ]] || printf 'no password was specified\n'
fi
done
}
Expand All @@ -209,7 +210,7 @@ confirm_configuration() {
local answer

printf '\nRepository metadata:\n\n'
[[ $REPO ]] && printf ' GIT_WORK_TREE: %s\n' "$REPO"
[[ ! $REPO ]] || printf ' GIT_WORK_TREE: %s\n' "$REPO"
printf ' GIT_DIR: %s\n' "$GIT_DIR"
printf ' GIT_ATTRIBUTES: %s\n\n' "$GIT_ATTRIBUTES"
printf 'The following configuration will be saved:\n\n'
Expand All @@ -232,7 +233,7 @@ confirm_rekey() {
local answer

printf '\nRepository metadata:\n\n'
[[ $REPO ]] && printf ' GIT_WORK_TREE: %s\n' "$REPO"
[[ ! $REPO ]] || printf ' GIT_WORK_TREE: %s\n' "$REPO"
printf ' GIT_DIR: %s\n' "$GIT_DIR"
printf ' GIT_ATTRIBUTES: %s\n\n' "$GIT_ATTRIBUTES"
printf 'The following configuration will be saved:\n\n'
Expand Down Expand Up @@ -267,7 +268,7 @@ stage_rekeyed_files() {

# save helper scripts under the repository's git directory
save_helper_scripts() {
[[ ! -d "${GIT_DIR}/crypt" ]] && mkdir "${GIT_DIR}/crypt"
mkdir -p "${GIT_DIR}/crypt"

# The `decryption -> encryption` process on an unchanged file must be
# deterministic for everything to work transparently. To do that, the same
Expand Down Expand Up @@ -362,7 +363,7 @@ display_configuration() {

printf 'The current repository was configured using transcrypt version %s\n' "$CONFIGURED"
printf 'and has the following configuration:\n\n'
[[ $REPO ]] && printf ' GIT_WORK_TREE: %s\n' "$REPO"
[[ ! $REPO ]] || printf ' GIT_WORK_TREE: %s\n' "$REPO"
printf ' GIT_DIR: %s\n' "$GIT_DIR"
printf ' GIT_ATTRIBUTES: %s\n\n' "$GIT_ATTRIBUTES"
printf ' CIPHER: %s\n' "$current_cipher"
Expand All @@ -373,15 +374,15 @@ display_configuration() {

# remove transcrypt-related settings from the repository's git config
clean_gitconfig() {
git config --remove-section transcrypt 2> /dev/null
git config --remove-section filter.crypt 2> /dev/null
git config --remove-section diff.crypt 2> /dev/null
git config --remove-section transcrypt 2> /dev/null || true
git config --remove-section filter.crypt 2> /dev/null || true
git config --remove-section diff.crypt 2> /dev/null || true
git config --unset merge.renormalize

# remove the merge section if it's now empty
local merge_values=$(git config --get-regex --local 'merge\..*')
if [[ ! $merge_values ]]; then
git config --remove-section merge 2> /dev/null
git config --remove-section merge 2> /dev/null || true
fi
}

Expand Down Expand Up @@ -454,9 +455,9 @@ uninstall_transcrypt() {

# remove helper scripts
for script in {clean,smudge,textconv}; do
[[ -f "${GIT_DIR}/crypt/${script}" ]] && rm "${GIT_DIR}/crypt/${script}"
[[ ! -f "${GIT_DIR}/crypt/${script}" ]] || rm "${GIT_DIR}/crypt/${script}"
done
[[ -d "${GIT_DIR}/crypt" ]] && rmdir "${GIT_DIR}/crypt"
[[ ! -d "${GIT_DIR}/crypt" ]] || rmdir "${GIT_DIR}/crypt"

# touch all encrypted files to prevent stale stat info
local encrypted_files=$(git ls-crypt)
Expand All @@ -471,7 +472,7 @@ uninstall_transcrypt() {
# remove the alias section if it's now empty
local alias_values=$(git config --get-regex --local 'alias\..*')
if [[ ! $alias_values ]]; then
git config --remove-section alias 2> /dev/null
git config --remove-section alias 2> /dev/null || true
fi

# remove any defined crypt patterns in gitattributes
Expand Down Expand Up @@ -535,7 +536,7 @@ export_gpg() {

local current_cipher=$(git config --get --local transcrypt.cipher)
local current_password=$(git config --get --local transcrypt.password)
[[ ! -d "${GIT_DIR}/crypt" ]] && mkdir "${GIT_DIR}/crypt"
mkdir -p "${GIT_DIR}/crypt"

local gpg_encrypt_cmd="gpg --batch --recipient $gpg_recipient --trust-model always --yes --armor --quiet --encrypt -"
printf 'password=%s\ncipher=%s\n' "$current_password" "$current_cipher" | $gpg_encrypt_cmd > "${GIT_DIR}/crypt/${gpg_recipient}.asc"
Expand All @@ -558,8 +559,8 @@ import_gpg() {
path="$gpg_import_file"
fi

local configuration
local safety_counter # fix for intermittent 'no secret key' decryption failures
local configuration=''
local safety_counter=0 # fix for intermittent 'no secret key' decryption failures
while [[ ! $configuration ]]
do
configuration=$(gpg --batch --quiet --decrypt "$path")
Expand Down Expand Up @@ -714,7 +715,7 @@ requires_existing_config=''
requires_clean_repo='true'

# parse command line options
while [[ "$1" != '' ]]
while [[ "${1:-}" != '' ]]
do
case $1 in
-c | --cipher)
Expand All @@ -732,12 +733,12 @@ do
password=${1#*=}
;;
-y | --yes)
unset interactive
interactive=''
;;
-d | --display)
display_config='true'
requires_existing_config='true'
unset requires_clean_repo
requires_clean_repo=''
;;
-r | --rekey)
rekey='true'
Expand All @@ -748,12 +749,12 @@ do
requires_existing_config='true'
;;
-F | --force)
unset requires_clean_repo
requires_clean_repo=''
;;
-u | --uninstall)
uninstall='true'
requires_existing_config='true'
unset requires_clean_repo
requires_clean_repo=''
;;
-l | --list)
list_files
Expand All @@ -772,13 +773,13 @@ do
-e | --export-gpg)
gpg_recipient=$2
requires_existing_config='true'
unset requires_clean_repo
requires_clean_repo=''
shift
;;
--export-gpg=*)
gpg_recipient=${1#*=}
requires_existing_config='true'
unset requires_clean_repo
requires_clean_repo=''
;;
-i | --import-gpg)
gpg_import_file=$2
Expand Down