@@ -108,13 +108,17 @@ func GetAllCommits(ctx *context.APIContext) {
108108 // in: query
109109 // description: SHA or branch to start listing commits from (usually 'master')
110110 // type: string
111+ // - name: path
112+ // in: query
113+ // description: filepath of a file/dir
114+ // type: string
111115 // - name: page
112116 // in: query
113117 // description: page number of results to return (1-based)
114118 // type: integer
115119 // - name: limit
116120 // in: query
117- // description: page size of results
121+ // description: page size of results (ignored if used with 'path')
118122 // type: integer
119123 // responses:
120124 // "200":
@@ -149,46 +153,73 @@ func GetAllCommits(ctx *context.APIContext) {
149153 }
150154
151155 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+ }
152186
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 ()
157189 if err != nil {
158- ctx .Error (http .StatusInternalServerError , "GetHEADBranch " , err )
190+ ctx .Error (http .StatusInternalServerError , "GetCommitsCount " , err )
159191 return
160192 }
161193
162- baseCommit , err = gitRepo .GetBranchCommit (head .Name )
194+ // Query commits
195+ commits , err = baseCommit .CommitsByRange (listOptions .Page , listOptions .PageSize )
163196 if err != nil {
164- ctx .Error (http .StatusInternalServerError , "GetCommit " , err )
197+ ctx .Error (http .StatusInternalServerError , "CommitsByRange " , err )
165198 return
166199 }
167200 } 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 )
170206 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 )
172211 return
173212 }
174- }
175213
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+ }
181219 }
182220
183221 pageCount := int (math .Ceil (float64 (commitsCountTotal ) / float64 (listOptions .PageSize )))
184222
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-
192223 userCache := make (map [string ]* user_model.User )
193224
194225 apiCommits := make ([]* api.Commit , len (commits ))
0 commit comments