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