@@ -19,6 +19,7 @@ import (
19
19
"context"
20
20
"errors"
21
21
"fmt"
22
+ "net/url"
22
23
"os"
23
24
"strings"
24
25
@@ -111,21 +112,42 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
111
112
}
112
113
113
114
//InstallGitLib installs a library hosted on a git repository on the specified path.
114
- func (lm * LibrariesManager ) InstallGitLib (url string ) error {
115
+ func (lm * LibrariesManager ) InstallGitLib (gitURL string ) error {
115
116
libsDir := lm .getUserLibrariesDir ()
116
117
if libsDir == nil {
117
118
return fmt .Errorf ("User directory not set" )
118
119
}
119
- i := strings .LastIndex (url , "/" )
120
- folder := strings .TrimRight (url [i + 1 :], ".git" )
121
- path := libsDir .Join (folder )
122
120
123
- _ , err := git .PlainClone (path .String (), false , & git.CloneOptions {
124
- URL : url ,
121
+ libraryName , err := parseGitURL (gitURL )
122
+ if err != nil {
123
+ return err
124
+ }
125
+
126
+ installPath := libsDir .Join (libraryName )
127
+
128
+ _ , err = git .PlainClone (installPath .String (), false , & git.CloneOptions {
129
+ URL : gitURL ,
125
130
Progress : os .Stdout ,
126
131
})
127
132
if err != nil {
128
133
return err
129
134
}
130
135
return nil
131
136
}
137
+
138
+ func parseGitURL (gitURL string ) (string , error ) {
139
+ var res string
140
+ if strings .HasPrefix (gitURL , "git@" ) {
141
+ // We can't parse these as URLs
142
+ i := strings .LastIndex (gitURL , "/" )
143
+ res = strings .TrimRight (gitURL [i + 1 :], ".git" )
144
+ } else if path := paths .New (gitURL ); path .Exist () {
145
+ res = path .Base ()
146
+ } else if parsed , err := url .Parse (gitURL ); err == nil {
147
+ i := strings .LastIndex (parsed .Path , "/" )
148
+ res = strings .TrimRight (parsed .Path [i + 1 :], ".git" )
149
+ } else {
150
+ return "" , fmt .Errorf ("invalid git url" )
151
+ }
152
+ return res , nil
153
+ }
0 commit comments