Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add versioning to plugin add command #916

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/commands/command-plugin-add.bash
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# -*- sh -*-

plugin_add_command() {
if [[ $# -lt 1 || $# -gt 2 ]]; then
display_error "usage: asdf plugin add <name> [<git-url>]"
if [[ $# -lt 1 || $# -gt 3 ]]; then
display_error "usage: asdf plugin add <name> [<git-url>] [<git-ref>]"
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_-]+$"
Expand All @@ -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
Expand All @@ -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
Expand Down
20 changes: 15 additions & 5 deletions lib/commands/command-plugin-update.bash
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,21 @@ update_plugin() {
plugin_remote_default_branch=$(git --git-dir "$plugin_path/.git" --work-tree "$plugin_path" ls-remote --symref origin HEAD | awk '{ sub(/refs\/heads\//, ""); print $2; exit }')
local gitref=${3:-${plugin_remote_default_branch}}
logfile=$(mktemp)
{
printf "Updating %s to %s\\n" "$plugin_name" "$gitref"
git --git-dir "$plugin_path/.git" --work-tree "$plugin_path" fetch --prune --update-head-ok origin "$gitref:$gitref"
git --git-dir "$plugin_path/.git" --work-tree "$plugin_path" -c advice.detachedHead=false checkout --force "$gitref"
} >"$logfile" 2>&1

local current_ref
current_ref=$(git --git-dir "$plugin_path/.git" branch --show-current)
if [ ! "$current_ref" = "$plugin_remote_default_branch" ]; then
{
printf "Skipping detached %s\\n" "$plugin_name"
} >"$logfile" 2>&1
else
{
printf "Updating %s to %s\\n" "$plugin_name" "$gitref"
git --git-dir "$plugin_path/.git" --work-tree "$plugin_path" fetch --prune --update-head-ok origin "$gitref:$gitref"
git --git-dir "$plugin_path/.git" --work-tree "$plugin_path" -c advice.detachedHead=false checkout --force "$gitref"
} >"$logfile" 2>&1
fi

cat "$logfile"
rm "$logfile"
}
Expand Down
14 changes: 14 additions & 0 deletions test/plugin_add_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" "tagname"

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"

Expand Down
15 changes: 15 additions & 0 deletions test/plugin_update_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ teardown() {
[ "$repo_head" = "main" ]
}

@test "asdf plugin-update should skip updates for detached plugin" {
install_mock_plugin_repo_with_ref "dummy" "tagname"

run asdf plugin add "dummy" "${BASE_DIR}/repo-dummy" "tagname"
[ "$status" -eq 0 ]

run asdf plugin update dummy-tagname

current_ref="$(git --git-dir "$ASDF_DIR/plugins/dummy-tagname/.git" --work-tree "$ASDF_DIR/plugins/dummy-tagname" describe --tags)"

[ "$status" -eq 0 ]
[[ "$output" =~ "Skipping detached dummy-tagname"* ]]
[ "$current_ref" = "tagname" ]
}

@test "asdf plugin-update should not remove plugin versions" {
run asdf install dummy 1.1
[ "$status" -eq 0 ]
Expand Down
8 changes: 8 additions & 0 deletions test/test_helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ 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 tagname=$2
local location="${BASE_DIR}/repo-${plugin_name}"
git -C "${location}" tag "${tagname}"
}

install_mock_plugin_version() {
local plugin_name=$1
local plugin_version=$2
Expand Down