Skip to content

Commit

Permalink
fix: collect also push URLs
Browse files Browse the repository at this point in the history
This is a hack: we should collect pull URLs and push URLs (if
any) separately and use the appropriate ones, or perhaps add a
flag to each URL, whether it is capable of pushing.

Also, add test for the remote URLs (pull and push)
  • Loading branch information
mcepl committed Sep 24, 2024
1 parent 9cf0e3e commit 619ad79
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions _examples/clone/auth/ssh/ssh_agent/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go-git-example-clone-auth-ssh_agent
1 change: 1 addition & 0 deletions _examples/push/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go-git-example-push
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ const (
extensionsSection = "extensions"
fetchKey = "fetch"
urlKey = "url"
pushurlKey = "pushurl"
bareKey = "bare"
worktreeKey = "worktree"
commentCharKey = "commentChar"
Expand Down Expand Up @@ -633,6 +634,7 @@ func (c *RemoteConfig) unmarshal(s *format.Subsection) error {

c.Name = c.raw.Name
c.URLs = append([]string(nil), c.raw.Options.GetAll(urlKey)...)
c.URLs = append([]string(nil), c.raw.Options.GetAll(pushurlKey)...)
c.Fetch = fetch
c.Mirror = c.raw.Options.Get(mirrorKey) == "true"

Expand Down
24 changes: 24 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,27 @@ func (s *ConfigSuite) TestRemoveUrlOptions(c *C) {
}
c.Assert(err, IsNil)
}

func (s *ConfigSuite) TestUnmarshalRemotes(c *C) {
input := []byte(`[core]
bare = true
worktree = foo
custom = ignored
[user]
name = John Doe
email = john@example.com
[remote "origin"]
url = https://git.sr.ht/~mcepl/go-git
pushurl = git@git.sr.ht:~mcepl/go-git.git
fetch = +refs/heads/*:refs/remotes/origin/*
mirror = true
`)

cfg := NewConfig()
err := cfg.Unmarshal(input)
c.Assert(err, IsNil)

c.Assert(cfg.Remotes["origin"].URLs[0], Equals, "https://git.sr.ht/~mcepl/go-git")
c.Assert(cfg.Remotes["origin"].URLs[1], Equals, "git@git.sr.ht:~mcepl/go-git.git")
}

4 changes: 2 additions & 2 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (r *Remote) String() string {
var fetch, push string
if len(r.c.URLs) > 0 {
fetch = r.c.URLs[0]
push = r.c.URLs[0]
push = r.c.URLs[len(r.c.URLs) - 1]
}

return fmt.Sprintf("%s\t%s (fetch)\n%[1]s\t%[3]s (push)", r.c.Name, fetch, push)
Expand Down Expand Up @@ -111,7 +111,7 @@ func (r *Remote) PushContext(ctx context.Context, o *PushOptions) (err error) {
}

if o.RemoteURL == "" {
o.RemoteURL = r.c.URLs[0]
o.RemoteURL = r.c.URLs[len(r.c.URLs) - 1]
}

s, err := newSendPackSession(o.RemoteURL, o.Auth, o.InsecureSkipTLS, o.CABundle, o.ProxyOptions)
Expand Down

0 comments on commit 619ad79

Please sign in to comment.