From ffb8a7411126e07118cd8d55de849d9f5aa47b67 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Sat, 31 Dec 2016 22:34:20 -0800 Subject: [PATCH 1/2] Defensive `is_repo` `is_repo` defended Document return codes for `is_repo()` `is_repo` tested for 128,1,0 return values. --- automated install/basic-install.sh | 39 +++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index dc21ac97..bf85cca3 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -133,26 +133,40 @@ fi ####### FUNCTIONS ########## is_repo() { - # Use git to check if directory is currently under VCS, return the value + # Use git to check if directory is currently under VCS, return the value 128 + # if directory is not a repo. Return 1 if directory does not exist. local directory="${1}" - if [ -d $directory ]; then + local curdir + local rc + + curdir="${PWD}" + if [[ -d "${directory}" ]]; then # git -C is not used here to support git versions older than 1.8.4 - curdir=$PWD; cd $directory; git status --short &> /dev/null; rc=$?; cd $curdir - return $rc + cd "${directory}" + git status --short &> /dev/null || rc=$? else - # non-zero return code if directory does not exist OR is not a valid git repository - return 1 + # non-zero return code if directory does not exist + rc=1 fi + cd "${curdir}" + return "${rc:-0}" } make_repo() { local directory="${1}" local remoteRepo="${2}" - # Remove the non-repod interface and clone the interface - echo -n "::: Cloning $remoteRepo into $directory..." - rm -rf "${directory}" - git clone -q --depth 1 "${remoteRepo}" "${directory}" &> /dev/null - echo " done!" + + echo -n "::: Cloning ${remoteRepo} into ${directory}..." + # Clean out the directory if it exists for git to clone into + if [[ -d "${directory}" ]]; then + rm -rf "${directory}" + fi + if ! git clone -q --depth 1 "${remoteRepo}" "${directory}" &> /dev/null; then + return $? + else + echo " done!" + fi + return 0 } update_repo() { @@ -175,8 +189,9 @@ getGitFiles() { if is_repo "${directory}"; then update_repo "${directory}" else - make_repo "${directory}" "${remoteRepo}" + make_repo "${directory}" "${remoteRepo}" || { echo "!!! Unable to clone ${remoteRepo}"; return 1; } fi + return 0 } find_IPv4_information() { From ab9c8f48595abce8244b8220730341c7c70add62 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Sat, 31 Dec 2016 23:59:07 -0800 Subject: [PATCH 2/2] `make_repo` more defensive. Signed-off-by: Dan Schaper --- automated install/basic-install.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index bf85cca3..efc583ee 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -161,11 +161,8 @@ make_repo() { if [[ -d "${directory}" ]]; then rm -rf "${directory}" fi - if ! git clone -q --depth 1 "${remoteRepo}" "${directory}" &> /dev/null; then - return $? - else - echo " done!" - fi + git clone -q --depth 1 "${remoteRepo}" "${directory}" &> /dev/null || return $? + echo " done!" return 0 } @@ -189,7 +186,10 @@ getGitFiles() { if is_repo "${directory}"; then update_repo "${directory}" else - make_repo "${directory}" "${remoteRepo}" || { echo "!!! Unable to clone ${remoteRepo}"; return 1; } + make_repo "${directory}" "${remoteRepo}" || \ + { echo "!!! Unable to clone ${remoteRepo} into ${directory}"; \ + return 1; \ + } fi return 0 } @@ -1091,8 +1091,8 @@ main() { echo "::: --reconfigure passed to install script. Not downloading/updating local repos" else # Get Git files for Core and Admin - getGitFiles ${PI_HOLE_LOCAL_REPO} ${piholeGitUrl} - getGitFiles ${webInterfaceDir} ${webInterfaceGitUrl} + getGitFiles ${PI_HOLE_LOCAL_REPO} ${piholeGitUrl} || { echo "Unable to clone ${piholeGitUrl}"; exit 1; } + getGitFiles ${webInterfaceDir} ${webInterfaceGitUrl} || { echo "Unable to clone ${webInterfaceGitUrl}"; exit 1; } fi if [[ ${useUpdateVars} == false ]]; then