From 70b014daabef1e474ab7c7bf13c02f5bbc512380 Mon Sep 17 00:00:00 2001 From: Byoungchan Lee Date: Thu, 4 Feb 2021 11:58:31 +0900 Subject: [PATCH 1/2] Support git_remote_create_with_opts (#733) Closes #645 (cherry picked from commit 73d97b9bbe7c9a7747af20aad670baba33b5e390) --- remote.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ remote_test.go | 22 ++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/remote.go b/remote.go index 122c74d..b8b1f64 100644 --- a/remote.go +++ b/remote.go @@ -17,6 +17,23 @@ import ( "unsafe" ) +// RemoteCreateOptionsFlag is Remote creation options flags +type RemoteCreateOptionsFlag uint + +const ( + // Ignore the repository apply.insteadOf configuration + RemoteCreateSkipInsteadof RemoteCreateOptionsFlag = C.GIT_REMOTE_CREATE_SKIP_INSTEADOF + // Don't build a fetchspec from the name if none is set + RemoteCreateSkipDefaultFetchspec RemoteCreateOptionsFlag = C.GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC +) + +// RemoteCreateOptions contains options for creating a remote +type RemoteCreateOptions struct { + Name string + FetchSpec string + Flags RemoteCreateOptionsFlag +} + type TransferProgress struct { TotalObjects uint IndexedObjects uint @@ -537,6 +554,28 @@ func (c *RemoteCollection) Create(name string, url string) (*Remote, error) { return remote, nil } +//CreateWithOptions Creates a repository object with extended options. +func (c *RemoteCollection) CreateWithOptions(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() + + copts := populateRemoteCreateOptions(&C.git_remote_create_options{}, option, c.repo) + defer freeRemoteCreateOptions(copts) + ret := C.git_remote_create_with_opts(&remote.ptr, curl, copts) + runtime.KeepAlive(c.repo) + if ret < 0 { + return nil, MakeGitError(ret) + } + + runtime.SetFinalizer(remote, (*Remote).Free) + return remote, nil +} + func (c *RemoteCollection) Delete(name string) error { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) @@ -1025,3 +1064,45 @@ func (o *Remote) Prune(callbacks *RemoteCallbacks) error { } return nil } + +// 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 ecode < 0 { + return nil, MakeGitError(ecode) + } + + return &RemoteCreateOptions{ + Flags: RemoteCreateOptionsFlag(opts.flags), + }, nil +} + +func populateRemoteCreateOptions(copts *C.git_remote_create_options, opts *RemoteCreateOptions, repo *Repository) *C.git_remote_create_options { + C.git_remote_create_options_init(copts, C.GIT_REMOTE_CREATE_OPTIONS_VERSION) + if opts == nil { + return nil + } + + var cRepository *C.git_repository + if repo != nil { + cRepository = repo.ptr + } + copts.repository = cRepository + copts.name = C.CString(opts.Name) + copts.fetchspec = C.CString(opts.FetchSpec) + copts.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)) +} diff --git a/remote_test.go b/remote_test.go index ccd436a..259957e 100644 --- a/remote_test.go +++ b/remote_test.go @@ -80,6 +80,28 @@ func TestRemoteConnect(t *testing.T) { 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.Name = "origin" + option.Flags = RemoteCreateSkipInsteadof + + remote, err := repo.Remotes.CreateWithOptions("https://github.com/libgit2/TestGitRepository", option) + checkFatal(t, err) + + err = remote.ConnectFetch(nil, nil, nil) + checkFatal(t, err) +} + func TestRemoteLs(t *testing.T) { t.Parallel() repo := createTestRepo(t) -- 2.45.2 From 43fb918458d8d8a3e9184f5a86f0073017e95051 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Fri, 5 Feb 2021 20:09:36 -0800 Subject: [PATCH 2/2] Use the older init api --- remote.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/remote.go b/remote.go index b8b1f64..8fc430c 100644 --- a/remote.go +++ b/remote.go @@ -1071,7 +1071,7 @@ func DefaultRemoteCreateOptions() (*RemoteCreateOptions, error) { defer runtime.UnlockOSThread() opts := C.git_remote_create_options{} - ecode := C.git_remote_create_options_init(&opts, C.GIT_REMOTE_CREATE_OPTIONS_VERSION) + ecode := C.git_remote_create_init_options(&opts, C.GIT_REMOTE_CREATE_OPTIONS_VERSION) if ecode < 0 { return nil, MakeGitError(ecode) } @@ -1082,7 +1082,7 @@ func DefaultRemoteCreateOptions() (*RemoteCreateOptions, error) { } func populateRemoteCreateOptions(copts *C.git_remote_create_options, opts *RemoteCreateOptions, repo *Repository) *C.git_remote_create_options { - C.git_remote_create_options_init(copts, C.GIT_REMOTE_CREATE_OPTIONS_VERSION) + C.git_remote_create_init_options(copts, C.GIT_REMOTE_CREATE_OPTIONS_VERSION) if opts == nil { return nil } -- 2.45.2