Skip to content

Commit e6ec041

Browse files
committed
fix(push): we need to allow pushing to a remote by name and refspec
This adds support for pushing to a remote by its name and branch name.
1 parent b67e7b2 commit e6ec041

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

cmd/gogit/push.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package main
22

33
import (
44
"errors"
5+
"fmt"
56

67
"github.com/go-git/go-git/v6"
78
"github.com/go-git/go-git/v6/config"
9+
"github.com/go-git/go-git/v6/plumbing"
810
"github.com/go-git/go-git/v6/plumbing/transport"
911
"github.com/spf13/cobra"
1012
"golang.org/x/term"
@@ -36,20 +38,38 @@ var pushCmd = &cobra.Command{
3638
return err
3739
}
3840

41+
cfg, err := r.Config()
42+
if err != nil {
43+
return fmt.Errorf("failed to get repository config: %w", err)
44+
}
45+
3946
var ep *transport.Endpoint
4047
remoteName := git.DefaultRemoteName
4148
if len(args) > 0 {
42-
ep, err = transport.NewEndpoint(args[0])
43-
if err != nil {
44-
// We have a remote name
45-
remoteName = args[0]
49+
isRemote := false
50+
for remote := range cfg.Remotes {
51+
if args[0] == remote {
52+
remoteName = remote
53+
isRemote = true
54+
break
55+
}
56+
}
57+
if !isRemote {
58+
// Is this a repository URL?
59+
ep, err = transport.NewEndpoint(args[0])
60+
if err != nil {
61+
// We have a remote name
62+
remoteName = args[0]
63+
}
4664
}
4765
}
4866

4967
var refspecs []config.RefSpec
5068
if len(args) > 1 {
5169
for _, arg := range args[1:] {
52-
refspecs = append(refspecs, config.RefSpec(arg))
70+
ref := plumbing.NewBranchReferenceName(arg)
71+
str := fmt.Sprintf("+%s:refs/heads/%s", ref.String(), ref.Short())
72+
refspecs = append(refspecs, config.RefSpec(str))
5373
}
5474
}
5575

0 commit comments

Comments
 (0)