@@ -13,6 +13,7 @@ import (
1313 retry "github.com/avast/retry-go"
1414 "github.com/pkg/errors"
1515 gitconfig "github.com/tcnksm/go-gitconfig"
16+ giturls "github.com/whilp/git-urls"
1617 "github.com/zaquestion/lab/internal/logger"
1718)
1819
@@ -181,26 +182,27 @@ func PathWithNamespace(remote string) (string, error) {
181182 if err != nil {
182183 return "" , err
183184 }
185+ if remoteURL == "" {
186+ // Branches can track remote based on ther URL, thus we don't
187+ // really have a remote entity in the git config, but only the
188+ // URL of the remote.
189+ // https://git-scm.com/docs/git-push#Documentation/git-push.txt-ltrepositorygt
190+ remoteURL = remote
191+ }
184192 }
185193
186- parts := strings .Split (remoteURL , "//" )
187-
188- if len (parts ) == 1 {
189- // scp-like short syntax (e.g. [email protected] ...) 190- part := parts [0 ]
191- parts = strings .Split (part , ":" )
192- } else if len (parts ) == 2 {
193- // every other protocol syntax (e.g. ssh://, http://, git://)
194- part := parts [1 ]
195- parts = strings .SplitN (part , "/" , 2 )
196- } else {
197- return "" , errors .Errorf ("cannot parse remote: %s url: %s" , remote , remoteURL )
194+ u , err := giturls .Parse (remoteURL )
195+ if err != nil {
196+ return "" , err
198197 }
199198
200- if len (parts ) != 2 {
201- return "" , errors .Errorf ("cannot parse remote: %s url: %s" , remote , remoteURL )
199+ // remote URLs can't refer to other files or local paths, ie., other remote
200+ // names.
201+ if u .Scheme == "file" {
202+ return "" , errors .Errorf ("invalid remote URL format for %s" , remote )
202203 }
203- path := parts [1 ]
204+
205+ path := strings .TrimPrefix (u .Path , "/" )
204206 path = strings .TrimSuffix (path , ".git" )
205207 return path , nil
206208}
0 commit comments