From 8453bf679c5d0b3b0d0d626f7060e6d2ba385101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 28 Jan 2020 19:51:44 +0100 Subject: [PATCH] Fix parsing non-GitHub remotes Otherwise, the remote URL translation mechanism had crashed when encountering a non-Github.com git remote. --- context/remote.go | 3 +++ context/remote_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) 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()) + } +}