From 4466c202fb61c0f11788d8eee68ea94354fe55ef Mon Sep 17 00:00:00 2001 From: "tlimoncelli@stackexchange.com" Date: Tue, 16 Jun 2015 17:11:22 -0400 Subject: [PATCH] Revert "Merge pull request #89 from fidian/optimize-vcs-type" This reverts commit 082809bab2f27926424d37b07dce45dfe7fcbd41, reversing changes made to 33429b3ca62c6b0b01a6ab2a5c61d54eb7fa8a6c. --- 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, 62 insertions(+), 37 deletions(-) diff --git a/bin/_blackbox_common.sh b/bin/_blackbox_common.sh index 4ae0aec..17a71a4 100755 --- a/bin/_blackbox_common.sh +++ b/bin/_blackbox_common.sh @@ -17,36 +17,45 @@ : "${EDITOR:=vi}" ; -# 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 +# 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 - 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. + 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=$(_determine_vcs_base_and_type) KEYRINGDIR="$REPOBASE/$BLACKBOXDATA" BB_ADMINS_FILE="blackbox-admins.txt" BB_ADMINS="${KEYRINGDIR}/${BB_ADMINS_FILE}" @@ -82,6 +91,7 @@ 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 @@ -269,6 +279,10 @@ 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" } @@ -331,9 +345,18 @@ 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_$VCS_TYPE "$@" + is_in_$(which_vcs) "$@" } # Mercurial function is_in_hg() { @@ -387,7 +410,7 @@ function is_in_unknown() { # Add a file to the repo (but don't commit it). function vcs_add() { - vcs_add_$VCS_TYPE "$@" + vcs_add_$(which_vcs) "$@" } # Mercurial function vcs_add_hg() { @@ -413,7 +436,7 @@ function vcs_add_unknown() { # Commit a file to the repo function vcs_commit() { - vcs_commit_$VCS_TYPE "$@" + vcs_commit_$(which_vcs) "$@" } # Mercurial function vcs_commit_hg() { @@ -440,7 +463,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_$VCS_TYPE "$@" + vcs_remove_$(which_vcs) "$@" } # Mercurial function vcs_remove_hg() { @@ -468,7 +491,7 @@ function vcs_remove_unknown() { function vcs_ignore() { local file for file in "$@"; do - vcs_ignore_$VCS_TYPE "$file" + vcs_ignore_$(which_vcs) "$file" done } # Mercurial @@ -509,7 +532,7 @@ function vcs_ignore_generic_file() { function vcs_notice() { local file for file in "$@"; do - vcs_notice_$VCS_TYPE "$file" + vcs_notice_$(which_vcs) "$file" done } # Mercurial diff --git a/bin/blackbox_deregister_file b/bin/blackbox_deregister_file index c1a38fb..f4ea76e 100755 --- a/bin/blackbox_deregister_file +++ b/bin/blackbox_deregister_file @@ -9,6 +9,7 @@ set -e blackbox_home=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) source "${blackbox_home}/_blackbox_common.sh" +_determine_vcs_base_and_type unencrypted_file=$(get_unencrypted_filename "$1") encrypted_file=$(get_encrypted_filename "$1") @@ -31,4 +32,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 " $VCS_TYPE push" +echo " $(which_vcs) push" diff --git a/bin/blackbox_register_new_file b/bin/blackbox_register_new_file index 79a1f7b..9bd9508 100755 --- a/bin/blackbox_register_new_file +++ b/bin/blackbox_register_new_file @@ -47,4 +47,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 " $VCS_TYPE push" +echo " $(which_vcs) push" diff --git a/bin/blackbox_update_all_files b/bin/blackbox_update_all_files index 74142f3..041140f 100755 --- a/bin/blackbox_update_all_files +++ b/bin/blackbox_update_all_files @@ -60,6 +60,7 @@ 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 " $VCS_TYPE push" +echo " ${VCSCMD} push"