Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
bugfix: allow lib-install from git with revision hash
  • Loading branch information
cmaglie committed Apr 7, 2025
commit 95ee7ae85d62711e6cf80e311851505bc5dae27a
31 changes: 20 additions & 11 deletions internal/arduino/libraries/librariesmanager/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"net/url"
"os"
"strings"

"github.com/arduino/arduino-cli/commands/cmderrors"
Expand Down Expand Up @@ -215,17 +214,27 @@ func (lmi *Installer) InstallGitLib(argURL string, overwrite bool) error {
defer tmp.RemoveAll()
tmpInstallPath := tmp.Join(libraryName)

depth := 1
if ref != "" {
depth = 0
}
if _, err := git.PlainClone(tmpInstallPath.String(), false, &git.CloneOptions{
URL: gitURL,
Depth: depth,
Progress: os.Stdout,
ReferenceName: ref,
ReferenceName: plumbing.ReferenceName(ref),
}); err != nil {
return err
if err.Error() != "reference not found" {
return err
}

// We did not find the requested reference, let's do a PlainClone and use
// "ResolveRevision" to find and checkout the requested revision
if repo, err := git.PlainClone(tmpInstallPath.String(), false, &git.CloneOptions{
URL: gitURL,
}); err != nil {
return err
} else if h, err := repo.ResolveRevision(plumbing.Revision(ref)); err != nil {
return err
} else if w, err := repo.Worktree(); err != nil {
return err
} else if err := w.Checkout(&git.CheckoutOptions{Hash: plumbing.NewHash(h.String())}); err != nil {
return err
}
}

// We don't want the installed library to be a git repository thus we delete this folder
Expand All @@ -241,7 +250,7 @@ func (lmi *Installer) InstallGitLib(argURL string, overwrite bool) error {

// parseGitArgURL tries to recover a library name from a git URL.
// Returns an error in case the URL is not a valid git URL.
func parseGitArgURL(argURL string) (string, string, plumbing.ReferenceName, error) {
func parseGitArgURL(argURL string) (string, string, string, error) {
// On Windows handle paths with backslashes in the form C:\Path\to\library
if path := paths.New(argURL); path != nil && path.Exist() {
return path.Base(), argURL, "", nil
Expand Down Expand Up @@ -279,7 +288,7 @@ func parseGitArgURL(argURL string) (string, string, plumbing.ReferenceName, erro
return "", "", "", errors.New(i18n.Tr("invalid git url"))
}
// fragment == "1.0.3"
rev := plumbing.ReferenceName(parsedURL.Fragment)
rev := parsedURL.Fragment
// gitURL == "https://github.com/arduino-libraries/SigFox.git"
parsedURL.Fragment = ""
gitURL := parsedURL.String()
Expand Down
Loading