@@ -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 )
172
211
return
173
212
}
174
- }
175
213
176
- // Total commit count
177
- commitsCountTotal , err := baseCommit . CommitsCount ()
178
- if err != nil {
179
- ctx . Error ( http . StatusInternalServerError , "GetCommitsCount" , err )
180
- return
214
+ commits , err = gitRepo . CommitsByFileAndRange ( sha , path , listOptions . Page )
215
+ if err != nil {
216
+ ctx . Error ( http . StatusInternalServerError , "CommitsByFileAndRange" , err )
217
+ return
218
+ }
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 ))
0 commit comments