1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
package locking
import (
"fmt"
"net/http"
"strconv"
"github.com/git-lfs/git-lfs/v3/errors"
"github.com/git-lfs/git-lfs/v3/git"
"github.com/git-lfs/git-lfs/v3/lfsapi"
"github.com/git-lfs/git-lfs/v3/lfshttp"
"github.com/git-lfs/git-lfs/v3/tr"
)
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
}
type lockRef struct {
Name string `json:"name,omitempty"`
}
// LockRequest encapsulates the payload sent across the API when a client would
// like to obtain a lock against a particular path on a given remote.
type lockRequest struct {
// Path is the path that the client would like to obtain a lock against.
Path string `json:"path"`
Ref *lockRef `json:"ref,omitempty"`
}
// LockResponse encapsulates the information sent over the API in response to
// a `LockRequest`.
type lockResponse struct {
// Lock is the Lock that was optionally created in response to the
// payload that was sent (see above). If the lock already exists, then
// the existing lock is sent in this field instead, and the author of
// that lock remains the same, meaning that the client failed to obtain
// that lock. An HTTP status of "409 - Conflict" is used here.
//
// If the lock was unable to be created, this field will hold the
// zero-value of Lock and the Err field will provide a more detailed set
// of information.
//
// If an error was experienced in creating this lock, then the
// zero-value of Lock should be sent here instead.
Lock *Lock `json:"lock"`
// Message is the optional error that was encountered while trying to create
// the above lock.
Message string `json:"message,omitempty"`
DocumentationURL string `json:"documentation_url,omitempty"`
RequestID string `json:"request_id,omitempty"`
}
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 {
return nil, 0, err
}
req = c.Client.LogRequest(req, "lfs.locks.lock")
res, err := c.DoAPIRequestWithAuth(remote, req)
if err != nil {
if res != nil {
return nil, res.StatusCode, err
}
return nil, 0, err
}
lockRes := &lockResponse{}
err = lfshttp.DecodeJSON(res, lockRes)
if err != nil {
return nil, res.StatusCode, err
}
if lockRes.Lock == nil && len(lockRes.Message) == 0 {
return nil, res.StatusCode, errors.New(tr.Tr.Get("invalid server response"))
}
return lockRes, res.StatusCode, nil
}
// UnlockRequest encapsulates the data sent in an API request to remove a lock.
type unlockRequest struct {
// Force determines whether or not the lock should be "forcibly"
// unlocked; that is to say whether or not a given individual should be
// able to break a different individual's lock.
Force bool `json:"force"`
Ref *lockRef `json:"ref,omitempty"`
}
// UnlockResponse is the result sent back from the API when asked to remove a
// lock.
type unlockResponse struct {
// Lock is the lock corresponding to the asked-about lock in the
// `UnlockPayload` (see above). If no matching lock was found, this
// field will take the zero-value of Lock, and Err will be non-nil.
Lock *Lock `json:"lock"`
// Message is an optional field which holds any error that was experienced
// while removing the lock.
Message string `json:"message,omitempty"`
DocumentationURL string `json:"documentation_url,omitempty"`
RequestID string `json:"request_id,omitempty"`
}
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{
Force: force,
Ref: &lockRef{Name: ref.Refspec()},
})
if err != nil {
return nil, 0, err
}
req = c.Client.LogRequest(req, "lfs.locks.unlock")
res, err := c.DoAPIRequestWithAuth(remote, req)
if err != nil {
if res != nil {
return nil, res.StatusCode, err
}
return nil, 0, err
}
unlockRes := &unlockResponse{}
err = lfshttp.DecodeJSON(res, unlockRes)
if err != nil {
return nil, res.StatusCode, err
}
if unlockRes.Lock == nil && len(unlockRes.Message) == 0 {
return nil, res.StatusCode, errors.New(tr.Tr.Get("invalid server response"))
}
return unlockRes, res.StatusCode, nil
}
// Filter represents a single qualifier to apply against a set of locks.
type lockFilter struct {
// Property is the property to search against.
// Value is the value that the property must take.
Property, Value string
}
// LockSearchRequest encapsulates the request sent to the server when the client
// would like a list of locks that match the given criteria.
type lockSearchRequest struct {
// Filters is the set of filters to query against. If the client wishes
// to obtain a list of all locks, an empty array should be passed here.
Filters []lockFilter
// Cursor is an optional field used to tell the server which lock was
// seen last, if scanning through multiple pages of results.
//
// Servers must return a list of locks sorted in reverse chronological
// order, so the Cursor provides a consistent method of viewing all
// locks, even if more were created between two requests.
Cursor string
// Limit is the maximum number of locks to return in a single page.
Limit int
Refspec string
}
func (r *lockSearchRequest) QueryValues() map[string]string {
q := make(map[string]string)
for _, filter := range r.Filters {
q[filter.Property] = filter.Value
}
if len(r.Cursor) > 0 {
q["cursor"] = r.Cursor
}
if r.Limit > 0 {
q["limit"] = strconv.Itoa(r.Limit)
}
if len(r.Refspec) > 0 {
q["refspec"] = r.Refspec
}
return q
}
// LockList encapsulates a set of Locks.
type lockList struct {
// Locks is the set of locks returned back, typically matching the query
// parameters sent in the LockListRequest call. If no locks were matched
// from a given query, then `Locks` will be represented as an empty
// array.
Locks []Lock `json:"locks"`
// NextCursor returns the Id of the Lock the client should update its
// cursor to, if there are multiple pages of results for a particular
// `LockListRequest`.
NextCursor string `json:"next_cursor,omitempty"`
// Message populates any error that was encountered during the search. If no
// error was encountered and the operation was successful, then a value
// of nil will be passed here.
Message string `json:"message,omitempty"`
DocumentationURL string `json:"documentation_url,omitempty"`
RequestID string `json:"request_id,omitempty"`
}
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 {
return nil, 0, err
}
q := req.URL.Query()
for key, value := range searchReq.QueryValues() {
q.Add(key, value)
}
req.URL.RawQuery = q.Encode()
req = c.Client.LogRequest(req, "lfs.locks.search")
res, err := c.DoAPIRequestWithAuth(remote, req)
if err != nil {
if res != nil {
return nil, res.StatusCode, err
}
return nil, 0, err
}
locks := &lockList{}
if res.StatusCode == http.StatusOK {
err = lfshttp.DecodeJSON(res, locks)
}
return locks, res.StatusCode, err
}
// lockVerifiableRequest encapsulates the request sent to the server when the
// client would like a list of locks to verify a Git push.
type lockVerifiableRequest struct {
Ref *lockRef `json:"ref,omitempty"`
// Cursor is an optional field used to tell the server which lock was
// seen last, if scanning through multiple pages of results.
//
// Servers must return a list of locks sorted in reverse chronological
// order, so the Cursor provides a consistent method of viewing all
// locks, even if more were created between two requests.
Cursor string `json:"cursor,omitempty"`
// Limit is the maximum number of locks to return in a single page.
Limit int `json:"limit,omitempty"`
}
// lockVerifiableList encapsulates a set of Locks to verify a Git push.
type lockVerifiableList struct {
// Ours is the set of locks returned back matching filenames that the user
// is allowed to edit.
Ours []Lock `json:"ours"`
// Their is the set of locks returned back matching filenames that the user
// is NOT allowed to edit. Any edits matching these files should reject
// the Git push.
Theirs []Lock `json:"theirs"`
// NextCursor returns the Id of the Lock the client should update its
// cursor to, if there are multiple pages of results for a particular
// `LockListRequest`.
NextCursor string `json:"next_cursor,omitempty"`
// Message populates any error that was encountered during the search. If no
// error was encountered and the operation was successful, then a value
// of nil will be passed here.
Message string `json:"message,omitempty"`
DocumentationURL string `json:"documentation_url,omitempty"`
RequestID string `json:"request_id,omitempty"`
}
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 {
return nil, 0, err
}
req = c.Client.LogRequest(req, "lfs.locks.verify")
res, err := c.DoAPIRequestWithAuth(remote, req)
if err != nil {
if res != nil {
return nil, res.StatusCode, err
}
return nil, 0, err
}
locks := &lockVerifiableList{}
if res.StatusCode == http.StatusOK {
err = lfshttp.DecodeJSON(res, locks)
}
return locks, res.StatusCode, err
}
// User represents the owner of a lock.
type User struct {
// Name is the name of the individual who would like to obtain the
// lock, for instance: "Rick Sanchez".
Name string `json:"name"`
}
func NewUser(name string) *User {
return &User{Name: name}
}
// String implements the fmt.Stringer interface.
func (u *User) String() string {
return u.Name
}
type lockClientInfo struct {
remote string
operation string
}
type genericLockClient struct {
client *lfsapi.Client
lclients map[lockClientInfo]lockClient
}
func newGenericLockClient(client *lfsapi.Client) *genericLockClient {
return &genericLockClient{
client: client,
lclients: make(map[lockClientInfo]lockClient),
}
}
func (c *genericLockClient) getClient(remote, operation string) lockClient {
info := lockClientInfo{
remote: remote,
operation: operation,
}
if client := c.lclients[info]; client != nil {
return client
}
transfer := c.client.SSHTransfer(operation, remote)
var lclient lockClient
if transfer != nil {
lclient = &sshLockClient{transfer: transfer, Client: c.client}
} else {
lclient = &httpLockClient{Client: c.client}
}
c.lclients[info] = lclient
return lclient
}
func (c *genericLockClient) Lock(remote string, lockReq *lockRequest) (*lockResponse, int, error) {
return c.getClient(remote, "upload").Lock(remote, lockReq)
}
func (c *genericLockClient) Unlock(ref *git.Ref, remote, id string, force bool) (*unlockResponse, int, error) {
return c.getClient(remote, "upload").Unlock(ref, remote, id, force)
}
func (c *genericLockClient) Search(remote string, searchReq *lockSearchRequest) (*lockList, int, error) {
return c.getClient(remote, "download").Search(remote, searchReq)
}
func (c *genericLockClient) SearchVerifiable(remote string, vreq *lockVerifiableRequest) (*lockVerifiableList, int, error) {
return c.getClient(remote, "upload").SearchVerifiable(remote, vreq)
}
|