diff --git a/context/remote.go b/context/remote.go index 5ecfa7fec..b40533a54 100644 --- a/context/remote.go +++ b/context/remote.go @@ -81,6 +81,9 @@ func translateRemotes(gitRemotes git.RemoteSet, urlTranslate func(*url.URL) *url if r.PushURL != nil && repo == nil { repo, _ = ghrepo.FromURL(urlTranslate(r.PushURL)) } + if repo == nil { + continue + } remotes = append(remotes, &Remote{ Remote: r, Owner: repo.RepoOwner(), diff --git a/context/remote_test.go b/context/remote_test.go index b84d61c7a..727ddf078 100644 --- a/context/remote_test.go +++ b/context/remote_test.go @@ -2,6 +2,7 @@ package context import ( "errors" + "net/url" "testing" "github.com/cli/cli/git" @@ -25,3 +26,34 @@ func Test_Remotes_FindByName(t *testing.T) { _, err = list.FindByName("nonexist") eq(t, err, errors.New(`no GitHub remotes found`)) } + +func Test_translateRemotes(t *testing.T) { + publicURL, _ := url.Parse("https://github.com/monalisa/hello") + originURL, _ := url.Parse("http://example.com/repo") + + gitRemotes := git.RemoteSet{ + &git.Remote{ + Name: "origin", + FetchURL: originURL, + }, + &git.Remote{ + Name: "public", + FetchURL: publicURL, + }, + } + + identityURL := func(u *url.URL) *url.URL { + return u + } + result := translateRemotes(gitRemotes, identityURL) + + if len(result) != 1 { + t.Errorf("got %d results", len(result)) + } + if result[0].Name != "public" { + t.Errorf("got %q", result[0].Name) + } + if result[0].RepoName() != "hello" { + t.Errorf("got %q", result[0].RepoName()) + } +}