Skip to content

Commit

Permalink
Merge pull request #96 from asdf-vm/tb/version-improvements-2
Browse files Browse the repository at this point in the history
feat(golang-rewrite): misc. version improvements part 2
  • Loading branch information
Stratus3D committed Dec 18, 2024
2 parents e69149e + b9e79e6 commit 08ca28f
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 305 deletions.
26 changes: 21 additions & 5 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ Manage all your runtime versions with one tool!
Complete documentation is available at https://asdf-vm.com/`

const updateCommandRemovedText = `
Upgrading asdf via asdf update is no longer supported. Please use your OS
package manager (Homebrew, APT, etc...) to upgrade asdf or download the
latest asdf binary manually from the asdf website.
Please visit https://asdf-vm.com/ or https://github.com/asdf-vm/asdf for more
details.`

// Execute defines the full CLI API and then runs it
func Execute(version string) {
logger := log.New(os.Stderr, "", 0)
Expand Down Expand Up @@ -263,6 +271,13 @@ func Execute(version string) {
return uninstallCommand(logger, tool, version)
},
},
{
Name: "update",
Action: func(_ *cli.Context) error {
fmt.Println(updateCommandRemovedText)
return errors.New("command removed")
},
},
{
Name: "where",
Action: func(cCtx *cli.Context) error {
Expand Down Expand Up @@ -647,7 +662,7 @@ func pluginAddCommand(_ *cli.Context, conf config.Config, logger *log.Logger, pl
return cli.Exit("usage: asdf plugin add <name> [<git-url>]", 1)
}

err := plugins.Add(conf, pluginName, pluginRepo)
err := plugins.Add(conf, pluginName, pluginRepo, "")
if err != nil {
logger.Printf("%s", err)

Expand Down Expand Up @@ -853,19 +868,20 @@ func pluginUpdateCommand(cCtx *cli.Context, logger *log.Logger, pluginName, ref
}

for _, plugin := range installedPlugins {
updatedToRef, err := plugins.Update(conf, plugin.Name, "")
updatedToRef, err := plugin.Update(conf, "", os.Stdout, os.Stderr)
formatUpdateResult(logger, plugin.Name, updatedToRef, err)
}

return nil
}

updatedToRef, err := plugins.Update(conf, pluginName, ref)
plugin := plugins.New(conf, pluginName)
updatedToRef, err := plugin.Update(conf, ref, os.Stdout, os.Stderr)
formatUpdateResult(logger, pluginName, updatedToRef, err)
return err
}

func pluginTestCommand(l *log.Logger, args []string, toolVersion, _ string) {
func pluginTestCommand(l *log.Logger, args []string, toolVersion, ref string) {
conf, err := config.LoadConfig()
if err != nil {
l.Printf("error loading config: %s", err)
Expand All @@ -882,7 +898,7 @@ func pluginTestCommand(l *log.Logger, args []string, toolVersion, _ string) {
testName := fmt.Sprintf("asdf-test-%s", name)

// Install plugin
err = plugins.Add(conf, testName, url)
err = plugins.Add(conf, testName, url, ref)
if err != nil {
failTest(l, fmt.Sprintf("%s was not properly installed", name))
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/asdf/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func TestBatsTests(t *testing.T) {
runBatsFile(t, dir, "plugin_test_command.bats")
})

//t.Run("plugin_update_command", func(t *testing.T) {
// runBatsFile(t, dir, "plugin_update_command.bats")
//})
t.Run("plugin_update_command", func(t *testing.T) {
runBatsFile(t, dir, "plugin_update_command.bats")
})

t.Run("remove_command", func(t *testing.T) {
runBatsFile(t, dir, "remove_command.bats")
Expand Down
15 changes: 10 additions & 5 deletions docs/guide/upgrading-from-v0-14-to-v0-15.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ versions are supported. The affected commands:
* `asdf plugin-test` -> `asdf plugin test`
* `asdf shim-versions` -> `asdf shimversions`

### `asdf global` and `asdf local` commands have been replaced by the `asdf set` command

`asdf global` and `asdf local` have been replaced by `asdf set`, which aims to
provide the same functionality while using terminology that is less likely to
mislead the user. TODO: Add more details here
### `asdf global` and `asdf local` commands have been removed

`asdf global` and `asdf local` have been removed. The "global" and "local"
terminology was wrong and also misleading. asdf doesn't actually support
"global" versions that apply everywhere. Any version that was specified with
`asdf global` could easily be overridden by a `.tool-versions` file in your
current directory specifying a different version. This was confusing to users.
The plan is to introduce an `asdf set` command in the near future that better
conveys how asdf works and provides similar functionality to `asdf global` and
`asdf local`.

### `asdf update` command has been removed

Expand Down
40 changes: 26 additions & 14 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const DefaultRemoteName = "origin"
// and upgrade plugins. If other approaches are supported this will be
// extracted into the `plugins` module.
type Repoer interface {
Clone(pluginURL string) error
Clone(pluginURL, ref string) error
Head() (string, error)
RemoteURL() (string, error)
Update(ref string) (string, error)
Update(ref string) (string, string, string, error)
}

// Repo is a struct to contain the Git repository details
Expand All @@ -38,10 +38,17 @@ func NewRepo(directory string) Repo {
}

// Clone installs a plugin via Git
func (r Repo) Clone(pluginURL string) error {
_, err := git.PlainClone(r.Directory, false, &git.CloneOptions{
func (r Repo) Clone(pluginURL, ref string) error {
options := git.CloneOptions{
URL: pluginURL,
})
}

// if ref is provided set it on CloneOptions
if ref != "" {
options.ReferenceName = plumbing.NewBranchReferenceName(ref)
}

_, err := git.PlainClone(r.Directory, false, &options)
if err != nil {
return fmt.Errorf("unable to clone plugin: %w", err)
}
Expand Down Expand Up @@ -81,10 +88,15 @@ func (r Repo) RemoteURL() (string, error) {

// Update updates the plugin's Git repository to the ref if provided, or the
// latest commit on the current branch
func (r Repo) Update(ref string) (string, error) {
func (r Repo) Update(ref string) (string, string, string, error) {
repo, err := gitOpen(r.Directory)
if err != nil {
return "", err
return "", "", "", err
}

oldHash, err := repo.ResolveRevision(plumbing.Revision("HEAD"))
if err != nil {
return "", "", "", err
}

var checkoutOptions git.CheckoutOptions
Expand All @@ -93,11 +105,11 @@ func (r Repo) Update(ref string) (string, error) {
// If no ref is provided checkout latest commit on current branch
head, err := repo.Head()
if err != nil {
return "", err
return "", "", "", err
}

if !head.Name().IsBranch() {
return "", fmt.Errorf("not on a branch, unable to update")
return "", "", "", fmt.Errorf("not on a branch, unable to update")
}

// If on a branch checkout the latest version of it from the remote
Expand All @@ -116,21 +128,21 @@ func (r Repo) Update(ref string) (string, error) {
err = repo.Fetch(&fetchOptions)

if err != nil && err != git.NoErrAlreadyUpToDate {
return "", err
return "", "", "", err
}

worktree, err := repo.Worktree()
if err != nil {
return "", err
return "", "", "", err
}

err = worktree.Checkout(&checkoutOptions)
if err != nil {
return "", err
return "", "", "", err
}

hash, err := repo.ResolveRevision(plumbing.Revision("HEAD"))
return hash.String(), err
newHash, err := repo.ResolveRevision(plumbing.Revision("HEAD"))
return ref, oldHash.String(), newHash.String(), err
}

func gitOpen(directory string) (*git.Repository, error) {
Expand Down
Loading

0 comments on commit 08ca28f

Please sign in to comment.