Skip to content

Commit 4ebd8c0

Browse files
committed
Use GitHub-compatible endpoint
1 parent f91aba5 commit 4ebd8c0

File tree

3 files changed

+57
-112
lines changed

3 files changed

+57
-112
lines changed

integrations/api_repo_git_commits_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func TestGetFileHistory(t *testing.T) {
140140
session := loginUser(t, user.Name)
141141
token := getTokenForLoggedInUser(t, session)
142142

143-
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/history/readme.md?token="+token+"&sha=good-sign", user.Name)
143+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?path=readme.md&token="+token+"&sha=good-sign", user.Name)
144144
resp := session.MakeRequest(t, req, http.StatusOK)
145145

146146
var apiData []api.Commit

routers/api/v1/api.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,6 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
971971
m.Get("/blobs/{sha}", context.RepoRefForAPI, repo.GetBlob)
972972
m.Get("/tags/{sha}", context.RepoRefForAPI, repo.GetAnnotatedTag)
973973
m.Get("/notes/{sha}", repo.GetNote)
974-
m.Get("/history/*", context.RepoRefForAPI, reqRepoReader(unit.TypeCode), repo.GetFileHistory)
975974
}, reqRepoReader(unit.TypeCode))
976975
m.Group("/contents", func() {
977976
m.Get("", repo.GetContentsList)

routers/api/v1/repo/commits.go

Lines changed: 56 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
211+
return
212+
}
213+
214+
commits, err = gitRepo.CommitsByFileAndRange(sha, path, listOptions.PageSize)
215+
if err != nil {
216+
ctx.Error(http.StatusInternalServerError, "CommitsByFileAndRange", err)
172217
return
173218
}
174-
}
175-
176-
// Total commit count
177-
commitsCountTotal, err := baseCommit.CommitsCount()
178-
if err != nil {
179-
ctx.Error(http.StatusInternalServerError, "GetCommitsCount", err)
180-
return
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))
@@ -264,88 +295,3 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) {
264295
return
265296
}
266297
}
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

Comments
 (0)