Skip to content

Commit 23ed1b1

Browse files
authored
fix: allow lib-install from git using revision hash as a reference (#2882)
* Added integration test * bugfix: allow lib-install from git with revision hash * Workaround for a bug in go-git
1 parent 00a33b7 commit 23ed1b1

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

internal/arduino/libraries/librariesmanager/install.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"errors"
2121
"fmt"
2222
"net/url"
23-
"os"
2423
"strings"
2524

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

218-
depth := 1
219-
if ref != "" {
220-
depth = 0
221-
}
222217
if _, err := git.PlainClone(tmpInstallPath.String(), false, &git.CloneOptions{
223218
URL: gitURL,
224-
Depth: depth,
225-
Progress: os.Stdout,
226-
ReferenceName: ref,
219+
ReferenceName: plumbing.ReferenceName(ref),
227220
}); err != nil {
228-
return err
221+
if err.Error() != "reference not found" {
222+
return err
223+
}
224+
225+
// We did not find the requested reference, let's do a PlainClone and use
226+
// "ResolveRevision" to find and checkout the requested revision
227+
if repo, err := git.PlainClone(tmpInstallPath.String(), false, &git.CloneOptions{
228+
URL: gitURL,
229+
}); err != nil {
230+
return err
231+
} else if h, err := repo.ResolveRevision(plumbing.Revision(ref)); err != nil {
232+
return err
233+
} else if w, err := repo.Worktree(); err != nil {
234+
return err
235+
} else if err := w.Checkout(&git.CheckoutOptions{
236+
Force: true, // workaround for: https://github.com/go-git/go-git/issues/1411
237+
Hash: plumbing.NewHash(h.String())}); err != nil {
238+
return err
239+
}
229240
}
230241

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

242253
// parseGitArgURL tries to recover a library name from a git URL.
243254
// Returns an error in case the URL is not a valid git URL.
244-
func parseGitArgURL(argURL string) (string, string, plumbing.ReferenceName, error) {
255+
func parseGitArgURL(argURL string) (string, string, string, error) {
245256
// On Windows handle paths with backslashes in the form C:\Path\to\library
246257
if path := paths.New(argURL); path != nil && path.Exist() {
247258
return path.Base(), argURL, "", nil
@@ -279,7 +290,7 @@ func parseGitArgURL(argURL string) (string, string, plumbing.ReferenceName, erro
279290
return "", "", "", errors.New(i18n.Tr("invalid git url"))
280291
}
281292
// fragment == "1.0.3"
282-
rev := plumbing.ReferenceName(parsedURL.Fragment)
293+
rev := parsedURL.Fragment
283294
// gitURL == "https://github.com/arduino-libraries/SigFox.git"
284295
parsedURL.Fragment = ""
285296
gitURL := parsedURL.String()

internal/integrationtest/lib/lib_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package lib_test
1717

1818
import (
19+
"crypto/sha256"
20+
"encoding/hex"
1921
"encoding/json"
2022
"fmt"
2123
"io"
@@ -687,6 +689,7 @@ func TestInstallWithGitUrlFragmentAsBranch(t *testing.T) {
687689

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

691694
// Verify install with ref pointing to a branch
692695
require.NoDirExists(t, libInstallDir.String())
@@ -700,6 +703,24 @@ func TestInstallWithGitUrlFragmentAsBranch(t *testing.T) {
700703
require.NoError(t, err)
701704
require.Contains(t, string(fileToTest), `#define LENGHT_M "meters"`) // nolint:misspell
702705
})
706+
707+
t.Run("RefPointingToHash", func(t *testing.T) {
708+
libInstallDir := cli.SketchbookDir().Join("libraries", "ArduinoCloud")
709+
t.Cleanup(func() { libInstallDir.RemoveAll() })
710+
711+
// Verify install with ref pointing to a branch
712+
require.NoDirExists(t, libInstallDir.String())
713+
_, _, err = cli.Run("lib", "install", "--git-url", "https://github.com/arduino-libraries/ArduinoCloud.git#fe1a1c5d1f8ea2cb27ece1a3b9344dc1eaed60b6", "--config-file", "arduino-cli.yaml")
714+
require.NoError(t, err)
715+
require.DirExists(t, libInstallDir.String())
716+
717+
// Verify that the correct branch is checked out
718+
// https://github.com/arduino-libraries/ArduinoCloud/commit/fe1a1c5d1f8ea2cb27ece1a3b9344dc1eaed60b6
719+
fileToTest, err := libInstallDir.Join("examples", "ReadAndWrite", "ReadAndWrite.ino").ReadFile()
720+
require.NoError(t, err)
721+
chksum := sha256.Sum256(fileToTest)
722+
require.Equal(t, hex.EncodeToString(chksum[:]), `f71889cd6da3b91755c7d1b8ec76b7ee6e2824d8a417c043d117ffdf1546f896`)
723+
})
703724
}
704725

705726
func TestUpdateIndex(t *testing.T) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy