From cc1d7a1851131bcbadcda72e958b9c445a9606a2 Mon Sep 17 00:00:00 2001 From: Tyler Akins Date: Tue, 16 Jun 2015 14:02:54 -0500 Subject: [PATCH 1/3] Removing the multiple calls to determine VCS type This removed the subshell from _determine_vcs_base_and_type so it can set environment variables. Because this always runs at the beginning of the scripts, there's no need to do checking if REPOBASE is unset or if VCS_TYPE is not yet determined, thus I simplified one function and eliminated which_vcs. Conflicts: bin/blackbox_deregister_file I found this easier to just cherry pick since there was a merge and the merge was reverted. --- bin/_blackbox_common.sh | 91 +++++++++++++--------------------- bin/blackbox_deregister_file | 3 +- bin/blackbox_register_new_file | 2 +- bin/blackbox_update_all_files | 3 +- 4 files changed, 37 insertions(+), 62 deletions(-) diff --git a/bin/_blackbox_common.sh b/bin/_blackbox_common.sh index 9256cdc..85ef6f1 100755 --- a/bin/_blackbox_common.sh +++ b/bin/_blackbox_common.sh @@ -23,45 +23,36 @@ source "${0%/*}"/_stack_lib.sh : "${EDITOR:=vi}" ; -# Outputs a string that is the base directory of this VCS repo. -# By side-effect, sets the variable VCS_TYPE to either 'git', 'hg', -# 'svn' or 'unknown'. -function _determine_vcs_base_and_type() { - if git rev-parse --show-toplevel 2>/dev/null ; then - VCS_TYPE=git - elif [ -d ".svn" ] ; then - #find topmost dir with .svn sub-dir - parent="" - grandparent="." - mydir="$(pwd)" - while [ -d "$grandparent/.svn" ]; do - parent=$grandparent - grandparent="$parent/.." - done +# Set REPOBASE to the top of the repository +# Set VCS_TYPE to 'git', 'hg', 'svn' or 'unknown' +if git rev-parse --show-toplevel >/dev/null 2>&1 ; then + VCS_TYPE=git + REPOBASE=$(git rev-parse --show-toplevel) +elif [ -d ".svn" ] ; then + # Find topmost dir with .svn sub-dir + parent="" + grandparent="." + while [ -d "$grandparent/.svn" ]; do + parent=$grandparent + grandparent="$parent/.." + done - if [ ! -z "$parent" ]; then - cd "$parent" - pwd - else - exit 1 - fi - cd "$mydir" - VCS_TYPE=svn - elif hg root 2>/dev/null ; then - # NOTE: hg has to be tested last because it always "succeeds". - VCS_TYPE=hg - else - # We aren't in a repo at all. Assume the cwd is the root - # of the tree. - echo . - VCS_TYPE=unknown - fi - export VCS_TYPE - # FIXME: Verify this function by checking for .hg or .git - # after determining what we believe to be the answer. -} + REPOBASE=$(cd "$parent" ; pwd) + VCS_TYPE=svn +elif hg root >/dev/null 2>&1 ; then + # NOTE: hg has to be tested last because it always "succeeds". + VCS_TYPE=hg + REPOBASE=$(hg root 2>/dev/null) +else + # We aren't in a repo at all. Assume the cwd is the root + # of the tree. + VCS_TYPE=unknown + REPOBASE="$(pwd)" +fi +export VCS_TYPE +# FIXME: Verify this function by checking for .hg or .git +# after determining what we believe to be the answer. -REPOBASE=$(_determine_vcs_base_and_type) KEYRINGDIR="$REPOBASE/$BLACKBOXDATA" BB_ADMINS_FILE="blackbox-admins.txt" BB_ADMINS="${KEYRINGDIR}/${BB_ADMINS_FILE}" @@ -97,7 +88,6 @@ function fail_if_not_exists() { # Exit we we aren't in a VCS repo. function fail_if_not_in_repo() { - _determine_vcs_base_and_type if [[ $VCS_TYPE = "unknown" ]]; then echo "ERROR: This must be run in a VCS repo: git, hg, or svn." >&2 echo Exiting... >&2 @@ -285,10 +275,6 @@ function enumerate_subdirs() { # chdir to the base of the repo. function change_to_vcs_root() { - if [[ $REPOBASE = '' ]]; then - echo 'ERROR: _determine_vcs_base_and_type failed to set REPOBASE.' - exit 1 - fi cd "$REPOBASE" } @@ -351,18 +337,9 @@ function md5sum_file() { # Abstract the difference between git and hg: # -# Are we in git, hg, or unknown repo? -function which_vcs() { - if [[ $VCS_TYPE = '' ]]; then - _determine_vcs_base_and_type >/dev/null - fi - echo "$VCS_TYPE" -} - - # Is this file in the current repo? function is_in_vcs() { - is_in_$(which_vcs) "$@" + is_in_$VCS_TYPE "$@" } # Mercurial function is_in_hg() { @@ -416,7 +393,7 @@ function is_in_unknown() { # Add a file to the repo (but don't commit it). function vcs_add() { - vcs_add_$(which_vcs) "$@" + vcs_add_$VCS_TYPE "$@" } # Mercurial function vcs_add_hg() { @@ -442,7 +419,7 @@ function vcs_add_unknown() { # Commit a file to the repo function vcs_commit() { - vcs_commit_$(which_vcs) "$@" + vcs_commit_$VCS_TYPE "$@" } # Mercurial function vcs_commit_hg() { @@ -469,7 +446,7 @@ function vcs_commit_unknown() { # Remove file from repo, even if it was deleted locally already. # If it doesn't exist yet in the repo, it should be a no-op. function vcs_remove() { - vcs_remove_$(which_vcs) "$@" + vcs_remove_$VCS_TYPE "$@" } # Mercurial function vcs_remove_hg() { @@ -497,7 +474,7 @@ function vcs_remove_unknown() { function vcs_ignore() { local file for file in "$@"; do - vcs_ignore_$(which_vcs) "$file" + vcs_ignore_$VCS_TYPE "$file" done } # Mercurial @@ -538,7 +515,7 @@ function vcs_ignore_generic_file() { function vcs_notice() { local file for file in "$@"; do - vcs_notice_$(which_vcs) "$file" + vcs_notice_$VCS_TYPE "$file" done } # Mercurial diff --git a/bin/blackbox_deregister_file b/bin/blackbox_deregister_file index a092bc1..404f3a4 100755 --- a/bin/blackbox_deregister_file +++ b/bin/blackbox_deregister_file @@ -8,7 +8,6 @@ set -e source "${0%/*}/_blackbox_common.sh" -_determine_vcs_base_and_type unencrypted_file=$(get_unencrypted_filename "$1") encrypted_file=$(get_encrypted_filename "$1") @@ -31,4 +30,4 @@ vcs_remove "$BB_FILES" vcs_commit "Removing from blackbox: ${unencrypted_file}" echo "========== UPDATING VCS: DONE" echo "Local repo updated. Please push when ready." -echo " $(which_vcs) push" +echo " $VCS_TYPE push" diff --git a/bin/blackbox_register_new_file b/bin/blackbox_register_new_file index f222ff9..2bb94a1 100755 --- a/bin/blackbox_register_new_file +++ b/bin/blackbox_register_new_file @@ -46,4 +46,4 @@ vcs_add "$BB_FILES" "$encrypted_file" vcs_commit "registered in blackbox: ${unencrypted_file}" "$BB_FILES" "$encrypted_file" echo "========== UPDATING VCS: DONE" echo "Local repo updated. Please push when ready." -echo " $(which_vcs) push" +echo " $VCS_TYPE push" diff --git a/bin/blackbox_update_all_files b/bin/blackbox_update_all_files index e244a1f..564f002 100755 --- a/bin/blackbox_update_all_files +++ b/bin/blackbox_update_all_files @@ -59,7 +59,6 @@ while IFS= read <&99 -r unencrypted_file; do done 99<"$BB_FILES" vcs_commit 'Re-encrypted keys' -VCSCMD=$(which_vcs) echo '========== DONE.' echo 'Likely next step:' -echo " ${VCSCMD} push" +echo " $VCS_TYPE push" From 64e019c8e69cfdd463a4ac6ac72c6d300ccbb9a1 Mon Sep 17 00:00:00 2001 From: "tlimoncelli@stackexchange.com" Date: Sat, 20 Jun 2015 19:10:02 +0000 Subject: [PATCH 2/3] Remove call to _determine_vcs_base_and_type. --- bin/blackbox_edit_end | 2 -- bin/blackbox_initialize | 2 -- 2 files changed, 4 deletions(-) diff --git a/bin/blackbox_edit_end b/bin/blackbox_edit_end index 1d6cd3a..80c1891 100755 --- a/bin/blackbox_edit_end +++ b/bin/blackbox_edit_end @@ -19,8 +19,6 @@ fail_if_keychain_has_secrets encrypt_file "$unencrypted_file" "$encrypted_file" shred_file "$unencrypted_file" -_determine_vcs_base_and_type - echo ========== UPDATED '"'$encrypted_file'"' echo "Likely next step:" echo " $VCS_TYPE commit -m\"${encrypted_file} updated\" \"$encrypted_file\"" diff --git a/bin/blackbox_initialize b/bin/blackbox_initialize index f661e94..271e911 100755 --- a/bin/blackbox_initialize +++ b/bin/blackbox_initialize @@ -11,8 +11,6 @@ set -e source "${0%/*}/_blackbox_common.sh" -_determine_vcs_base_and_type # Sets VCS_TYPE - if [[ $1 != 'yes' ]]; then read -r -p "Enable blackbox for this $VCS_TYPE repo? (yes/no) " ans if [[ $ans = 'no' || $ans = 'n' || $ans = '' ]]; then From efe7bbc8cd9a775a246094a9ed4ea8a690b781a5 Mon Sep 17 00:00:00 2001 From: "tlimoncelli@stackexchange.com" Date: Sat, 20 Jun 2015 19:10:17 +0000 Subject: [PATCH 3/3] Removing the multiple calls to determine VCS type. * Credit goes to fidian@rumkin.com --- bin/_blackbox_common.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/_blackbox_common.sh b/bin/_blackbox_common.sh index 85ef6f1..1aaccdd 100755 --- a/bin/_blackbox_common.sh +++ b/bin/_blackbox_common.sh @@ -25,7 +25,7 @@ source "${0%/*}"/_stack_lib.sh # Set REPOBASE to the top of the repository # Set VCS_TYPE to 'git', 'hg', 'svn' or 'unknown' -if git rev-parse --show-toplevel >/dev/null 2>&1 ; then +if which >/dev/null 2>/dev/null git && git rev-parse --show-toplevel >/dev/null 2>&1 ; then VCS_TYPE=git REPOBASE=$(git rev-parse --show-toplevel) elif [ -d ".svn" ] ; then @@ -39,7 +39,7 @@ elif [ -d ".svn" ] ; then REPOBASE=$(cd "$parent" ; pwd) VCS_TYPE=svn -elif hg root >/dev/null 2>&1 ; then +elif which >/dev/null 2>/dev/null hg && hg root >/dev/null 2>&1 ; then # NOTE: hg has to be tested last because it always "succeeds". VCS_TYPE=hg REPOBASE=$(hg root 2>/dev/null) @@ -240,10 +240,10 @@ function shred_file() { local OPT name="$1" - if which shred >/dev/null ; then + if which shred >/dev/null 2>/dev/null ; then CMD=shred OPT=-u - elif which srm >/dev/null ; then + elif which srm >/dev/null 2>/dev/null ; then #NOTE: srm by default uses 35-pass Gutmann algorithm CMD=srm OPT=-f