Skip to content

fix: allow lib-install from git using revision hash as a reference #2882

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

Merged
merged 3 commits into from
Apr 8, 2025
Merged
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
33 changes: 22 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,29 @@ 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{
Force: true, // workaround for: https://github.com/go-git/go-git/issues/1411
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 +252,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 +290,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
21 changes: 21 additions & 0 deletions internal/integrationtest/lib/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package lib_test

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -687,6 +689,7 @@ func TestInstallWithGitUrlFragmentAsBranch(t *testing.T) {

t.Run("RefPointingToBranch", func(t *testing.T) {
libInstallDir := cli.SketchbookDir().Join("libraries", "ArduinoCloud")
t.Cleanup(func() { libInstallDir.RemoveAll() })

// Verify install with ref pointing to a branch
require.NoDirExists(t, libInstallDir.String())
Expand All @@ -700,6 +703,24 @@ func TestInstallWithGitUrlFragmentAsBranch(t *testing.T) {
require.NoError(t, err)
require.Contains(t, string(fileToTest), `#define LENGHT_M "meters"`) // nolint:misspell
})

t.Run("RefPointingToHash", func(t *testing.T) {
libInstallDir := cli.SketchbookDir().Join("libraries", "ArduinoCloud")
t.Cleanup(func() { libInstallDir.RemoveAll() })

// Verify install with ref pointing to a branch
require.NoDirExists(t, libInstallDir.String())
_, _, err = cli.Run("lib", "install", "--git-url", "https://github.com/arduino-libraries/ArduinoCloud.git#fe1a1c5d1f8ea2cb27ece1a3b9344dc1eaed60b6", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)
require.DirExists(t, libInstallDir.String())

// Verify that the correct branch is checked out
// https://github.com/arduino-libraries/ArduinoCloud/commit/fe1a1c5d1f8ea2cb27ece1a3b9344dc1eaed60b6
fileToTest, err := libInstallDir.Join("examples", "ReadAndWrite", "ReadAndWrite.ino").ReadFile()
require.NoError(t, err)
chksum := sha256.Sum256(fileToTest)
require.Equal(t, hex.EncodeToString(chksum[:]), `f71889cd6da3b91755c7d1b8ec76b7ee6e2824d8a417c043d117ffdf1546f896`)
})
}

func TestUpdateIndex(t *testing.T) {
Expand Down