From 8c1e320dd40c8f99df46bdf0f1097d2365485519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Cepl?= Date: Tue, 27 Aug 2024 12:02:32 +0200 Subject: [PATCH] fix: collect also push URLs 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) --- _examples/clone/auth/ssh/ssh_agent/.gitignore | 1 + _examples/push/.gitignore | 1 + config/config.go | 2 ++ config/config_test.go | 24 +++++++++++++++++++ remote.go | 4 ++-- 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 _examples/clone/auth/ssh/ssh_agent/.gitignore create mode 100644 _examples/push/.gitignore diff --git a/_examples/clone/auth/ssh/ssh_agent/.gitignore b/_examples/clone/auth/ssh/ssh_agent/.gitignore new file mode 100644 index 000000000..6b40babd9 --- /dev/null +++ b/_examples/clone/auth/ssh/ssh_agent/.gitignore @@ -0,0 +1 @@ +go-git-example-clone-auth-ssh_agent diff --git a/_examples/push/.gitignore b/_examples/push/.gitignore new file mode 100644 index 000000000..27f20a4fc --- /dev/null +++ b/_examples/push/.gitignore @@ -0,0 +1 @@ +go-git-example-push diff --git a/config/config.go b/config/config.go index 6d41c15dc..26acd588a 100644 --- a/config/config.go +++ b/config/config.go @@ -252,6 +252,7 @@ const ( extensionsSection = "extensions" fetchKey = "fetch" urlKey = "url" + pushurlKey = "pushurl" bareKey = "bare" worktreeKey = "worktree" commentCharKey = "commentChar" @@ -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" diff --git a/config/config_test.go b/config/config_test.go index 7e9483f6f..6f011e012 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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") +} + diff --git a/remote.go b/remote.go index 170883abc..e3359451b 100644 --- a/remote.go +++ b/remote.go @@ -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) @@ -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)