fork of go-git with some jj specific features

plumbing: transport, move client registry

+5 -5
plumbing/server/server_test.go
··· 7 "github.com/go-git/go-git/v5/plumbing/cache" 8 "github.com/go-git/go-git/v5/plumbing/server" 9 "github.com/go-git/go-git/v5/plumbing/transport" 10 - "github.com/go-git/go-git/v5/plumbing/transport/client" 11 "github.com/go-git/go-git/v5/storage/filesystem" 12 "github.com/go-git/go-git/v5/storage/memory" 13 ··· 35 s.client = server.NewServer(s.loader) 36 } 37 38 - s.clientBackup = client.Protocols["file"] 39 - client.Protocols["file"] = s.client 40 } 41 42 func (s *BaseSuite) TearDownSuite(c *C) { 43 if s.clientBackup == nil { 44 - delete(client.Protocols, "file") 45 } else { 46 - client.Protocols["file"] = s.clientBackup 47 } 48 } 49
··· 7 "github.com/go-git/go-git/v5/plumbing/cache" 8 "github.com/go-git/go-git/v5/plumbing/server" 9 "github.com/go-git/go-git/v5/plumbing/transport" 10 + "github.com/go-git/go-git/v5/plumbing/transport/file" 11 "github.com/go-git/go-git/v5/storage/filesystem" 12 "github.com/go-git/go-git/v5/storage/memory" 13 ··· 35 s.client = server.NewServer(s.loader) 36 } 37 38 + s.clientBackup = file.DefaultClient 39 + transport.Register("file", s.client) 40 } 41 42 func (s *BaseSuite) TearDownSuite(c *C) { 43 if s.clientBackup == nil { 44 + transport.Unregister("file") 45 } else { 46 + transport.Register("file", s.clientBackup) 47 } 48 } 49
-51
plumbing/transport/client/client.go
··· 1 - // Package client contains helper function to deal with the different client 2 - // protocols. 3 - package client 4 - 5 - import ( 6 - "fmt" 7 - 8 - "github.com/go-git/go-git/v5/plumbing/transport" 9 - "github.com/go-git/go-git/v5/plumbing/transport/file" 10 - "github.com/go-git/go-git/v5/plumbing/transport/git" 11 - "github.com/go-git/go-git/v5/plumbing/transport/http" 12 - "github.com/go-git/go-git/v5/plumbing/transport/ssh" 13 - ) 14 - 15 - // Protocols are the protocols supported by default. 16 - var Protocols = map[string]transport.Transport{ 17 - "http": http.DefaultClient, 18 - "https": http.DefaultClient, 19 - "ssh": ssh.DefaultClient, 20 - "git": git.DefaultClient, 21 - "file": file.DefaultClient, 22 - } 23 - 24 - // InstallProtocol adds or modifies an existing protocol. 25 - func InstallProtocol(scheme string, c transport.Transport) { 26 - if c == nil { 27 - delete(Protocols, scheme) 28 - return 29 - } 30 - 31 - Protocols[scheme] = c 32 - } 33 - 34 - // NewClient returns the appropriate client among of the set of known protocols: 35 - // http://, https://, ssh:// and file://. 36 - // See `InstallProtocol` to add or modify protocols. 37 - func NewClient(endpoint *transport.Endpoint) (transport.Transport, error) { 38 - return getTransport(endpoint) 39 - } 40 - 41 - func getTransport(endpoint *transport.Endpoint) (transport.Transport, error) { 42 - f, ok := Protocols[endpoint.Protocol] 43 - if !ok { 44 - return nil, fmt.Errorf("unsupported scheme %q", endpoint.Protocol) 45 - } 46 - 47 - if f == nil { 48 - return nil, fmt.Errorf("malformed client for scheme %q, client is defined as nil", endpoint.Protocol) 49 - } 50 - return f, nil 51 - }
···
+15 -15
plumbing/transport/client/client_test.go plumbing/transport/registry_test.go
··· 1 - package client 2 3 import ( 4 "net/http" 5 - "testing" 6 7 - "github.com/go-git/go-git/v5/plumbing/transport" 8 9 . "gopkg.in/check.v1" 10 ) 11 - 12 - func Test(t *testing.T) { TestingT(t) } 13 14 type ClientSuite struct{} 15 ··· 19 e, err := transport.NewEndpoint("ssh://github.com/src-d/go-git") 20 c.Assert(err, IsNil) 21 22 - output, err := NewClient(e) 23 c.Assert(err, IsNil) 24 c.Assert(output, NotNil) 25 } ··· 28 e, err := transport.NewEndpoint("unknown://github.com/src-d/go-git") 29 c.Assert(err, IsNil) 30 31 - _, err = NewClient(e) 32 c.Assert(err, NotNil) 33 } 34 35 func (s *ClientSuite) TestNewClientNil(c *C) { 36 - Protocols["newscheme"] = nil 37 e, err := transport.NewEndpoint("newscheme://github.com/src-d/go-git") 38 c.Assert(err, IsNil) 39 40 - _, err = NewClient(e) 41 c.Assert(err, NotNil) 42 } 43 44 func (s *ClientSuite) TestInstallProtocol(c *C) { 45 - InstallProtocol("newscheme", &dummyClient{}) 46 - c.Assert(Protocols["newscheme"], NotNil) 47 } 48 49 func (s *ClientSuite) TestInstallProtocolNilValue(c *C) { 50 - InstallProtocol("newscheme", &dummyClient{}) 51 - InstallProtocol("newscheme", nil) 52 53 - _, ok := Protocols["newscheme"] 54 - c.Assert(ok, Equals, false) 55 } 56 57 type dummyClient struct {
··· 1 + package transport_test 2 3 import ( 4 "net/http" 5 6 + _ "github.com/go-git/go-git/v5/plumbing/transport/ssh" // ssh transport 7 8 + "github.com/go-git/go-git/v5/plumbing/transport" 9 . "gopkg.in/check.v1" 10 ) 11 12 type ClientSuite struct{} 13 ··· 17 e, err := transport.NewEndpoint("ssh://github.com/src-d/go-git") 18 c.Assert(err, IsNil) 19 20 + output, err := transport.Get(e.Protocol) 21 c.Assert(err, IsNil) 22 c.Assert(output, NotNil) 23 } ··· 26 e, err := transport.NewEndpoint("unknown://github.com/src-d/go-git") 27 c.Assert(err, IsNil) 28 29 + _, err = transport.Get(e.Protocol) 30 c.Assert(err, NotNil) 31 } 32 33 func (s *ClientSuite) TestNewClientNil(c *C) { 34 + transport.Register("newscheme", nil) 35 e, err := transport.NewEndpoint("newscheme://github.com/src-d/go-git") 36 c.Assert(err, IsNil) 37 38 + _, err = transport.Get(e.Protocol) 39 c.Assert(err, NotNil) 40 } 41 42 func (s *ClientSuite) TestInstallProtocol(c *C) { 43 + transport.Register("newscheme", &dummyClient{}) 44 + p, err := transport.Get("newscheme") 45 + c.Assert(err, IsNil) 46 + c.Assert(p, NotNil) 47 } 48 49 func (s *ClientSuite) TestInstallProtocolNilValue(c *C) { 50 + transport.Register("newscheme", &dummyClient{}) 51 + transport.Unregister("newscheme") 52 53 + _, err := transport.Get("newscheme") 54 + c.Assert(err, NotNil) 55 } 56 57 type dummyClient struct {
+3 -3
plumbing/transport/client/example_test.go plumbing/transport/registry_example_test.go
··· 1 - package client_test 2 3 import ( 4 "crypto/tls" 5 "net/http" 6 7 - "github.com/go-git/go-git/v5/plumbing/transport/client" 8 githttp "github.com/go-git/go-git/v5/plumbing/transport/http" 9 ) 10 ··· 17 } 18 19 // Install it as default client for https URLs. 20 - client.InstallProtocol("https", githttp.NewClient(httpClient)) 21 }
··· 1 + package transport_test 2 3 import ( 4 "crypto/tls" 5 "net/http" 6 7 + "github.com/go-git/go-git/v5/plumbing/transport" 8 githttp "github.com/go-git/go-git/v5/plumbing/transport/http" 9 ) 10 ··· 17 } 18 19 // Install it as default client for https URLs. 20 + transport.Register("https", githttp.NewClient(httpClient)) 21 }
+4
plumbing/transport/file/client.go
··· 13 "golang.org/x/sys/execabs" 14 ) 15 16 // DefaultClient is the default local client. 17 var DefaultClient = NewClient( 18 transport.UploadPackServiceName,
··· 13 "golang.org/x/sys/execabs" 14 ) 15 16 + func init() { 17 + transport.Register("file", DefaultClient) 18 + } 19 + 20 // DefaultClient is the default local client. 21 var DefaultClient = NewClient( 22 transport.UploadPackServiceName,
+4
plumbing/transport/git/common.go
··· 11 "github.com/go-git/go-git/v5/utils/ioutil" 12 ) 13 14 // DefaultClient is the default git client. 15 var DefaultClient = transport.NewClient(&runner{}) 16
··· 11 "github.com/go-git/go-git/v5/utils/ioutil" 12 ) 13 14 + func init() { 15 + transport.Register("git", DefaultClient) 16 + } 17 + 18 // DefaultClient is the default git client. 19 var DefaultClient = transport.NewClient(&runner{}) 20
+5
plumbing/transport/http/common.go
··· 22 "github.com/golang/groupcache/lru" 23 ) 24 25 // it requires a bytes.Buffer, because we need to know the length 26 func applyHeadersToRequest(req *http.Request, content *bytes.Buffer, host string, requestType string) { 27 req.Header.Add("User-Agent", "git/1.0")
··· 22 "github.com/golang/groupcache/lru" 23 ) 24 25 + func init() { 26 + transport.Register("http", DefaultClient) 27 + transport.Register("https", DefaultClient) 28 + } 29 + 30 // it requires a bytes.Buffer, because we need to know the length 31 func applyHeadersToRequest(req *http.Request, content *bytes.Buffer, host string, requestType string) { 32 req.Header.Add("User-Agent", "git/1.0")
+39
plumbing/transport/registry.go
···
··· 1 + package transport 2 + 3 + import ( 4 + "fmt" 5 + "sync" 6 + ) 7 + 8 + // registry are the protocols supported by default. 9 + var ( 10 + registry = map[string]Transport{} 11 + mtx sync.Mutex 12 + ) 13 + 14 + // Register adds or modifies an existing protocol. 15 + func Register(protocol string, c Transport) { 16 + mtx.Lock() 17 + defer mtx.Unlock() 18 + registry[protocol] = c 19 + } 20 + 21 + // Unregister removes a protocol from the list of supported protocols. 22 + func Unregister(scheme string) { 23 + mtx.Lock() 24 + defer mtx.Unlock() 25 + delete(registry, scheme) 26 + } 27 + 28 + // Get returns the appropriate client for the given protocol. 29 + func Get(p string) (Transport, error) { 30 + f, ok := registry[p] 31 + if !ok { 32 + return nil, fmt.Errorf("unsupported scheme %q", p) 33 + } 34 + 35 + if f == nil { 36 + return nil, fmt.Errorf("malformed client for scheme %q, client is defined as nil", p) 37 + } 38 + return f, nil 39 + }
+4
plumbing/transport/ssh/common.go
··· 17 "golang.org/x/net/proxy" 18 ) 19 20 // DefaultClient is the default SSH client. 21 var DefaultClient = NewClient(nil) 22
··· 17 "golang.org/x/net/proxy" 18 ) 19 20 + func init() { 21 + transport.Register("ssh", DefaultClient) 22 + } 23 + 24 // DefaultClient is the default SSH client. 25 var DefaultClient = NewClient(nil) 26
+1 -2
remote.go
··· 21 "github.com/go-git/go-git/v5/plumbing/revlist" 22 "github.com/go-git/go-git/v5/plumbing/storer" 23 "github.com/go-git/go-git/v5/plumbing/transport" 24 - "github.com/go-git/go-git/v5/plumbing/transport/client" 25 "github.com/go-git/go-git/v5/storage" 26 "github.com/go-git/go-git/v5/storage/filesystem" 27 "github.com/go-git/go-git/v5/storage/memory" ··· 539 ep.CaBundle = cabundle 540 ep.Proxy = proxyOpts 541 542 - c, err := client.NewClient(ep) 543 if err != nil { 544 return nil, nil, err 545 }
··· 21 "github.com/go-git/go-git/v5/plumbing/revlist" 22 "github.com/go-git/go-git/v5/plumbing/storer" 23 "github.com/go-git/go-git/v5/plumbing/transport" 24 "github.com/go-git/go-git/v5/storage" 25 "github.com/go-git/go-git/v5/storage/filesystem" 26 "github.com/go-git/go-git/v5/storage/memory" ··· 538 ep.CaBundle = cabundle 539 ep.Proxy = proxyOpts 540 541 + c, err := transport.Get(ep.Protocol) 542 if err != nil { 543 return nil, nil, err 544 }
+9
transport.go
···
··· 1 + package git 2 + 3 + // Default supported transports. 4 + import ( 5 + _ "github.com/go-git/go-git/v5/plumbing/transport/file" // file transport 6 + _ "github.com/go-git/go-git/v5/plumbing/transport/git" // git transport 7 + _ "github.com/go-git/go-git/v5/plumbing/transport/http" // http transport 8 + _ "github.com/go-git/go-git/v5/plumbing/transport/ssh" // ssh transport 9 + )