Skip to content

Commit 5e64929

Browse files
committed
Add RemoteURL to {Fetch,Pull,Push}Options
Can be used to override the URL to operate on: RemoteName will be ignored for the actual fetch
1 parent 4ec1753 commit 5e64929

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

options.go

Lines changed: 7 additions & 1 deletion
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

remote.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

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

106-
s, err := newSendPackSession(r.c.URLs[0], o.Auth, o.InsecureSkipTLS, o.CABundle)
107+
if o.RemoteURL == "" {
108+
o.RemoteURL = r.c.URLs[0]
109+
}
110+
111+
s, err := newSendPackSession(o.RemoteURL, o.Auth, o.InsecureSkipTLS, o.CABundle)
107112
if err != nil {
108113
return err
109114
}
@@ -183,12 +188,12 @@ func (r *Remote) PushContext(ctx context.Context, o *PushOptions) (err error) {
183188
var hashesToPush []plumbing.Hash
184189
// Avoid the expensive revlist operation if we're only doing deletes.
185190
if !allDelete {
186-
if r.c.IsFirstURLLocal() {
191+
if url.IsLocalEndpoint(o.RemoteURL) {
187192
// If we're are pushing to a local repo, it might be much
188193
// faster to use a local storage layer to get the commits
189194
// to ignore, when calculating the object revlist.
190195
localStorer := filesystem.NewStorage(
191-
osfs.New(r.c.URLs[0]), cache.NewObjectLRUDefault())
196+
osfs.New(o.RemoteURL), cache.NewObjectLRUDefault())
192197
hashesToPush, err = revlist.ObjectsWithStorageForIgnores(
193198
r.s, localStorer, objects, haves)
194199
} else {
@@ -314,7 +319,11 @@ func (r *Remote) fetch(ctx context.Context, o *FetchOptions) (sto storer.Referen
314319
o.RefSpecs = r.c.Fetch
315320
}
316321

317-
s, err := newUploadPackSession(r.c.URLs[0], o.Auth, o.InsecureSkipTLS, o.CABundle)
322+
if o.RemoteURL == "" {
323+
o.RemoteURL = r.c.URLs[0]
324+
}
325+
326+
s, err := newUploadPackSession(o.RemoteURL, o.Auth, o.InsecureSkipTLS, o.CABundle)
318327
if err != nil {
319328
return nil, err
320329
}

worktree.go

Lines changed: 1 addition & 0 deletions
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)