@@ -19,6 +19,7 @@ import (
1919 "context"
2020 "errors"
2121 "fmt"
22+ "net/url"
2223 "os"
2324 "strings"
2425
@@ -111,21 +112,42 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
111112}
112113
113114//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 {
115116 libsDir := lm .getUserLibrariesDir ()
116117 if libsDir == nil {
117118 return fmt .Errorf ("User directory not set" )
118119 }
119- i := strings .LastIndex (url , "/" )
120- folder := strings .TrimRight (url [i + 1 :], ".git" )
121- path := libsDir .Join (folder )
122120
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 ,
125130 Progress : os .Stdout ,
126131 })
127132 if err != nil {
128133 return err
129134 }
130135 return nil
131136}
137+
138+ func parseGitURL (gitURL string ) (string , error ) {
139+ var res string
140+ if strings .HasPrefix (gitURL , "git" ) || strings .HasPrefix (gitURL , "ssh" ) {
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