11#! /usr/bin/env bash
2+ set -euo pipefail
23
34#
45# transcrypt - https://github.com/elasticdog/transcrypt
@@ -151,7 +152,7 @@ validate_cipher() {
151152 printf ' "%s" is not a valid cipher; choose one of the following:\n\n' " $cipher "
152153 $list_cipher_commands | column -c 80
153154 printf ' \n'
154- unset cipher
155+ cipher= ' '
155156 else
156157 die 1 ' "%s" is not a valid cipher; see `%s`' " $cipher " " $list_cipher_commands "
157158 fi
@@ -199,7 +200,7 @@ get_password() {
199200 else
200201 printf ' Password: '
201202 read -r password
202- [[ ! $password ]] && printf ' no password was specified\n'
203+ [[ $password ]] || printf ' no password was specified\n'
203204 fi
204205 done
205206}
@@ -209,7 +210,7 @@ confirm_configuration() {
209210 local answer
210211
211212 printf ' \nRepository metadata:\n\n'
212- [[ $REPO ]] && printf ' GIT_WORK_TREE: %s\n' " $REPO "
213+ [[ ! $REPO ]] || printf ' GIT_WORK_TREE: %s\n' " $REPO "
213214 printf ' GIT_DIR: %s\n' " $GIT_DIR "
214215 printf ' GIT_ATTRIBUTES: %s\n\n' " $GIT_ATTRIBUTES "
215216 printf ' The following configuration will be saved:\n\n'
@@ -232,7 +233,7 @@ confirm_rekey() {
232233 local answer
233234
234235 printf ' \nRepository metadata:\n\n'
235- [[ $REPO ]] && printf ' GIT_WORK_TREE: %s\n' " $REPO "
236+ [[ ! $REPO ]] || printf ' GIT_WORK_TREE: %s\n' " $REPO "
236237 printf ' GIT_DIR: %s\n' " $GIT_DIR "
237238 printf ' GIT_ATTRIBUTES: %s\n\n' " $GIT_ATTRIBUTES "
238239 printf ' The following configuration will be saved:\n\n'
@@ -267,7 +268,7 @@ stage_rekeyed_files() {
267268
268269# save helper scripts under the repository's git directory
269270save_helper_scripts () {
270- [[ ! -d " ${GIT_DIR} /crypt " ]] && mkdir " ${GIT_DIR} /crypt"
271+ mkdir -p " ${GIT_DIR} /crypt"
271272
272273 # The `decryption -> encryption` process on an unchanged file must be
273274 # deterministic for everything to work transparently. To do that, the same
@@ -362,7 +363,7 @@ display_configuration() {
362363
363364 printf ' The current repository was configured using transcrypt version %s\n' " $CONFIGURED "
364365 printf ' and has the following configuration:\n\n'
365- [[ $REPO ]] && printf ' GIT_WORK_TREE: %s\n' " $REPO "
366+ [[ ! $REPO ]] || printf ' GIT_WORK_TREE: %s\n' " $REPO "
366367 printf ' GIT_DIR: %s\n' " $GIT_DIR "
367368 printf ' GIT_ATTRIBUTES: %s\n\n' " $GIT_ATTRIBUTES "
368369 printf ' CIPHER: %s\n' " $current_cipher "
@@ -373,15 +374,15 @@ display_configuration() {
373374
374375# remove transcrypt-related settings from the repository's git config
375376clean_gitconfig () {
376- git config --remove-section transcrypt 2> /dev/null
377- git config --remove-section filter.crypt 2> /dev/null
378- git config --remove-section diff.crypt 2> /dev/null
377+ git config --remove-section transcrypt 2> /dev/null || true
378+ git config --remove-section filter.crypt 2> /dev/null || true
379+ git config --remove-section diff.crypt 2> /dev/null || true
379380 git config --unset merge.renormalize
380381
381382 # remove the merge section if it's now empty
382383 local merge_values=$( git config --get-regex --local ' merge\..*' )
383384 if [[ ! $merge_values ]]; then
384- git config --remove-section merge 2> /dev/null
385+ git config --remove-section merge 2> /dev/null || true
385386 fi
386387}
387388
@@ -454,9 +455,9 @@ uninstall_transcrypt() {
454455
455456 # remove helper scripts
456457 for script in {clean,smudge,textconv}; do
457- [[ -f " ${GIT_DIR} /crypt/${script} " ]] && rm " ${GIT_DIR} /crypt/${script} "
458+ [[ ! -f " ${GIT_DIR} /crypt/${script} " ]] || rm " ${GIT_DIR} /crypt/${script} "
458459 done
459- [[ -d " ${GIT_DIR} /crypt" ]] && rmdir " ${GIT_DIR} /crypt"
460+ [[ ! -d " ${GIT_DIR} /crypt" ]] || rmdir " ${GIT_DIR} /crypt"
460461
461462 # touch all encrypted files to prevent stale stat info
462463 local encrypted_files=$( git ls-crypt)
@@ -471,7 +472,7 @@ uninstall_transcrypt() {
471472 # remove the alias section if it's now empty
472473 local alias_values=$( git config --get-regex --local ' alias\..*' )
473474 if [[ ! $alias_values ]]; then
474- git config --remove-section alias 2> /dev/null
475+ git config --remove-section alias 2> /dev/null || true
475476 fi
476477
477478 # remove any defined crypt patterns in gitattributes
@@ -535,7 +536,7 @@ export_gpg() {
535536
536537 local current_cipher=$( git config --get --local transcrypt.cipher)
537538 local current_password=$( git config --get --local transcrypt.password)
538- [[ ! -d " ${GIT_DIR} /crypt " ]] && mkdir " ${GIT_DIR} /crypt"
539+ mkdir -p " ${GIT_DIR} /crypt"
539540
540541 local gpg_encrypt_cmd=" gpg --batch --recipient $gpg_recipient --trust-model always --yes --armor --quiet --encrypt -"
541542 printf ' password=%s\ncipher=%s\n' " $current_password " " $current_cipher " | $gpg_encrypt_cmd > " ${GIT_DIR} /crypt/${gpg_recipient} .asc"
@@ -558,8 +559,8 @@ import_gpg() {
558559 path=" $gpg_import_file "
559560 fi
560561
561- local configuration
562- local safety_counter # fix for intermittent 'no secret key' decryption failures
562+ local configuration= ' '
563+ local safety_counter=0 # fix for intermittent 'no secret key' decryption failures
563564 while [[ ! $configuration ]]
564565 do
565566 configuration=$( gpg --batch --quiet --decrypt " $path " )
@@ -714,7 +715,7 @@ requires_existing_config=''
714715requires_clean_repo=' true'
715716
716717# parse command line options
717- while [[ " $1 " != ' ' ]]
718+ while [[ " ${1 :- } " != ' ' ]]
718719do
719720 case $1 in
720721 -c | --cipher)
732733 password=${1#* =}
733734 ;;
734735 -y | --yes)
735- unset interactive
736+ interactive= ' '
736737 ;;
737738 -d | --display)
738739 display_config=' true'
739740 requires_existing_config=' true'
740- unset requires_clean_repo
741+ requires_clean_repo= ' '
741742 ;;
742743 -r | --rekey)
743744 rekey=' true'
748749 requires_existing_config=' true'
749750 ;;
750751 -F | --force)
751- unset requires_clean_repo
752+ requires_clean_repo= ' '
752753 ;;
753754 -u | --uninstall)
754755 uninstall=' true'
755756 requires_existing_config=' true'
756- unset requires_clean_repo
757+ requires_clean_repo= ' '
757758 ;;
758759 -l | --list)
759760 list_files
772773 -e | --export-gpg)
773774 gpg_recipient=$2
774775 requires_existing_config=' true'
775- unset requires_clean_repo
776+ requires_clean_repo= ' '
776777 shift
777778 ;;
778779 --export-gpg=* )
779780 gpg_recipient=${1#* =}
780781 requires_existing_config=' true'
781- unset requires_clean_repo
782+ requires_clean_repo= ' '
782783 ;;
783784 -i | --import-gpg)
784785 gpg_import_file=$2
0 commit comments