Support git_remote_create_with_opts #733

Merged
bc-lee merged 4 commits from feature/645-git-remote-create-with-opts into master 2021-02-03 20:58:32 -06:00
2 changed files with 103 additions and 0 deletions
Showing only changes of commit 5551581893 - Show all commits

View File

@ -18,6 +18,23 @@ import (
"unsafe" "unsafe"
) )
type RemoteCreate uint
const (
// Ignore the repository apply.insteadOf configuration
RemoteCreateSkipInsteadof RemoteCreate = C.GIT_REMOTE_CREATE_SKIP_INSTEADOF
// Don't build a fetchspec from the name if none is set
RemoteCreateSkipDefaultFetchspec RemoteCreate = C.GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC
)
// RemoteCreateOptions contains options for creating a remote
type RemoteCreateOptions struct {
Repository *Repository
Name string
FetchSpec string
Flags RemoteCreate
}
type TransferProgress struct { type TransferProgress struct {
TotalObjects uint TotalObjects uint
IndexedObjects uint IndexedObjects uint
@ -539,6 +556,26 @@ func (c *RemoteCollection) Create(name string, url string) (*Remote, error) {
return remote, nil return remote, nil
} }
func (c *RemoteCollection) CreateOptions(url string, option *RemoteCreateOptions) (*Remote, error) {
remote := &Remote{repo: c.repo}
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
opts := remoteCreateOptionsToC(option)
defer freeRemoteCreateOptions(opts)
ret := C.git_remote_create_with_opts(&remote.ptr, curl, opts)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(remote, (*Remote).Free)
return remote, nil
}
func (c *RemoteCollection) Delete(name string) error { func (c *RemoteCollection) Delete(name string) error {
cname := C.CString(name) cname := C.CString(name)
defer C.free(unsafe.Pointer(cname)) defer C.free(unsafe.Pointer(cname))
@ -1027,3 +1064,46 @@ func (o *Remote) Prune(callbacks *RemoteCallbacks) error {
} }
return nil return nil
lhchavez commented 2021-02-03 09:17:19 -06:00 (Migrated from github.com)
Review
func populateRemoteCreateOptions(copts *C.git_remote_create_options, opts *RemoteCreateOptionsrepository *Repository) *C.git_remote_create_options {

for consistency with all the other populateXxxOptions() functions.

```suggestion func populateRemoteCreateOptions(copts *C.git_remote_create_options, opts *RemoteCreateOptionsrepository *Repository) *C.git_remote_create_options { ``` for consistency with all the other `populateXxxOptions()` functions.
lhchavez commented 2021-02-03 09:18:40 -06:00 (Migrated from github.com)
Review
	if opts == nil {

having a nil repository is completely legal.

```suggestion if opts == nil { ``` having a nil `repository` is completely legal.
lhchavez commented 2021-02-03 09:20:39 -06:00 (Migrated from github.com)
Review
	if ecode < 0 {

for consistency with all other places ecode is used.

```suggestion if ecode < 0 { ``` for consistency with all other places `ecode` is used.
} }
// DefaultApplyOptions returns default options for remote create
func DefaultRemoteCreateOptions() (*RemoteCreateOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
opts := C.git_remote_create_options{}
ecode := C.git_remote_create_options_init(&opts, C.GIT_REMOTE_CREATE_OPTIONS_VERSION)
if int(ecode) != 0 {
return nil, MakeGitError(ecode)
}
return &RemoteCreateOptions{
Repository: nil,
Flags: 0,
}, nil
}
func remoteCreateOptionsToC(opts *RemoteCreateOptions) (copts *C.git_remote_create_options) {
if opts == nil {
return
}
cName := C.CString(opts.Name)
cFetchSpec := C.CString(opts.FetchSpec)
copts = &C.git_remote_create_options{
version: C.GIT_REMOTE_CREATE_OPTIONS_VERSION,
repository: opts.Repository.ptr,
name: cName,
fetchspec: cFetchSpec,
flags: C.uint(opts.Flags),
}
return copts
}
func freeRemoteCreateOptions(ptr *C.git_remote_create_options) {
if ptr == nil {
return
}
C.free(unsafe.Pointer(ptr.name))
C.free(unsafe.Pointer(ptr.fetchspec))
}

View File

@ -80,6 +80,29 @@ func TestRemoteConnect(t *testing.T) {
checkFatal(t, err) checkFatal(t, err)
} }
func TestRemoteConnectOption(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
config, err := repo.Config()
checkFatal(t, err)
err = config.SetString("url.git@github.com:.insteadof", "https://github.com/")
checkFatal(t, err)
option, err := DefaultRemoteCreateOptions()
checkFatal(t, err)
option.Repository = repo
option.Name = "origin"
option.Flags = RemoteCreateSkipInsteadof
remote, err := repo.Remotes.CreateOptions("https://github.com/libgit2/TestGitRepository", option)
checkFatal(t, err)
err = remote.ConnectFetch(nil, nil, nil)
checkFatal(t, err)
}
func TestRemoteLs(t *testing.T) { func TestRemoteLs(t *testing.T) {
t.Parallel() t.Parallel()
repo := createTestRepo(t) repo := createTestRepo(t)