Skip to content

Commit 99457e5

Browse files
authored
Merge pull request go-git#375 from noerw/add-remoteurl-option
Remote: add RemoteURL to {Fetch,Pull,Push}Options
2 parents 243e7c8 + c4b334d commit 99457e5

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

Diff for: options.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ func (o *CloneOptions) Validate() error {
9191
type PullOptions struct {
9292
// Name of the remote to be pulled. If empty, uses the default.
9393
RemoteName string
94+
// RemoteURL overrides the remote repo address with a custom URL
95+
RemoteURL string
9496
// Remote branch to clone. If empty, uses HEAD.
9597
ReferenceName plumbing.ReferenceName
9698
// Fetch only ReferenceName if true.
@@ -147,7 +149,9 @@ const (
147149
type FetchOptions struct {
148150
// Name of the remote to fetch from. Defaults to origin.
149151
RemoteName string
150-
RefSpecs []config.RefSpec
152+
// RemoteURL overrides the remote repo address with a custom URL
153+
RemoteURL string
154+
RefSpecs []config.RefSpec
151155
// Depth limit fetching to the specified number of commits from the tip of
152156
// each remote branch history.
153157
Depth int
@@ -192,6 +196,8 @@ func (o *FetchOptions) Validate() error {
192196
type PushOptions struct {
193197
// RemoteName is the name of the remote to be pushed to.
194198
RemoteName string
199+
// RemoteURL overrides the remote repo address with a custom URL
200+
RemoteURL string
195201
// RefSpecs specify what destination ref to update with what source
196202
// object. A refspec with empty src can be used to delete a reference.
197203
RefSpecs []config.RefSpec

Diff for: remote.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/go-git/go-billy/v5/osfs"
1212
"github.com/go-git/go-git/v5/config"
13+
"github.com/go-git/go-git/v5/internal/url"
1314
"github.com/go-git/go-git/v5/plumbing"
1415
"github.com/go-git/go-git/v5/plumbing/cache"
1516
"github.com/go-git/go-git/v5/plumbing/format/packfile"
@@ -104,7 +105,11 @@ func (r *Remote) PushContext(ctx context.Context, o *PushOptions) (err error) {
104105
return fmt.Errorf("remote names don't match: %s != %s", o.RemoteName, r.c.Name)
105106
}
106107

107-
s, err := newSendPackSession(r.c.URLs[0], o.Auth, o.InsecureSkipTLS, o.CABundle)
108+
if o.RemoteURL == "" {
109+
o.RemoteURL = r.c.URLs[0]
110+
}
111+
112+
s, err := newSendPackSession(o.RemoteURL, o.Auth, o.InsecureSkipTLS, o.CABundle)
108113
if err != nil {
109114
return err
110115
}
@@ -184,12 +189,12 @@ func (r *Remote) PushContext(ctx context.Context, o *PushOptions) (err error) {
184189
var hashesToPush []plumbing.Hash
185190
// Avoid the expensive revlist operation if we're only doing deletes.
186191
if !allDelete {
187-
if r.c.IsFirstURLLocal() {
192+
if url.IsLocalEndpoint(o.RemoteURL) {
188193
// If we're are pushing to a local repo, it might be much
189194
// faster to use a local storage layer to get the commits
190195
// to ignore, when calculating the object revlist.
191196
localStorer := filesystem.NewStorage(
192-
osfs.New(r.c.URLs[0]), cache.NewObjectLRUDefault())
197+
osfs.New(o.RemoteURL), cache.NewObjectLRUDefault())
193198
hashesToPush, err = revlist.ObjectsWithStorageForIgnores(
194199
r.s, localStorer, objects, haves)
195200
} else {
@@ -392,7 +397,11 @@ func (r *Remote) fetch(ctx context.Context, o *FetchOptions) (sto storer.Referen
392397
o.RefSpecs = r.c.Fetch
393398
}
394399

395-
s, err := newUploadPackSession(r.c.URLs[0], o.Auth, o.InsecureSkipTLS, o.CABundle)
400+
if o.RemoteURL == "" {
401+
o.RemoteURL = r.c.URLs[0]
402+
}
403+
404+
s, err := newUploadPackSession(o.RemoteURL, o.Auth, o.InsecureSkipTLS, o.CABundle)
396405
if err != nil {
397406
return nil, err
398407
}

Diff for: remote_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ func (s *RemoteSuite) TestFetchInvalidSchemaEndpoint(c *C) {
4646
c.Assert(err, ErrorMatches, ".*unsupported scheme.*")
4747
}
4848

49+
func (s *RemoteSuite) TestFetchOverriddenEndpoint(c *C) {
50+
r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"http://perfectly-valid-url.example.com"}})
51+
err := r.Fetch(&FetchOptions{RemoteURL: "http://\\"})
52+
c.Assert(err, ErrorMatches, ".*invalid character.*")
53+
}
54+
4955
func (s *RemoteSuite) TestFetchInvalidFetchOptions(c *C) {
5056
r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"qux://foo"}})
5157
invalid := config.RefSpec("^*$ñ")
@@ -963,6 +969,12 @@ func (s *RemoteSuite) TestPushNonExistentEndpoint(c *C) {
963969
c.Assert(err, NotNil)
964970
}
965971

972+
func (s *RemoteSuite) TestPushOverriddenEndpoint(c *C) {
973+
r := NewRemote(nil, &config.RemoteConfig{Name: "origin", URLs: []string{"http://perfectly-valid-url.example.com"}})
974+
err := r.Push(&PushOptions{RemoteURL: "http://\\"})
975+
c.Assert(err, ErrorMatches, ".*invalid character.*")
976+
}
977+
966978
func (s *RemoteSuite) TestPushInvalidSchemaEndpoint(c *C) {
967979
r := NewRemote(nil, &config.RemoteConfig{Name: "origin", URLs: []string{"qux://foo"}})
968980
err := r.Push(&PushOptions{})

Diff for: worktree.go

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func (w *Worktree) PullContext(ctx context.Context, o *PullOptions) error {
7373

7474
fetchHead, err := remote.fetch(ctx, &FetchOptions{
7575
RemoteName: o.RemoteName,
76+
RemoteURL: o.RemoteURL,
7677
Depth: o.Depth,
7778
Auth: o.Auth,
7879
Progress: o.Progress,

0 commit comments

Comments
 (0)