Skip to content

Commit

Permalink
transport: ssh, fix cloning large repositories (#272)
Browse files Browse the repository at this point in the history
* Fix cloning large repositories

Ignore the error on close when the connection is already closed

Fixes #70

* Compatibility for go 1.13

Because it's required by the pipeline

* Add test for allowing to close a command when the client is already closed

This test is for issue #70

* Add debug information for broken test
  • Loading branch information
dcu committed Mar 26, 2021
1 parent bf3471d commit 1f32838
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
9 changes: 8 additions & 1 deletion plumbing/transport/ssh/common.go
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"reflect"
"strconv"
"strings"

"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/internal/common"
Expand Down Expand Up @@ -90,8 +91,14 @@ func (c *command) Close() error {
//XXX: If did read the full packfile, then the session might be already
// closed.
_ = c.Session.Close()
err := c.client.Close()

return c.client.Close()
//XXX: in go1.16+ we can use errors.Is(err, net.ErrClosed)
if err != nil && strings.HasSuffix(err.Error(), "use of closed network connection") {
return nil
}

return err
}

// connect connects to the SSH server, unless a AuthMethod was set with
Expand Down
21 changes: 21 additions & 0 deletions plumbing/transport/ssh/common_test.go
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/kevinburke/ssh_config"
"golang.org/x/crypto/ssh"
stdssh "golang.org/x/crypto/ssh"
. "gopkg.in/check.v1"
)

Expand Down Expand Up @@ -93,6 +94,26 @@ func (s *SuiteCommon) TestDefaultSSHConfigWildcard(c *C) {
c.Assert(cmd.getHostWithPort(), Equals, "github.com:22")
}

func (s *SuiteCommon) TestIssue70(c *C) {
uploadPack := &UploadPackSuite{}
uploadPack.SetUpSuite(c)

config := &ssh.ClientConfig{
HostKeyCallback: stdssh.InsecureIgnoreHostKey(),
}
r := &runner{
config: config,
}

cmd, err := r.Command("command", uploadPack.newEndpoint(c, "endpoint"), uploadPack.EmptyAuth)
c.Assert(err, IsNil)

c.Assert(cmd.(*command).client.Close(), IsNil)

err = cmd.Close()
c.Assert(err, IsNil)
}

type mockSSHConfig struct {
Values map[string]map[string]string
}
Expand Down
4 changes: 4 additions & 0 deletions worktree_commit_test.go
Expand Up @@ -3,9 +3,11 @@ package git
import (
"bytes"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -309,6 +311,8 @@ func (s *WorktreeSuite) TestJustStoreObjectsNotAlreadyStored(c *C) {

infoLicenseSecond, err := fsDotgit.Stat(filepath.Join("objects", "04", "84eba0d41636ba71fa612c78559cd6c3006cde"))
c.Assert(err, IsNil)

log.Printf("comparing mod time: %v == %v on %v (%v)", infoLicenseSecond.ModTime(), infoLicense.ModTime(), runtime.GOOS, runtime.GOARCH)
c.Assert(infoLicenseSecond.ModTime(), Equals, infoLicense.ModTime()) // object of LICENSE should have the same timestamp because no additional write operation was performed
}

Expand Down

0 comments on commit 1f32838

Please sign in to comment.