Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remote: add keep-alive and references to the repository #387

Merged
merged 2 commits into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: go

go:
- 1.5
- 1.6
- 1.7
- 1.8
- tip

script: make test-static
Expand Down
51 changes: 39 additions & 12 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ type ProxyOptions struct {
type Remote struct {
ptr *C.git_remote
callbacks RemoteCallbacks
repo *Repository
}

type CertificateKind uint
Expand Down Expand Up @@ -370,6 +371,7 @@ func RemoteIsValidName(name string) bool {
func (r *Remote) Free() {
runtime.SetFinalizer(r, nil)
C.git_remote_free(r.ptr)
r.ptr = nil
}

type RemoteCollection struct {
Expand All @@ -383,6 +385,7 @@ func (c *RemoteCollection) List() ([]string, error) {
defer runtime.UnlockOSThread()

ecode := C.git_remote_list(&r, c.repo.ptr)
runtime.KeepAlive(c.repo)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
Expand All @@ -393,7 +396,7 @@ func (c *RemoteCollection) List() ([]string, error) {
}

func (c *RemoteCollection) Create(name string, url string) (*Remote, error) {
remote := &Remote{}
remote := &Remote{repo: c.repo}

cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
Expand All @@ -419,14 +422,15 @@ func (c *RemoteCollection) Delete(name string) error {
defer runtime.UnlockOSThread()

ret := C.git_remote_delete(c.repo.ptr, cname)
runtime.KeepAlive(c.repo)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}

func (c *RemoteCollection) CreateWithFetchspec(name string, url string, fetch string) (*Remote, error) {
remote := &Remote{}
remote := &Remote{repo: c.repo}

cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
Expand All @@ -447,7 +451,7 @@ func (c *RemoteCollection) CreateWithFetchspec(name string, url string, fetch st
}

func (c *RemoteCollection) CreateAnonymous(url string) (*Remote, error) {
remote := &Remote{}
remote := &Remote{repo: c.repo}

curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
Expand All @@ -464,7 +468,7 @@ func (c *RemoteCollection) CreateAnonymous(url string) (*Remote, error) {
}

func (c *RemoteCollection) Lookup(name string) (*Remote, error) {
remote := &Remote{}
remote := &Remote{repo: c.repo}

cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
Expand All @@ -481,15 +485,21 @@ func (c *RemoteCollection) Lookup(name string) (*Remote, error) {
}

func (o *Remote) Name() string {
return C.GoString(C.git_remote_name(o.ptr))
s := C.git_remote_name(o.ptr)
runtime.KeepAlive(o)
return C.GoString(s)
}

func (o *Remote) Url() string {
return C.GoString(C.git_remote_url(o.ptr))
s := C.git_remote_url(o.ptr)
runtime.KeepAlive(o)
return C.GoString(s)
}

func (o *Remote) PushUrl() string {
return C.GoString(C.git_remote_pushurl(o.ptr))
s := C.git_remote_pushurl(o.ptr)
runtime.KeepAlive(o)
return C.GoString(s)
}

func (c *RemoteCollection) Rename(remote, newname string) ([]string, error) {
Expand All @@ -504,6 +514,7 @@ func (c *RemoteCollection) Rename(remote, newname string) ([]string, error) {
defer runtime.UnlockOSThread()

ret := C.git_remote_rename(&cproblems, c.repo.ptr, cremote, cnewname)
runtime.KeepAlive(c.repo)
if ret < 0 {
return []string{}, MakeGitError(ret)
}
Expand All @@ -522,6 +533,7 @@ func (c *RemoteCollection) SetUrl(remote, url string) error {
defer runtime.UnlockOSThread()

ret := C.git_remote_set_url(c.repo.ptr, cremote, curl)
runtime.KeepAlive(c.repo)
if ret < 0 {
return MakeGitError(ret)
}
Expand All @@ -538,6 +550,7 @@ func (c *RemoteCollection) SetPushUrl(remote, url string) error {
defer runtime.UnlockOSThread()

ret := C.git_remote_set_pushurl(c.repo.ptr, cremote, curl)
runtime.KeepAlive(c.repo)
if ret < 0 {
return MakeGitError(ret)
}
Expand All @@ -554,6 +567,7 @@ func (c *RemoteCollection) AddFetch(remote, refspec string) error {
defer runtime.UnlockOSThread()

ret := C.git_remote_add_fetch(c.repo.ptr, cremote, crefspec)
runtime.KeepAlive(c.repo)
if ret < 0 {
return MakeGitError(ret)
}
Expand Down Expand Up @@ -605,6 +619,7 @@ func (o *Remote) FetchRefspecs() ([]string, error) {
defer runtime.UnlockOSThread()

ret := C.git_remote_get_fetch_refspecs(&crefspecs, o.ptr)
runtime.KeepAlive(o)
if ret < 0 {
return nil, MakeGitError(ret)
}
Expand All @@ -624,6 +639,7 @@ func (c *RemoteCollection) AddPush(remote, refspec string) error {
defer runtime.UnlockOSThread()

ret := C.git_remote_add_push(c.repo.ptr, cremote, crefspec)
runtime.KeepAlive(c.repo)
if ret < 0 {
return MakeGitError(ret)
}
Expand All @@ -641,12 +657,16 @@ func (o *Remote) PushRefspecs() ([]string, error) {
return nil, MakeGitError(ret)
}
defer C.git_strarray_free(&crefspecs)
runtime.KeepAlive(o)

refspecs := makeStringsFromCStrings(crefspecs.strings, int(crefspecs.count))
return refspecs, nil
}

func (o *Remote) RefspecCount() uint {
return uint(C.git_remote_refspec_count(o.ptr))
count := C.git_remote_refspec_count(o.ptr)
runtime.KeepAlive(o)
return uint(count)
}

func populateFetchOptions(options *C.git_fetch_options, opts *FetchOptions) {
Expand Down Expand Up @@ -706,6 +726,7 @@ func (o *Remote) Fetch(refspecs []string, opts *FetchOptions, msg string) error
defer runtime.UnlockOSThread()

ret := C.git_remote_fetch(o.ptr, &crefspecs, coptions, cmsg)
runtime.KeepAlive(o)
if ret < 0 {
return MakeGitError(ret)
}
Expand Down Expand Up @@ -734,17 +755,18 @@ func (o *Remote) Connect(direction ConnectDirection, callbacks *RemoteCallbacks,
var cproxy C.git_proxy_options
populateProxyOptions(&cproxy, proxyOpts)
defer freeProxyOptions(&cproxy)

cheaders := C.git_strarray{}
cheaders.count = C.size_t(len(headers))
cheaders.strings = makeCStringsFromStrings(headers)
defer freeStrarray(&cheaders)


runtime.LockOSThread()
defer runtime.UnlockOSThread()

if ret := C.git_remote_connect(o.ptr, C.git_direction(direction), &ccallbacks, &cproxy, &cheaders); ret != 0 {
ret := C.git_remote_connect(o.ptr, C.git_direction(direction), &ccallbacks, &cproxy, &cheaders)
runtime.KeepAlive(o)
if ret != 0 {
return MakeGitError(ret)
}
return nil
Expand All @@ -755,6 +777,7 @@ func (o *Remote) Disconnect() {
defer runtime.UnlockOSThread()

C.git_remote_disconnect(o.ptr)
runtime.KeepAlive(o)
}

func (o *Remote) Ls(filterRefs ...string) ([]RemoteHead, error) {
Expand All @@ -765,7 +788,9 @@ func (o *Remote) Ls(filterRefs ...string) ([]RemoteHead, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

if ret := C.git_remote_ls(&refs, &length, o.ptr); ret != 0 {
ret := C.git_remote_ls(&refs, &length, o.ptr)
runtime.KeepAlive(o)
if ret != 0 {
return nil, MakeGitError(ret)
}

Expand Down Expand Up @@ -820,6 +845,7 @@ func (o *Remote) Push(refspecs []string, opts *PushOptions) error {
defer runtime.UnlockOSThread()

ret := C.git_remote_push(o.ptr, &crefspecs, coptions)
runtime.KeepAlive(o)
if ret < 0 {
return MakeGitError(ret)
}
Expand All @@ -838,6 +864,7 @@ func (o *Remote) Prune(callbacks *RemoteCallbacks) error {
defer runtime.UnlockOSThread()

ret := C.git_remote_prune(o.ptr, &ccallbacks)
runtime.KeepAlive(o)
if ret < 0 {
return MakeGitError(ret)
}
Expand Down