Skip to content

Commit 95ee7ae

Browse files
committed
bugfix: allow lib-install from git with revision hash
1 parent e7f8f88 commit 95ee7ae

File tree

1 file changed

+20
-11
lines changed
  • internal/arduino/libraries/librariesmanager

1 file changed

+20
-11
lines changed

Diff for: internal/arduino/libraries/librariesmanager/install.go

+20-11
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,27 @@ 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{Hash: plumbing.NewHash(h.String())}); err != nil {
236+
return err
237+
}
229238
}
230239

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

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

0 commit comments

Comments
 (0)