Skip to content

Commit 73d97b9

Browse files
Support git_remote_create_with_opts (#733)
Closes #645
1 parent 07147a8 commit 73d97b9

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

remote.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ import (
1818
"unsafe"
1919
)
2020

21+
// RemoteCreateOptionsFlag is Remote creation options flags
22+
type RemoteCreateOptionsFlag uint
23+
24+
const (
25+
// Ignore the repository apply.insteadOf configuration
26+
RemoteCreateSkipInsteadof RemoteCreateOptionsFlag = C.GIT_REMOTE_CREATE_SKIP_INSTEADOF
27+
// Don't build a fetchspec from the name if none is set
28+
RemoteCreateSkipDefaultFetchspec RemoteCreateOptionsFlag = C.GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC
29+
)
30+
31+
// RemoteCreateOptions contains options for creating a remote
32+
type RemoteCreateOptions struct {
33+
Name string
34+
FetchSpec string
35+
Flags RemoteCreateOptionsFlag
36+
}
37+
2138
type TransferProgress struct {
2239
TotalObjects uint
2340
IndexedObjects uint
@@ -539,6 +556,28 @@ func (c *RemoteCollection) Create(name string, url string) (*Remote, error) {
539556
return remote, nil
540557
}
541558

559+
//CreateWithOptions Creates a repository object with extended options.
560+
func (c *RemoteCollection) CreateWithOptions(url string, option *RemoteCreateOptions) (*Remote, error) {
561+
remote := &Remote{repo: c.repo}
562+
563+
curl := C.CString(url)
564+
defer C.free(unsafe.Pointer(curl))
565+
566+
runtime.LockOSThread()
567+
defer runtime.UnlockOSThread()
568+
569+
copts := populateRemoteCreateOptions(&C.git_remote_create_options{}, option, c.repo)
570+
defer freeRemoteCreateOptions(copts)
571+
ret := C.git_remote_create_with_opts(&remote.ptr, curl, copts)
572+
runtime.KeepAlive(c.repo)
573+
if ret < 0 {
574+
return nil, MakeGitError(ret)
575+
}
576+
577+
runtime.SetFinalizer(remote, (*Remote).Free)
578+
return remote, nil
579+
}
580+
542581
func (c *RemoteCollection) Delete(name string) error {
543582
cname := C.CString(name)
544583
defer C.free(unsafe.Pointer(cname))
@@ -1027,3 +1066,45 @@ func (o *Remote) Prune(callbacks *RemoteCallbacks) error {
10271066
}
10281067
return nil
10291068
}
1069+
1070+
// DefaultApplyOptions returns default options for remote create
1071+
func DefaultRemoteCreateOptions() (*RemoteCreateOptions, error) {
1072+
runtime.LockOSThread()
1073+
defer runtime.UnlockOSThread()
1074+
1075+
opts := C.git_remote_create_options{}
1076+
ecode := C.git_remote_create_options_init(&opts, C.GIT_REMOTE_CREATE_OPTIONS_VERSION)
1077+
if ecode < 0 {
1078+
return nil, MakeGitError(ecode)
1079+
}
1080+
1081+
return &RemoteCreateOptions{
1082+
Flags: RemoteCreateOptionsFlag(opts.flags),
1083+
}, nil
1084+
}
1085+
1086+
func populateRemoteCreateOptions(copts *C.git_remote_create_options, opts *RemoteCreateOptions, repo *Repository) *C.git_remote_create_options {
1087+
C.git_remote_create_options_init(copts, C.GIT_REMOTE_CREATE_OPTIONS_VERSION)
1088+
if opts == nil {
1089+
return nil
1090+
}
1091+
1092+
var cRepository *C.git_repository
1093+
if repo != nil {
1094+
cRepository = repo.ptr
1095+
}
1096+
copts.repository = cRepository
1097+
copts.name = C.CString(opts.Name)
1098+
copts.fetchspec = C.CString(opts.FetchSpec)
1099+
copts.flags = C.uint(opts.Flags)
1100+
1101+
return copts
1102+
}
1103+
1104+
func freeRemoteCreateOptions(ptr *C.git_remote_create_options) {
1105+
if ptr == nil {
1106+
return
1107+
}
1108+
C.free(unsafe.Pointer(ptr.name))
1109+
C.free(unsafe.Pointer(ptr.fetchspec))
1110+
}

remote_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ func TestRemoteConnect(t *testing.T) {
8080
checkFatal(t, err)
8181
}
8282

83+
func TestRemoteConnectOption(t *testing.T) {
84+
t.Parallel()
85+
repo := createTestRepo(t)
86+
defer cleanupTestRepo(t, repo)
87+
88+
config, err := repo.Config()
89+
checkFatal(t, err)
90+
err = config.SetString("url.git@github.com:.insteadof", "https://github.com/")
91+
checkFatal(t, err)
92+
93+
option, err := DefaultRemoteCreateOptions()
94+
checkFatal(t, err)
95+
option.Name = "origin"
96+
option.Flags = RemoteCreateSkipInsteadof
97+
98+
remote, err := repo.Remotes.CreateWithOptions("https://github.com/libgit2/TestGitRepository", option)
99+
checkFatal(t, err)
100+
101+
err = remote.ConnectFetch(nil, nil, nil)
102+
checkFatal(t, err)
103+
}
104+
83105
func TestRemoteLs(t *testing.T) {
84106
t.Parallel()
85107
repo := createTestRepo(t)

0 commit comments

Comments
 (0)