@@ -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 ()
0 commit comments