@@ -20,7 +20,6 @@ import (
20
20
"errors"
21
21
"fmt"
22
22
"net/url"
23
- "os"
24
23
"strings"
25
24
26
25
"github.com/arduino/arduino-cli/commands/cmderrors"
@@ -215,17 +214,29 @@ func (lmi *Installer) InstallGitLib(argURL string, overwrite bool) error {
215
214
defer tmp .RemoveAll ()
216
215
tmpInstallPath := tmp .Join (libraryName )
217
216
218
- depth := 1
219
- if ref != "" {
220
- depth = 0
221
- }
222
217
if _ , err := git .PlainClone (tmpInstallPath .String (), false , & git.CloneOptions {
223
218
URL : gitURL ,
224
- Depth : depth ,
225
- Progress : os .Stdout ,
226
- ReferenceName : ref ,
219
+ ReferenceName : plumbing .ReferenceName (ref ),
227
220
}); 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
+ }
229
240
}
230
241
231
242
// 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 {
241
252
242
253
// parseGitArgURL tries to recover a library name from a git URL.
243
254
// 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 ) {
245
256
// On Windows handle paths with backslashes in the form C:\Path\to\library
246
257
if path := paths .New (argURL ); path != nil && path .Exist () {
247
258
return path .Base (), argURL , "" , nil
@@ -279,7 +290,7 @@ func parseGitArgURL(argURL string) (string, string, plumbing.ReferenceName, erro
279
290
return "" , "" , "" , errors .New (i18n .Tr ("invalid git url" ))
280
291
}
281
292
// fragment == "1.0.3"
282
- rev := plumbing . ReferenceName ( parsedURL .Fragment )
293
+ rev := parsedURL .Fragment
283
294
// gitURL == "https://github.com/arduino-libraries/SigFox.git"
284
295
parsedURL .Fragment = ""
285
296
gitURL := parsedURL .String ()
0 commit comments