Skip to content

Commit

Permalink
bugfix: HTTPS Clone fails with remote pointer not found using Go tran…
Browse files Browse the repository at this point in the history
…sport (#836) (#842)

Fixes: #836

Changes:

* adding a weak bool param for Remote
* create a new remote in the smartTransportCallback incase one is not found

(cherry picked from commit 0e8009f)
  • Loading branch information
codexetreme authored and lhchavez committed Oct 23, 2021
1 parent 5ae36de commit 4d85201
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
15 changes: 15 additions & 0 deletions clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package git

import (
"io/ioutil"
"os"
"testing"
)

Expand Down Expand Up @@ -76,3 +77,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)
}
}
15 changes: 15 additions & 0 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,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 {
Expand Down Expand Up @@ -591,6 +594,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()
}

Expand Down Expand Up @@ -1220,3 +1226,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,
}
}
7 changes: 4 additions & 3 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ void _go_git_setup_smart_subtransport_stream(_go_managed_smart_subtransport_stre
*/
import "C"
import (
"errors"
"fmt"
"io"
"reflect"
Expand Down Expand Up @@ -294,8 +293,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{
Expand Down

0 comments on commit 4d85201

Please # to comment.