Skip to content

Commit c24e9e3

Browse files
authored
fix: remote origin check fails with references with "/" (#78)
1 parent 8775d97 commit c24e9e3

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

git/git.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,11 @@ func (git *Git) Remotes() ([]Remote, error) {
283283
for _, rawref := range strings.Split(res, "\n") {
284284
trimmedref := strings.TrimPrefix(rawref, refprefix)
285285
parsed := strings.Split(trimmedref, "/")
286-
if len(parsed) != 2 {
286+
if len(parsed) < 2 {
287287
return nil, fmt.Errorf("unexpected remote reference %q", rawref)
288288
}
289-
name, branch := parsed[0], parsed[1]
289+
name := parsed[0]
290+
branch := strings.Join(parsed[1:], "/")
290291
branches := references[name]
291292
references[name] = append(branches, branch)
292293
}

git/git_test.go

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,16 @@ func TestFetchRemoteRevErrorHandling(t *testing.T) {
189189

190190
func TestListingAvailableRemotes(t *testing.T) {
191191
type testcase struct {
192-
name string
193-
remotes []string
194-
want []git.Remote
192+
name string
193+
want []git.Remote
195194
}
196195

197196
tests := []testcase{
198197
{
199198
name: "no remotes",
200199
},
201200
{
202-
name: "one remote",
203-
remotes: []string{"origin"},
201+
name: "one remote",
204202
want: []git.Remote{
205203
{
206204
Name: "origin",
@@ -209,8 +207,34 @@ func TestListingAvailableRemotes(t *testing.T) {
209207
},
210208
},
211209
{
212-
name: "two remotes",
213-
remotes: []string{"origin", "another"},
210+
name: "two branches",
211+
want: []git.Remote{
212+
{
213+
Name: "origin",
214+
Branches: []string{"main", "test"},
215+
},
216+
},
217+
},
218+
{
219+
name: "branches with one forward slash",
220+
want: []git.Remote{
221+
{
222+
Name: "origin",
223+
Branches: []string{"main", "test/hi"},
224+
},
225+
},
226+
},
227+
{
228+
name: "branches with multiple forward slashes",
229+
want: []git.Remote{
230+
{
231+
Name: "origin",
232+
Branches: []string{"main", "test/hi/one/more/yay"},
233+
},
234+
},
235+
},
236+
{
237+
name: "two remotes",
214238
want: []git.Remote{
215239
{
216240
Name: "another",
@@ -229,14 +253,25 @@ func TestListingAvailableRemotes(t *testing.T) {
229253
repodir := mkOneCommitRepo(t)
230254
g := test.NewGitWrapper(t, repodir, false)
231255

232-
for _, remote := range tc.remotes {
256+
for _, gitRemote := range tc.want {
233257

258+
remote := gitRemote.Name
234259
remoteDir := test.EmptyRepo(t, true)
235260
err := g.RemoteAdd(remote, remoteDir)
236261
assert.NoError(t, err)
237262

238-
err = g.Push(remote, "main")
239-
assert.NoError(t, err)
263+
for _, branch := range gitRemote.Branches {
264+
265+
if branch == defaultBranch {
266+
err = g.Push(remote, branch)
267+
assert.NoError(t, err)
268+
continue
269+
}
270+
271+
assert.NoError(t, g.Checkout(branch, true))
272+
assert.NoError(t, g.Push(remote, branch))
273+
assert.NoError(t, g.Checkout(defaultBranch, false))
274+
}
240275
}
241276

242277
gotRemotes, err := g.Remotes()
@@ -250,8 +285,7 @@ func TestListingAvailableRemotes(t *testing.T) {
250285

251286
func TestListRemoteWithMultipleBranches(t *testing.T) {
252287
const (
253-
remote = "origin"
254-
defaultBranch = "main"
288+
remote = "origin"
255289
)
256290

257291
repodir := mkOneCommitRepo(t)
@@ -281,6 +315,8 @@ func TestListRemoteWithMultipleBranches(t *testing.T) {
281315
assertEqualRemotes(t, got, want)
282316
}
283317

318+
const defaultBranch = "main"
319+
284320
func mkOneCommitRepo(t *testing.T) string {
285321
repodir := test.EmptyRepo(t, false)
286322

0 commit comments

Comments
 (0)