From a593511c8db27990d74fe792dbe6db6aabbeeb1d Mon Sep 17 00:00:00 2001 From: Lucas Charles Date: Mon, 5 Apr 2021 09:25:22 -0700 Subject: [PATCH] feat: Add versioning to plugin add command Allows user to specify version along with URL when adding plugin to lock to a specific version instead of always relying on HEAD. Relates to https://github.com/asdf-vm/asdf/pull/234 --- lib/commands/command-plugin-add.bash | 20 ++++++++++++++++---- test/plugin_add_command.bats | 14 ++++++++++++++ test/test_helpers.bash | 7 +++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/lib/commands/command-plugin-add.bash b/lib/commands/command-plugin-add.bash index 68f30fa37..82a17b9cd 100644 --- a/lib/commands/command-plugin-add.bash +++ b/lib/commands/command-plugin-add.bash @@ -1,12 +1,13 @@ # -*- sh -*- plugin_add_command() { - if [[ $# -lt 1 || $# -gt 2 ]]; then - display_error "usage: asdf plugin add []" + if [[ $# -lt 1 || $# -gt 3 ]]; then + display_error "usage: asdf plugin add [] []" exit 1 fi local plugin_name=$1 + local plugin_ref="" if ! printf "%s" "$plugin_name" | grep --quiet --extended-regexp "^[a-zA-Z0-9_-]+$"; then display_error "$plugin_name is invalid. Name must match regex ^[a-zA-Z0-9_-]+$" @@ -21,6 +22,11 @@ plugin_add_command() { source_url=$(get_plugin_source_url "$plugin_name") fi + if [ -n "$3" ]; then + plugin_ref="$3" + plugin_name=$plugin_name-$3 + fi + if [ -z "$source_url" ]; then display_error "plugin $plugin_name not found in repository" exit 1 @@ -38,8 +44,14 @@ plugin_add_command() { asdf_run_hook "pre_asdf_plugin_add" "$plugin_name" asdf_run_hook "pre_asdf_plugin_add_${plugin_name}" - if ! git clone -q "$source_url" "$plugin_path"; then - exit 1 + if [ -z "$plugin_ref" ]; then + if ! git clone -q "$source_url" "$plugin_path"; then + exit 1 + fi + else + if ! git clone -q -b "$plugin_ref" "$source_url" "$plugin_path"; then + exit 1 + fi fi if [ -f "${plugin_path}/bin/post-plugin-add" ]; then diff --git a/test/plugin_add_command.bats b/test/plugin_add_command.bats index 021e38508..05b1539af 100644 --- a/test/plugin_add_command.bats +++ b/test/plugin_add_command.bats @@ -52,6 +52,20 @@ teardown() { [ "$output" = "dummy" ] } +@test "plugin_add command with URL and git-ref specified adds a plugin using repo" { + install_mock_plugin_repo_with_ref "dummy" + + run asdf plugin-add "dummy" "${BASE_DIR}/repo-dummy" "tagname" + [ "$status" -eq 0 ] + + repo_head="$(git --git-dir "${BASE_DIR}/repo-dummy/.git" --work-tree "$ASDF_DIR/plugins/dummy" describe --tags)" + [ "$status" -eq 0 ] + [ "$repo_head" = "tagname" ] + + run asdf plugin-list + [ "$output" = "dummy-tagname" ] +} + @test "plugin_add command with URL specified run twice returns error second time" { install_mock_plugin_repo "dummy" diff --git a/test/test_helpers.bash b/test/test_helpers.bash index 1704fce78..d6ade7fbb 100644 --- a/test/test_helpers.bash +++ b/test/test_helpers.bash @@ -54,6 +54,13 @@ install_mock_plugin_repo() { git -C "${location}" commit -q -m "asdf ${plugin_name} plugin" } +install_mock_plugin_repo_with_ref() { + install_mock_plugin_repo "$1" + local plugin_name=$1 + local location="${BASE_DIR}/repo-${plugin_name}" + git -C "${location}" tag "tagname" +} + install_mock_plugin_version() { local plugin_name=$1 local plugin_version=$2