@@ -19,10 +19,12 @@ import (
1919//checkIsValidRequest check if it a valid request in case of bad request it write the response to ctx.
2020func checkIsValidRequest (ctx * context.Context ) bool {
2121 if ! setting .LFS .StartServer {
22+ log .Debug ("Attempt to access LFS server but LFS server is disabled" )
2223 writeStatus (ctx , 404 )
2324 return false
2425 }
2526 if ! MetaMatcher (ctx .Req ) {
27+ log .Info ("Attempt access LOCKs without accepting the correct media type: %s" , metaMediaType )
2628 writeStatus (ctx , 400 )
2729 return false
2830 }
@@ -47,7 +49,7 @@ func handleLockListOut(ctx *context.Context, repo *models.Repository, lock *mode
4749 return
4850 }
4951 ctx .JSON (500 , api.LFSLockError {
50- Message : "unable to list locks : " + err . Error () ,
52+ Message : "unable to list locks : Internal Server Error" ,
5153 })
5254 return
5355 }
@@ -65,6 +67,7 @@ func handleLockListOut(ctx *context.Context, repo *models.Repository, lock *mode
6567// GetListLockHandler list locks
6668func GetListLockHandler (ctx * context.Context ) {
6769 if ! checkIsValidRequest (ctx ) {
70+ // Status is written in checkIsValidRequest
6871 return
6972 }
7073 ctx .Resp .Header ().Set ("Content-Type" , metaMediaType )
@@ -87,7 +90,17 @@ func GetListLockHandler(ctx *context.Context) {
8790 })
8891 return
8992 }
90- //TODO handle query cursor and limit
93+
94+ cursor := ctx .QueryInt ("cursor" )
95+ if cursor < 0 {
96+ cursor = 0
97+ }
98+ limit := ctx .QueryInt ("limit" )
99+ if limit > setting .LFS .LocksPagingNum && setting .LFS .LocksPagingNum > 0 {
100+ limit = setting .LFS .LocksPagingNum
101+ } else if limit < 0 {
102+ limit = 0
103+ }
91104 id := ctx .Query ("id" )
92105 if id != "" { //Case where we request a specific id
93106 v , err := strconv .ParseInt (id , 10 , 64 )
@@ -98,37 +111,50 @@ func GetListLockHandler(ctx *context.Context) {
98111 return
99112 }
100113 lock , err := models .GetLFSLockByID (v )
114+ if err != nil && ! models .IsErrLFSLockNotExist (err ) {
115+ log .Error ("Unable to get lock with ID[%s]: Error: %v" , v , err )
116+ }
101117 handleLockListOut (ctx , repository , lock , err )
102118 return
103119 }
104120
105121 path := ctx .Query ("path" )
106122 if path != "" { //Case where we request a specific id
107123 lock , err := models .GetLFSLock (repository , path )
124+ if err != nil && ! models .IsErrLFSLockNotExist (err ) {
125+ log .Error ("Unable to get lock for repository %-v with path %s: Error: %v" , repository , path , err )
126+ }
108127 handleLockListOut (ctx , repository , lock , err )
109128 return
110129 }
111130
112131 //If no query params path or id
113- lockList , err := models .GetLFSLockByRepoID (repository .ID , 0 , 0 )
132+ lockList , err := models .GetLFSLockByRepoID (repository .ID , cursor , limit )
114133 if err != nil {
134+ log .Error ("Unable to list locks for repository ID[%d]: Error: %v" , repository .ID , err )
115135 ctx .JSON (500 , api.LFSLockError {
116- Message : "unable to list locks : " + err . Error () ,
136+ Message : "unable to list locks : Internal Server Error" ,
117137 })
118138 return
119139 }
120140 lockListAPI := make ([]* api.LFSLock , len (lockList ))
141+ next := ""
121142 for i , l := range lockList {
122143 lockListAPI [i ] = l .APIFormat ()
123144 }
145+ if limit > 0 && len (lockList ) == limit {
146+ next = strconv .Itoa (cursor + 1 )
147+ }
124148 ctx .JSON (200 , api.LFSLockList {
125149 Locks : lockListAPI ,
150+ Next : next ,
126151 })
127152}
128153
129154// PostLockHandler create lock
130155func PostLockHandler (ctx * context.Context ) {
131156 if ! checkIsValidRequest (ctx ) {
157+ // Status is written in checkIsValidRequest
132158 return
133159 }
134160 ctx .Resp .Header ().Set ("Content-Type" , metaMediaType )
@@ -139,7 +165,7 @@ func PostLockHandler(ctx *context.Context) {
139165
140166 repository , err := models .GetRepositoryByOwnerAndName (userName , repoName )
141167 if err != nil {
142- log .Debug ( "Could not find repository: %s/%s - %s " , userName , repoName , err )
168+ log .Error ( "Unable to get repository: %s/%s Error: %v " , userName , repoName , err )
143169 writeStatus (ctx , 404 )
144170 return
145171 }
@@ -159,6 +185,7 @@ func PostLockHandler(ctx *context.Context) {
159185 defer bodyReader .Close ()
160186 dec := json .NewDecoder (bodyReader )
161187 if err := dec .Decode (& req ); err != nil {
188+ log .Warn ("Failed to decode lock request as json. Error: %v" , err )
162189 writeStatus (ctx , 400 )
163190 return
164191 }
@@ -183,8 +210,9 @@ func PostLockHandler(ctx *context.Context) {
183210 })
184211 return
185212 }
213+ log .Error ("Unable to CreateLFSLock in repository %-v at %s for user %-v: Error: %v" , repository , req .Path , ctx .User , err )
186214 ctx .JSON (500 , api.LFSLockError {
187- Message : "internal server error : " + err . Error () ,
215+ Message : "internal server error : Internal Server Error" ,
188216 })
189217 return
190218 }
@@ -194,6 +222,7 @@ func PostLockHandler(ctx *context.Context) {
194222// VerifyLockHandler list locks for verification
195223func VerifyLockHandler (ctx * context.Context ) {
196224 if ! checkIsValidRequest (ctx ) {
225+ // Status is written in checkIsValidRequest
197226 return
198227 }
199228 ctx .Resp .Header ().Set ("Content-Type" , metaMediaType )
@@ -204,7 +233,7 @@ func VerifyLockHandler(ctx *context.Context) {
204233
205234 repository , err := models .GetRepositoryByOwnerAndName (userName , repoName )
206235 if err != nil {
207- log .Debug ( "Could not find repository: %s/%s - %s " , userName , repoName , err )
236+ log .Error ( "Unable to get repository: %s/%s Error: %v " , userName , repoName , err )
208237 writeStatus (ctx , 404 )
209238 return
210239 }
@@ -219,14 +248,28 @@ func VerifyLockHandler(ctx *context.Context) {
219248 return
220249 }
221250
222- //TODO handle body json cursor and limit
223- lockList , err := models .GetLFSLockByRepoID (repository .ID , 0 , 0 )
251+ cursor := ctx .QueryInt ("cursor" )
252+ if cursor < 0 {
253+ cursor = 0
254+ }
255+ limit := ctx .QueryInt ("limit" )
256+ if limit > setting .LFS .LocksPagingNum && setting .LFS .LocksPagingNum > 0 {
257+ limit = setting .LFS .LocksPagingNum
258+ } else if limit < 0 {
259+ limit = 0
260+ }
261+ lockList , err := models .GetLFSLockByRepoID (repository .ID , cursor , limit )
224262 if err != nil {
263+ log .Error ("Unable to list locks for repository ID[%d]: Error: %v" , repository .ID , err )
225264 ctx .JSON (500 , api.LFSLockError {
226- Message : "unable to list locks : " + err . Error () ,
265+ Message : "unable to list locks : Internal Server Error" ,
227266 })
228267 return
229268 }
269+ next := ""
270+ if limit > 0 && len (lockList ) == limit {
271+ next = strconv .Itoa (cursor + 1 )
272+ }
230273 lockOursListAPI := make ([]* api.LFSLock , 0 , len (lockList ))
231274 lockTheirsListAPI := make ([]* api.LFSLock , 0 , len (lockList ))
232275 for _ , l := range lockList {
@@ -239,12 +282,14 @@ func VerifyLockHandler(ctx *context.Context) {
239282 ctx .JSON (200 , api.LFSLockListVerify {
240283 Ours : lockOursListAPI ,
241284 Theirs : lockTheirsListAPI ,
285+ Next : next ,
242286 })
243287}
244288
245289// UnLockHandler delete locks
246290func UnLockHandler (ctx * context.Context ) {
247291 if ! checkIsValidRequest (ctx ) {
292+ // Status is written in checkIsValidRequest
248293 return
249294 }
250295 ctx .Resp .Header ().Set ("Content-Type" , metaMediaType )
@@ -255,7 +300,7 @@ func UnLockHandler(ctx *context.Context) {
255300
256301 repository , err := models .GetRepositoryByOwnerAndName (userName , repoName )
257302 if err != nil {
258- log .Debug ( "Could not find repository: %s/%s - %s " , userName , repoName , err )
303+ log .Error ( "Unable to get repository: %s/%s Error: %v " , userName , repoName , err )
259304 writeStatus (ctx , 404 )
260305 return
261306 }
@@ -275,6 +320,7 @@ func UnLockHandler(ctx *context.Context) {
275320 defer bodyReader .Close ()
276321 dec := json .NewDecoder (bodyReader )
277322 if err := dec .Decode (& req ); err != nil {
323+ log .Warn ("Failed to decode lock request as json. Error: %v" , err )
278324 writeStatus (ctx , 400 )
279325 return
280326 }
@@ -288,8 +334,9 @@ func UnLockHandler(ctx *context.Context) {
288334 })
289335 return
290336 }
337+ log .Error ("Unable to DeleteLFSLockByID[%d] by user %-v with force %t: Error: %v" , ctx .ParamsInt64 ("lid" ), ctx .User , req .Force , err )
291338 ctx .JSON (500 , api.LFSLockError {
292- Message : "unable to delete lock : " + err . Error () ,
339+ Message : "unable to delete lock : Internal Server Error" ,
293340 })
294341 return
295342 }
0 commit comments