locking: use an interface for lock client

Right now, all of our locking happens over HTTP.  However, in the
future, we'll add support for a pure SSH-based protocol, and we'll want
to use a different concrete implementation, so let's make our lockClient
an interface and switch the concrete implementation to httpLockClient.
This commit is contained in:
brian m. carlson 2021-04-06 14:54:12 +00:00
parent a0998e88ba
commit 73b33753c0
No known key found for this signature in database
GPG Key ID: 2D0C9BC12F82B3A1
3 changed files with 18 additions and 11 deletions

@ -10,7 +10,14 @@ import (
"github.com/git-lfs/git-lfs/lfshttp"
)
type lockClient struct {
type lockClient interface {
Lock(remote string, lockReq *lockRequest) (*lockResponse, int, error)
Unlock(ref *git.Ref, remote, id string, force bool) (*unlockResponse, int, error)
Search(remote string, searchReq *lockSearchRequest) (*lockList, int, error)
SearchVerifiable(remote string, vreq *lockVerifiableRequest) (*lockVerifiableList, int, error)
}
type httpLockClient struct {
*lfsapi.Client
}
@ -50,7 +57,7 @@ type lockResponse struct {
RequestID string `json:"request_id,omitempty"`
}
func (c *lockClient) Lock(remote string, lockReq *lockRequest) (*lockResponse, int, error) {
func (c *httpLockClient) Lock(remote string, lockReq *lockRequest) (*lockResponse, int, error) {
e := c.Endpoints.Endpoint("upload", remote)
req, err := c.NewRequest("POST", e, "locks", lockReq)
if err != nil {
@ -101,7 +108,7 @@ type unlockResponse struct {
RequestID string `json:"request_id,omitempty"`
}
func (c *lockClient) Unlock(ref *git.Ref, remote, id string, force bool) (*unlockResponse, int, error) {
func (c *httpLockClient) Unlock(ref *git.Ref, remote, id string, force bool) (*unlockResponse, int, error) {
e := c.Endpoints.Endpoint("upload", remote)
suffix := fmt.Sprintf("locks/%s/unlock", id)
req, err := c.NewRequest("POST", e, suffix, &unlockRequest{
@ -198,7 +205,7 @@ type lockList struct {
RequestID string `json:"request_id,omitempty"`
}
func (c *lockClient) Search(remote string, searchReq *lockSearchRequest) (*lockList, int, error) {
func (c *httpLockClient) Search(remote string, searchReq *lockSearchRequest) (*lockList, int, error) {
e := c.Endpoints.Endpoint("download", remote)
req, err := c.NewRequest("GET", e, "locks", nil)
if err != nil {
@ -267,7 +274,7 @@ type lockVerifiableList struct {
RequestID string `json:"request_id,omitempty"`
}
func (c *lockClient) SearchVerifiable(remote string, vreq *lockVerifiableRequest) (*lockVerifiableList, int, error) {
func (c *httpLockClient) SearchVerifiable(remote string, vreq *lockVerifiableRequest) (*lockVerifiableList, int, error) {
e := c.Endpoints.Endpoint("upload", remote)
req, err := c.NewRequest("POST", e, "locks/verify", vreq)
if err != nil {

@ -60,7 +60,7 @@ func TestAPILock(t *testing.T) {
}))
require.Nil(t, err)
lc := &lockClient{Client: c}
lc := &httpLockClient{Client: c}
lockRes, status, err := lc.Lock("", &lockRequest{Path: "request", Ref: &lockRef{Name: "refs/heads/master"}})
require.Nil(t, err)
assert.Equal(t, 200, status)
@ -108,7 +108,7 @@ func TestAPIUnlock(t *testing.T) {
}))
require.Nil(t, err)
lc := &lockClient{Client: c}
lc := &httpLockClient{Client: c}
unlockRes, status, err := lc.Unlock(&git.Ref{
Name: "master",
Sha: "6161616161616161616161616161616161616161",
@ -156,7 +156,7 @@ func TestAPISearch(t *testing.T) {
}))
require.Nil(t, err)
lc := &lockClient{Client: c}
lc := &httpLockClient{Client: c}
locks, status, err := lc.Search("", &lockSearchRequest{
Filters: []lockFilter{
{Property: "a", Value: "A"},
@ -211,7 +211,7 @@ func TestAPISearchVerifiable(t *testing.T) {
}))
require.Nil(t, err)
lc := &lockClient{Client: c}
lc := &httpLockClient{Client: c}
locks, status, err := lc.SearchVerifiable("", &lockVerifiableRequest{
Cursor: "cursor",
Limit: 5,

@ -42,7 +42,7 @@ type LockCacher interface {
type Client struct {
Remote string
RemoteRef *git.Ref
client *lockClient
client lockClient
cache LockCacher
cacheDir string
cfg *config.Configuration
@ -63,7 +63,7 @@ type Client struct {
func NewClient(remote string, lfsClient *lfsapi.Client, cfg *config.Configuration) (*Client, error) {
return &Client{
Remote: remote,
client: &lockClient{Client: lfsClient},
client: &httpLockClient{Client: lfsClient},
cache: &nilLockCacher{},
cfg: cfg,
ModifyIgnoredFiles: lfsClient.GitEnv().Bool("lfs.lockignoredfiles", false),