From 82f5bd7c9c9118f2e5b623860beee1c76e03cec0 Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Mon, 9 Mar 2015 11:48:49 +0000 Subject: prevent bad grep usage when no branch_name --- git-base.sh | 14 ++++++++------ test-branches.sh | 6 +++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/git-base.sh b/git-base.sh index 40f733b..2debafa 100755 --- a/git-base.sh +++ b/git-base.sh @@ -123,12 +123,14 @@ readable_branch_name() { remote_branch_name() { local localRef="$(branch_name)" - local remoteBranch="$(git for-each-ref --format='%(upstream:short)' refs/heads $localRef 2>/dev/null | grep $localRef)" - if [[ -n $remoteBranch ]]; then - echo $remoteBranch - return 0 - else - return 1 + if [[ -n "$localRef" ]]; then + local remoteBranch="$(git for-each-ref --format='%(upstream:short)' refs/heads $localRef 2>/dev/null | grep $localRef)" + if [[ -n $remoteBranch ]]; then + echo $remoteBranch + return 0 + else + return 1 + fi fi } diff --git a/test-branches.sh b/test-branches.sh index 40c5a50..4e52242 100755 --- a/test-branches.sh +++ b/test-branches.sh @@ -83,16 +83,16 @@ test_remote_branch_name_quiet_when_not_in_repo() { debug_output="$( { - set -x output="$( remote_branch_name; )" - set +x } 2>&1 echo "$output" )" - usages="$(echo "$debug_output" | grep 'fatal: ' | wc -l)" + usages="$(echo "$debug_output" | grep -E "(usage|fatal):" | wc -l)" + + echo "$debug_output" assertEquals " 0" "$usages" -- cgit v1.2.3