fork of go-git with some jj specific features

plumbing: transport, move client registry

+5 -5
plumbing/server/server_test.go
··· 7 7 "github.com/go-git/go-git/v5/plumbing/cache" 8 8 "github.com/go-git/go-git/v5/plumbing/server" 9 9 "github.com/go-git/go-git/v5/plumbing/transport" 10 - "github.com/go-git/go-git/v5/plumbing/transport/client" 10 + "github.com/go-git/go-git/v5/plumbing/transport/file" 11 11 "github.com/go-git/go-git/v5/storage/filesystem" 12 12 "github.com/go-git/go-git/v5/storage/memory" 13 13 ··· 35 35 s.client = server.NewServer(s.loader) 36 36 } 37 37 38 - s.clientBackup = client.Protocols["file"] 39 - client.Protocols["file"] = s.client 38 + s.clientBackup = file.DefaultClient 39 + transport.Register("file", s.client) 40 40 } 41 41 42 42 func (s *BaseSuite) TearDownSuite(c *C) { 43 43 if s.clientBackup == nil { 44 - delete(client.Protocols, "file") 44 + transport.Unregister("file") 45 45 } else { 46 - client.Protocols["file"] = s.clientBackup 46 + transport.Register("file", s.clientBackup) 47 47 } 48 48 } 49 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 1 + package transport_test 2 2 3 3 import ( 4 4 "net/http" 5 - "testing" 6 5 7 - "github.com/go-git/go-git/v5/plumbing/transport" 6 + _ "github.com/go-git/go-git/v5/plumbing/transport/ssh" // ssh transport 8 7 8 + "github.com/go-git/go-git/v5/plumbing/transport" 9 9 . "gopkg.in/check.v1" 10 10 ) 11 - 12 - func Test(t *testing.T) { TestingT(t) } 13 11 14 12 type ClientSuite struct{} 15 13 ··· 19 17 e, err := transport.NewEndpoint("ssh://github.com/src-d/go-git") 20 18 c.Assert(err, IsNil) 21 19 22 - output, err := NewClient(e) 20 + output, err := transport.Get(e.Protocol) 23 21 c.Assert(err, IsNil) 24 22 c.Assert(output, NotNil) 25 23 } ··· 28 26 e, err := transport.NewEndpoint("unknown://github.com/src-d/go-git") 29 27 c.Assert(err, IsNil) 30 28 31 - _, err = NewClient(e) 29 + _, err = transport.Get(e.Protocol) 32 30 c.Assert(err, NotNil) 33 31 } 34 32 35 33 func (s *ClientSuite) TestNewClientNil(c *C) { 36 - Protocols["newscheme"] = nil 34 + transport.Register("newscheme", nil) 37 35 e, err := transport.NewEndpoint("newscheme://github.com/src-d/go-git") 38 36 c.Assert(err, IsNil) 39 37 40 - _, err = NewClient(e) 38 + _, err = transport.Get(e.Protocol) 41 39 c.Assert(err, NotNil) 42 40 } 43 41 44 42 func (s *ClientSuite) TestInstallProtocol(c *C) { 45 - InstallProtocol("newscheme", &dummyClient{}) 46 - c.Assert(Protocols["newscheme"], NotNil) 43 + transport.Register("newscheme", &dummyClient{}) 44 + p, err := transport.Get("newscheme") 45 + c.Assert(err, IsNil) 46 + c.Assert(p, NotNil) 47 47 } 48 48 49 49 func (s *ClientSuite) TestInstallProtocolNilValue(c *C) { 50 - InstallProtocol("newscheme", &dummyClient{}) 51 - InstallProtocol("newscheme", nil) 50 + transport.Register("newscheme", &dummyClient{}) 51 + transport.Unregister("newscheme") 52 52 53 - _, ok := Protocols["newscheme"] 54 - c.Assert(ok, Equals, false) 53 + _, err := transport.Get("newscheme") 54 + c.Assert(err, NotNil) 55 55 } 56 56 57 57 type dummyClient struct {
+3 -3
plumbing/transport/client/example_test.go plumbing/transport/registry_example_test.go
··· 1 - package client_test 1 + package transport_test 2 2 3 3 import ( 4 4 "crypto/tls" 5 5 "net/http" 6 6 7 - "github.com/go-git/go-git/v5/plumbing/transport/client" 7 + "github.com/go-git/go-git/v5/plumbing/transport" 8 8 githttp "github.com/go-git/go-git/v5/plumbing/transport/http" 9 9 ) 10 10 ··· 17 17 } 18 18 19 19 // Install it as default client for https URLs. 20 - client.InstallProtocol("https", githttp.NewClient(httpClient)) 20 + transport.Register("https", githttp.NewClient(httpClient)) 21 21 }
+4
plumbing/transport/file/client.go
··· 13 13 "golang.org/x/sys/execabs" 14 14 ) 15 15 16 + func init() { 17 + transport.Register("file", DefaultClient) 18 + } 19 + 16 20 // DefaultClient is the default local client. 17 21 var DefaultClient = NewClient( 18 22 transport.UploadPackServiceName,
+4
plumbing/transport/git/common.go
··· 11 11 "github.com/go-git/go-git/v5/utils/ioutil" 12 12 ) 13 13 14 + func init() { 15 + transport.Register("git", DefaultClient) 16 + } 17 + 14 18 // DefaultClient is the default git client. 15 19 var DefaultClient = transport.NewClient(&runner{}) 16 20
+5
plumbing/transport/http/common.go
··· 22 22 "github.com/golang/groupcache/lru" 23 23 ) 24 24 25 + func init() { 26 + transport.Register("http", DefaultClient) 27 + transport.Register("https", DefaultClient) 28 + } 29 + 25 30 // it requires a bytes.Buffer, because we need to know the length 26 31 func applyHeadersToRequest(req *http.Request, content *bytes.Buffer, host string, requestType string) { 27 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 17 "golang.org/x/net/proxy" 18 18 ) 19 19 20 + func init() { 21 + transport.Register("ssh", DefaultClient) 22 + } 23 + 20 24 // DefaultClient is the default SSH client. 21 25 var DefaultClient = NewClient(nil) 22 26
+1 -2
remote.go
··· 21 21 "github.com/go-git/go-git/v5/plumbing/revlist" 22 22 "github.com/go-git/go-git/v5/plumbing/storer" 23 23 "github.com/go-git/go-git/v5/plumbing/transport" 24 - "github.com/go-git/go-git/v5/plumbing/transport/client" 25 24 "github.com/go-git/go-git/v5/storage" 26 25 "github.com/go-git/go-git/v5/storage/filesystem" 27 26 "github.com/go-git/go-git/v5/storage/memory" ··· 539 538 ep.CaBundle = cabundle 540 539 ep.Proxy = proxyOpts 541 540 542 - c, err := client.NewClient(ep) 541 + c, err := transport.Get(ep.Protocol) 543 542 if err != nil { 544 543 return nil, nil, err 545 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 + )