From 1a0be6f0ad5073d258d333c0502f534b0876aa94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 24 Dec 2015 03:52:33 +0100 Subject: [PATCH] Improve `git --version` git revision lookup It doesn't try to chdir into RBENV_ROOT anymore because that might be a location of an unrelated rbenv install that might have a different version than the current one that is installed e.g. via a package manager such as Homebrew. Now just tries the repo where the source files (`libexec/*`) are located, and if that isn't a valid rbenv repo, bail out early. --- libexec/rbenv---version | 11 ++++------- test/--version.bats | 33 +++++++-------------------------- 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/libexec/rbenv---version b/libexec/rbenv---version index 1254a2fd..f3fba63d 100755 --- a/libexec/rbenv---version +++ b/libexec/rbenv---version @@ -15,12 +15,9 @@ set -e version="0.4.0" git_revision="" -for source_dir in "${BASH_SOURCE%/*}" "$RBENV_ROOT"; do - if cd "$source_dir" 2>/dev/null && git remote -v 2>/dev/null | grep -q rbenv; then - git_revision="$(git describe --tags HEAD 2>/dev/null || true)" - git_revision="${git_revision#v}" - [ -z "$git_revision" ] || break - fi -done +if cd "${BASH_SOURCE%/*}" 2>/dev/null && git remote -v 2>/dev/null | grep -q rbenv; then + git_revision="$(git describe --tags HEAD 2>/dev/null || true)" + git_revision="${git_revision#v}" +fi echo "rbenv ${git_revision:-$version}" diff --git a/test/--version.bats b/test/--version.bats index 3d7ad5c7..556c7d64 100644 --- a/test/--version.bats +++ b/test/--version.bats @@ -2,22 +2,13 @@ load test_helper +export GIT_DIR="${RBENV_TEST_DIR}/.git" + setup() { mkdir -p "$HOME" git config --global user.name "Tester" git config --global user.email "tester@test.local" - - mkdir -p "${RBENV_TEST_DIR}/bin" - cat > "${RBENV_TEST_DIR}/bin/git" <&2 - exit 1 -else - exec $(which git) "\$@" -fi -CMD - chmod +x "${RBENV_TEST_DIR}/bin/git" + cd "$RBENV_TEST_DIR" } git_commit() { @@ -28,26 +19,21 @@ git_commit() { assert [ ! -e "$RBENV_ROOT" ] run rbenv---version assert_success - [[ $output == "rbenv 0."* ]] + [[ $output == "rbenv "?.?.? ]] } @test "doesn't read version from non-rbenv repo" { - mkdir -p "$RBENV_ROOT" - cd "$RBENV_ROOT" git init git remote add origin https://github.com/homebrew/homebrew.git git_commit git tag v1.0 - cd "$RBENV_TEST_DIR" run rbenv---version assert_success - [[ $output == "rbenv 0."* ]] + [[ $output == "rbenv "?.?.? ]] } @test "reads version from git repo" { - mkdir -p "$RBENV_ROOT" - cd "$RBENV_ROOT" git init git remote add origin https://github.com/rbenv/rbenv.git git_commit @@ -55,20 +41,15 @@ git_commit() { git_commit git_commit - cd "$RBENV_TEST_DIR" run rbenv---version - assert_success - [[ $output == "rbenv 0.4.1-2-g"* ]] + assert_success "rbenv 0.4.1-2-g$(git rev-parse --short HEAD)" } @test "prints default version if no tags in git repo" { - mkdir -p "$RBENV_ROOT" - cd "$RBENV_ROOT" git init git remote add origin https://github.com/rbenv/rbenv.git git_commit - cd "$RBENV_TEST_DIR" run rbenv---version - [[ $output == "rbenv 0."* ]] + [[ $output == "rbenv "?.?.? ]] }