bugfix: HTTPS Clone fails with remote pointer not found using Go transport (#836) (#842)

Fixes: #836 

Changes:

* adding a weak bool param for Remote
* create a new remote in the smartTransportCallback incase one is not found
This commit is contained in:
Yashodhan Ghadge 2021-10-23 20:33:10 +03:00 committed by GitHub
parent 6eae74c128
commit 0e8009f00a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package git
import (
"io/ioutil"
"os"
"testing"
)
@ -70,3 +71,17 @@ func TestCloneWithCallback(t *testing.T) {
}
defer remote.Free()
}
// TestCloneWithExternalHTTPUrl
func TestCloneWithExternalHTTPUrl(t *testing.T) {
path, err := ioutil.TempDir("", "git2go")
defer os.RemoveAll(path)
// clone the repo
url := "https://github.com/libgit2/TestGitRepository"
_, err = Clone(url, path, &CloneOptions{})
if err != nil {
t.Fatal("cannot clone remote repo via https, error: ", err)
}
}

View File

@ -182,6 +182,9 @@ type Remote struct {
ptr *C.git_remote
callbacks RemoteCallbacks
repo *Repository
// weak indicates that a remote is a weak pointer and should not be
// freed.
weak bool
}
type remotePointerList struct {
@ -602,6 +605,9 @@ func (r *Remote) free() {
// Free releases the resources of the Remote.
func (r *Remote) Free() {
r.repo.Remotes.untrackRemote(r)
if r.weak {
return
}
r.free()
}
@ -1231,3 +1237,12 @@ func freeRemoteCreateOptions(ptr *C.git_remote_create_options) {
C.free(unsafe.Pointer(ptr.name))
C.free(unsafe.Pointer(ptr.fetchspec))
}
// createNewEmptyRemote used to get a new empty object of *Remote
func createNewEmptyRemote() *Remote {
return &Remote{
callbacks: RemoteCallbacks{},
repo: nil,
weak: false,
}
}

View File

@ -22,7 +22,6 @@ void _go_git_setup_smart_subtransport_stream(_go_managed_smart_subtransport_stre
*/
import "C"
import (
"errors"
"fmt"
"io"
"reflect"
@ -306,8 +305,10 @@ func smartTransportCallback(
registeredSmartTransport := pointerHandles.Get(handle).(*RegisteredSmartTransport)
remote, ok := remotePointers.get(owner)
if !ok {
err := errors.New("remote pointer not found")
return setCallbackError(errorMessage, err)
// create a new empty remote and set it
// as a weak pointer, so that control stays in golang
remote = createNewEmptyRemote()
remote.weak = true
}
managed := &managedSmartSubtransport{