Skip to content

Commit f91aba5

Browse files
committed
Move func to commits.go
1 parent 2ee9ac8 commit f91aba5

File tree

2 files changed

+85
-88
lines changed

2 files changed

+85
-88
lines changed

routers/api/v1/repo/commits.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,88 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) {
264264
return
265265
}
266266
}
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+
}

routers/api/v1/repo/file.go

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ import (
1414
"code.gitea.io/gitea/models"
1515
repo_model "code.gitea.io/gitea/models/repo"
1616
"code.gitea.io/gitea/models/unit"
17-
user_model "code.gitea.io/gitea/models/user"
1817
"code.gitea.io/gitea/modules/context"
19-
"code.gitea.io/gitea/modules/convert"
2018
"code.gitea.io/gitea/modules/git"
21-
"code.gitea.io/gitea/modules/setting"
2219
api "code.gitea.io/gitea/modules/structs"
2320
"code.gitea.io/gitea/modules/web"
2421
"code.gitea.io/gitea/routers/common"
@@ -601,88 +598,3 @@ func GetContentsList(ctx *context.APIContext) {
601598
// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
602599
GetContents(ctx)
603600
}
604-
605-
// GetFileHistory get a file's commit history
606-
func GetFileHistory(ctx *context.APIContext) {
607-
// swagger:operation GET /repos/{owner}/{repo}/git/history/{filepath} repository repoGetFileHistory
608-
// ---
609-
// summary: Get the commit history of a file or directory
610-
// produces:
611-
// - application/json
612-
// parameters:
613-
// - name: owner
614-
// in: path
615-
// description: owner of the repo
616-
// type: string
617-
// required: true
618-
// - name: repo
619-
// in: path
620-
// description: name of the repo
621-
// type: string
622-
// required: true
623-
// - name: filepath
624-
// in: path
625-
// description: filepath of the file/directory
626-
// type: string
627-
// required: true
628-
// - name: ref
629-
// in: query
630-
// description: "The name of the ref (branch/tag). Default the repository’s default branch"
631-
// type: string
632-
// required: false
633-
// - name: page
634-
// in: query
635-
// description: page number of results to return (1-based)
636-
// type: integer
637-
// responses:
638-
// "200":
639-
// "$ref": "#/responses/CommitList"
640-
// "404":
641-
// "$ref": "#/responses/notFound"
642-
643-
if ctx.Repo.Repository.IsEmpty {
644-
ctx.NotFound()
645-
return
646-
}
647-
648-
ref := ctx.FormTrim("ref")
649-
if len(ref) < 1 {
650-
ref = ctx.Repo.Repository.DefaultBranch
651-
}
652-
653-
page := ctx.FormInt("page")
654-
if page <= 1 {
655-
page = 1
656-
}
657-
658-
commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ref, ctx.Repo.TreePath)
659-
if err != nil {
660-
ctx.Error(http.StatusInternalServerError, "FileCommitsCount", err)
661-
return
662-
} else if commitsCount == 0 {
663-
ctx.NotFound("FileCommitsCount", nil)
664-
return
665-
}
666-
667-
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(ref, ctx.Repo.TreePath, page)
668-
if err != nil {
669-
ctx.Error(http.StatusInternalServerError, "CommitsByFileAndRange", err)
670-
return
671-
}
672-
673-
userCache := make(map[string]*user_model.User)
674-
apiCommits := make([]*api.Commit, len(commits))
675-
for i, commit := range commits {
676-
// Create json struct
677-
apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache)
678-
if err != nil {
679-
ctx.Error(http.StatusInternalServerError, "toCommit", err)
680-
return
681-
}
682-
}
683-
684-
ctx.SetLinkHeader(int(commitsCount), setting.Git.CommitsRangeSize)
685-
ctx.SetTotalCountHeader(commitsCount)
686-
687-
ctx.JSON(http.StatusOK, &apiCommits)
688-
}

0 commit comments

Comments
 (0)