@@ -108,13 +108,17 @@ func GetAllCommits(ctx *context.APIContext) {
108
108
// in: query
109
109
// description: SHA or branch to start listing commits from (usually 'master')
110
110
// type: string
111
+ // - name: path
112
+ // in: query
113
+ // description: filepath of a file/dir
114
+ // type: string
111
115
// - name: page
112
116
// in: query
113
117
// description: page number of results to return (1-based)
114
118
// type: integer
115
119
// - name: limit
116
120
// in: query
117
- // description: page size of results
121
+ // description: page size of results (ignored if used with 'path')
118
122
// type: integer
119
123
// responses:
120
124
// "200":
@@ -149,46 +153,73 @@ func GetAllCommits(ctx *context.APIContext) {
149
153
}
150
154
151
155
sha := ctx .FormString ("sha" )
156
+ path := ctx .FormString ("path" )
157
+
158
+ var (
159
+ commitsCountTotal int64
160
+ commits []* git.Commit
161
+ )
162
+
163
+ if len (path ) == 0 {
164
+ var baseCommit * git.Commit
165
+ if len (sha ) == 0 {
166
+ // no sha supplied - use default branch
167
+ head , err := gitRepo .GetHEADBranch ()
168
+ if err != nil {
169
+ ctx .Error (http .StatusInternalServerError , "GetHEADBranch" , err )
170
+ return
171
+ }
172
+
173
+ baseCommit , err = gitRepo .GetBranchCommit (head .Name )
174
+ if err != nil {
175
+ ctx .Error (http .StatusInternalServerError , "GetCommit" , err )
176
+ return
177
+ }
178
+ } else {
179
+ // get commit specified by sha
180
+ baseCommit , err = gitRepo .GetCommit (sha )
181
+ if err != nil {
182
+ ctx .Error (http .StatusInternalServerError , "GetCommit" , err )
183
+ return
184
+ }
185
+ }
152
186
153
- var baseCommit * git.Commit
154
- if len (sha ) == 0 {
155
- // no sha supplied - use default branch
156
- head , err := gitRepo .GetHEADBranch ()
187
+ // Total commit count
188
+ commitsCountTotal , err = baseCommit .CommitsCount ()
157
189
if err != nil {
158
- ctx .Error (http .StatusInternalServerError , "GetHEADBranch " , err )
190
+ ctx .Error (http .StatusInternalServerError , "GetCommitsCount " , err )
159
191
return
160
192
}
161
193
162
- baseCommit , err = gitRepo .GetBranchCommit (head .Name )
194
+ // Query commits
195
+ commits , err = baseCommit .CommitsByRange (listOptions .Page , listOptions .PageSize )
163
196
if err != nil {
164
- ctx .Error (http .StatusInternalServerError , "GetCommit " , err )
197
+ ctx .Error (http .StatusInternalServerError , "CommitsByRange " , err )
165
198
return
166
199
}
167
200
} else {
168
- // get commit specified by sha
169
- baseCommit , err = gitRepo .GetCommit (sha )
201
+ if len (sha ) == 0 {
202
+ sha = ctx .Repo .Repository .DefaultBranch
203
+ }
204
+
205
+ commitsCountTotal , err = gitRepo .FileCommitsCount (sha , path )
170
206
if err != nil {
171
- ctx .Error (http .StatusInternalServerError , "GetCommit" , err )
207
+ ctx .Error (http .StatusInternalServerError , "FileCommitsCount" , err )
208
+ return
209
+ } else if commitsCountTotal == 0 {
210
+ ctx .NotFound ("FileCommitsCount" , nil )
211
+ return
212
+ }
213
+
214
+ commits , err = gitRepo .CommitsByFileAndRange (sha , path , listOptions .PageSize )
215
+ if err != nil {
216
+ ctx .Error (http .StatusInternalServerError , "CommitsByFileAndRange" , err )
172
217
return
173
218
}
174
- }
175
-
176
- // Total commit count
177
- commitsCountTotal , err := baseCommit .CommitsCount ()
178
- if err != nil {
179
- ctx .Error (http .StatusInternalServerError , "GetCommitsCount" , err )
180
- return
181
219
}
182
220
183
221
pageCount := int (math .Ceil (float64 (commitsCountTotal ) / float64 (listOptions .PageSize )))
184
222
185
- // Query commits
186
- commits , err := baseCommit .CommitsByRange (listOptions .Page , listOptions .PageSize )
187
- if err != nil {
188
- ctx .Error (http .StatusInternalServerError , "CommitsByRange" , err )
189
- return
190
- }
191
-
192
223
userCache := make (map [string ]* user_model.User )
193
224
194
225
apiCommits := make ([]* api.Commit , len (commits ))
@@ -264,88 +295,3 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) {
264
295
return
265
296
}
266
297
}
267
-
268
- // GetFileHistory get a file's commit history
269
- func GetFileHistory (ctx * context.APIContext ) {
270
- // swagger:operation GET /repos/{owner}/{repo}/git/history/{filepath} repository repoGetFileHistory
271
- // ---
272
- // summary: Get the commit history of a file or directory
273
- // produces:
274
- // - application/json
275
- // parameters:
276
- // - name: owner
277
- // in: path
278
- // description: owner of the repo
279
- // type: string
280
- // required: true
281
- // - name: repo
282
- // in: path
283
- // description: name of the repo
284
- // type: string
285
- // required: true
286
- // - name: filepath
287
- // in: path
288
- // description: filepath of the file/directory
289
- // type: string
290
- // required: true
291
- // - name: ref
292
- // in: query
293
- // description: "The name of the ref (branch/tag). Default the repository’s default branch"
294
- // type: string
295
- // required: false
296
- // - name: page
297
- // in: query
298
- // description: page number of results to return (1-based)
299
- // type: integer
300
- // responses:
301
- // "200":
302
- // "$ref": "#/responses/CommitList"
303
- // "404":
304
- // "$ref": "#/responses/notFound"
305
-
306
- if ctx .Repo .Repository .IsEmpty {
307
- ctx .NotFound ()
308
- return
309
- }
310
-
311
- ref := ctx .FormTrim ("ref" )
312
- if len (ref ) < 1 {
313
- ref = ctx .Repo .Repository .DefaultBranch
314
- }
315
-
316
- page := ctx .FormInt ("page" )
317
- if page <= 1 {
318
- page = 1
319
- }
320
-
321
- commitsCount , err := ctx .Repo .GitRepo .FileCommitsCount (ref , ctx .Repo .TreePath )
322
- if err != nil {
323
- ctx .Error (http .StatusInternalServerError , "FileCommitsCount" , err )
324
- return
325
- } else if commitsCount == 0 {
326
- ctx .NotFound ("FileCommitsCount" , nil )
327
- return
328
- }
329
-
330
- commits , err := ctx .Repo .GitRepo .CommitsByFileAndRange (ref , ctx .Repo .TreePath , page )
331
- if err != nil {
332
- ctx .Error (http .StatusInternalServerError , "CommitsByFileAndRange" , err )
333
- return
334
- }
335
-
336
- userCache := make (map [string ]* user_model.User )
337
- apiCommits := make ([]* api.Commit , len (commits ))
338
- for i , commit := range commits {
339
- // Create json struct
340
- apiCommits [i ], err = convert .ToCommit (ctx .Repo .Repository , commit , userCache )
341
- if err != nil {
342
- ctx .Error (http .StatusInternalServerError , "toCommit" , err )
343
- return
344
- }
345
- }
346
-
347
- ctx .SetLinkHeader (int (commitsCount ), setting .Git .CommitsRangeSize )
348
- ctx .SetTotalCountHeader (commitsCount )
349
-
350
- ctx .JSON (http .StatusOK , & apiCommits )
351
- }
0 commit comments