604 lines
16 KiB
Go
604 lines
16 KiB
Go
// Copyright 2018 The Gogs Authors. All rights reserved.
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package repo
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"net/http"
|
|
"sort"
|
|
"strconv"
|
|
|
|
"code.gitea.io/gitea/models"
|
|
"code.gitea.io/gitea/modules/context"
|
|
"code.gitea.io/gitea/modules/convert"
|
|
"code.gitea.io/gitea/modules/git"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
"code.gitea.io/gitea/modules/validation"
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
|
"code.gitea.io/gitea/services/gitdiff"
|
|
)
|
|
|
|
// GetSingleCommit get a commit via sha
|
|
func GetSingleCommit(ctx *context.APIContext) {
|
|
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommit
|
|
// ---
|
|
// summary: Get a single commit from a repository
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: owner
|
|
// in: path
|
|
// description: owner of the repo
|
|
// type: string
|
|
// required: true
|
|
// - name: repo
|
|
// in: path
|
|
// description: name of the repo
|
|
// type: string
|
|
// required: true
|
|
// - name: sha
|
|
// in: path
|
|
// description: a git ref or commit sha
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/Commit"
|
|
// "422":
|
|
// "$ref": "#/responses/validationError"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
sha := ctx.Params(":sha")
|
|
if (validation.GitRefNamePatternInvalid.MatchString(sha) || !validation.CheckGitRefAdditionalRulesValid(sha)) && !git.SHAPattern.MatchString(sha) {
|
|
ctx.Error(http.StatusUnprocessableEntity, "no valid ref or sha", fmt.Sprintf("no valid ref or sha: %s", sha))
|
|
return
|
|
}
|
|
getCommit(ctx, sha)
|
|
}
|
|
|
|
func getCommit(ctx *context.APIContext, identifier string) {
|
|
gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
|
|
return
|
|
}
|
|
defer gitRepo.Close()
|
|
commit, err := gitRepo.GetCommit(identifier)
|
|
if err != nil {
|
|
if git.IsErrNotExist(err) {
|
|
ctx.NotFound(identifier)
|
|
return
|
|
}
|
|
ctx.Error(http.StatusInternalServerError, "gitRepo.GetCommit", err)
|
|
return
|
|
}
|
|
|
|
json, err := convert.ToCommit(ctx.Repo.Repository, commit, nil)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "toCommit", err)
|
|
return
|
|
}
|
|
ctx.JSON(http.StatusOK, json)
|
|
}
|
|
|
|
// GetAllCommits get all commits via
|
|
func GetAllCommits(ctx *context.APIContext) {
|
|
// swagger:operation GET /repos/{owner}/{repo}/commits repository repoGetAllCommits
|
|
// ---
|
|
// summary: Get a list of all commits from a repository
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: owner
|
|
// in: path
|
|
// description: owner of the repo
|
|
// type: string
|
|
// required: true
|
|
// - name: repo
|
|
// in: path
|
|
// description: name of the repo
|
|
// type: string
|
|
// required: true
|
|
// - name: sha
|
|
// in: query
|
|
// description: SHA or branch to start listing commits from (usually 'master')
|
|
// type: string
|
|
// - name: page
|
|
// in: query
|
|
// description: page number of results to return (1-based)
|
|
// type: integer
|
|
// - name: limit
|
|
// in: query
|
|
// description: page size of results
|
|
// type: integer
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/CommitList"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
// "409":
|
|
// "$ref": "#/responses/EmptyRepository"
|
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
|
ctx.JSON(http.StatusConflict, api.APIError{
|
|
Message: "Git Repository is empty.",
|
|
URL: setting.API.SwaggerURL,
|
|
})
|
|
return
|
|
}
|
|
|
|
gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
|
|
return
|
|
}
|
|
defer gitRepo.Close()
|
|
|
|
listOptions := utils.GetListOptions(ctx)
|
|
if listOptions.Page <= 0 {
|
|
listOptions.Page = 1
|
|
}
|
|
|
|
if listOptions.PageSize > setting.Git.CommitsRangeSize {
|
|
listOptions.PageSize = setting.Git.CommitsRangeSize
|
|
}
|
|
|
|
sha := ctx.Query("sha")
|
|
|
|
var baseCommit *git.Commit
|
|
if len(sha) == 0 {
|
|
// no sha supplied - use default branch
|
|
head, err := gitRepo.GetHEADBranch()
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetHEADBranch", err)
|
|
return
|
|
}
|
|
|
|
baseCommit, err = gitRepo.GetBranchCommit(head.Name)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
|
|
return
|
|
}
|
|
} else {
|
|
// get commit specified by sha
|
|
baseCommit, err = gitRepo.GetCommit(sha)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
// Total commit count
|
|
commitsCountTotal, err := baseCommit.CommitsCount()
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetCommitsCount", err)
|
|
return
|
|
}
|
|
|
|
pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(listOptions.PageSize)))
|
|
|
|
// Query commits
|
|
commits, err := baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "CommitsByRange", err)
|
|
return
|
|
}
|
|
|
|
userCache := make(map[string]*models.User)
|
|
|
|
apiCommits := make([]*api.Commit, commits.Len())
|
|
|
|
i := 0
|
|
for commitPointer := commits.Front(); commitPointer != nil; commitPointer = commitPointer.Next() {
|
|
commit := commitPointer.Value.(*git.Commit)
|
|
|
|
// Create json struct
|
|
apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "toCommit", err)
|
|
return
|
|
}
|
|
|
|
i++
|
|
}
|
|
|
|
// kept for backwards compatibility
|
|
ctx.Header().Set("X-Page", strconv.Itoa(listOptions.Page))
|
|
ctx.Header().Set("X-PerPage", strconv.Itoa(listOptions.PageSize))
|
|
ctx.Header().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10))
|
|
ctx.Header().Set("X-PageCount", strconv.Itoa(pageCount))
|
|
ctx.Header().Set("X-HasMore", strconv.FormatBool(listOptions.Page < pageCount))
|
|
|
|
ctx.SetLinkHeader(int(commitsCountTotal), listOptions.PageSize)
|
|
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", commitsCountTotal))
|
|
ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, X-PerPage, X-Total, X-PageCount, X-HasMore, Link")
|
|
|
|
ctx.JSON(http.StatusOK, &apiCommits)
|
|
}
|
|
|
|
func GetAllCommitsSliceByTime(ctx *context.APIContext) {
|
|
// swagger:operation GET /repos/{owner}/{repo}/commits_slice repository repoGetAllCommitsSlice
|
|
// ---
|
|
// summary: Get a list of all commits from a repository and sort by time
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: owner
|
|
// in: path
|
|
// description: owner of the repo
|
|
// type: string
|
|
// required: true
|
|
// - name: repo
|
|
// in: path
|
|
// description: name of the repo
|
|
// type: string
|
|
// required: true
|
|
// - name: sha
|
|
// in: query
|
|
// description: SHA or branch to start listing commits from (usually 'master')
|
|
// type: string
|
|
// - name: page
|
|
// in: query
|
|
// description: page number of results to return (1-based)
|
|
// type: integer
|
|
// - name: limit
|
|
// in: query
|
|
// description: page size of results
|
|
// type: integer
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/CommitList"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
// "409":
|
|
// "$ref": "#/responses/EmptyRepository"
|
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
|
ctx.JSON(http.StatusConflict, api.APIError{
|
|
Message: "Git Repository is empty.",
|
|
URL: setting.API.SwaggerURL,
|
|
})
|
|
return
|
|
}
|
|
|
|
gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
|
|
return
|
|
}
|
|
defer gitRepo.Close()
|
|
|
|
listOptions := utils.GetListOptions(ctx)
|
|
if listOptions.Page <= 0 {
|
|
listOptions.Page = 1
|
|
}
|
|
|
|
if listOptions.PageSize > setting.Git.CommitsRangeSize {
|
|
listOptions.PageSize = setting.Git.CommitsRangeSize
|
|
}
|
|
|
|
sha := ctx.Query("sha")
|
|
|
|
var baseCommit *git.Commit
|
|
if len(sha) == 0 {
|
|
// no sha supplied - use default branch
|
|
head, err := gitRepo.GetHEADBranch()
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetHEADBranch", err)
|
|
return
|
|
}
|
|
|
|
baseCommit, err = gitRepo.GetBranchCommit(head.Name)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
|
|
return
|
|
}
|
|
} else {
|
|
// get commit specified by sha
|
|
baseCommit, err = gitRepo.GetCommit(sha)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
// Total commit count
|
|
commitsCountTotal, err := baseCommit.CommitsCount()
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetCommitsCount", err)
|
|
return
|
|
}
|
|
|
|
pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(listOptions.PageSize)))
|
|
|
|
// Query commits
|
|
commits, err := baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "CommitsByRange", err)
|
|
return
|
|
}
|
|
|
|
userCache := make(map[string]*models.User)
|
|
|
|
apiCommits := make([]*api.Commit, commits.Len())
|
|
apiCommitsList := []api.Commit{}
|
|
i := 0
|
|
for commitPointer := commits.Front(); commitPointer != nil; commitPointer = commitPointer.Next() {
|
|
commit := commitPointer.Value.(*git.Commit)
|
|
|
|
// Create json struct
|
|
apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "toCommit", err)
|
|
return
|
|
}
|
|
apiCommitsList = append(apiCommitsList, *apiCommits[i])
|
|
i++
|
|
}
|
|
|
|
// kept for backwards compatibility
|
|
ctx.Header().Set("X-Page", strconv.Itoa(listOptions.Page))
|
|
ctx.Header().Set("X-PerPage", strconv.Itoa(listOptions.PageSize))
|
|
ctx.Header().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10))
|
|
ctx.Header().Set("X-PageCount", strconv.Itoa(pageCount))
|
|
ctx.Header().Set("X-HasMore", strconv.FormatBool(listOptions.Page < pageCount))
|
|
|
|
ctx.SetLinkHeader(int(commitsCountTotal), listOptions.PageSize)
|
|
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", commitsCountTotal))
|
|
ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, X-PerPage, X-Total, X-PageCount, X-HasMore, Link")
|
|
|
|
ctx.JSON(http.StatusOK, CommitSplitSlice(apiCommitsList))
|
|
}
|
|
|
|
func CommitSplitSlice(CommitsList []api.Commit) []api.CommitsSlice {
|
|
// sort by time
|
|
sort.Sort(api.SortCommit(CommitsList))
|
|
Commits := make([]api.CommitsSlice, 0)
|
|
i := 0
|
|
var j int
|
|
for {
|
|
if i >= len(CommitsList) {
|
|
break
|
|
}
|
|
// Detect equal CommitData,
|
|
for j = i + 1; j < len(CommitsList) && CommitsList[i].CommitDate == CommitsList[j].CommitDate; j++ {
|
|
}
|
|
// if equal, put commitdata in an array
|
|
commitDate := CommitsList[i].CommitDate
|
|
commitDatalist := CommitsList[i:j]
|
|
i = j // variable value
|
|
// get all the values,,,Commits
|
|
Commits = append(Commits, api.CommitsSlice{
|
|
CommitDate: commitDate,
|
|
Commits: commitDatalist,
|
|
})
|
|
}
|
|
return Commits
|
|
}
|
|
|
|
// 获取某一个分支或标签的 commits 数量
|
|
func GetCommitsCount(ctx *context.APIContext) {
|
|
var err error
|
|
// ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
|
|
// if err != nil {
|
|
// ctx.ServerError("GetBranchCommit", err)
|
|
// return
|
|
// }
|
|
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
|
|
if err != nil {
|
|
ctx.ServerError("GetCommitsCount", err)
|
|
return
|
|
}
|
|
ctx.JSON(http.StatusOK, &ctx.Repo)
|
|
}
|
|
|
|
// GetFileAllCommits get all commits by path on a repository
|
|
func GetFileAllCommits(ctx *context.APIContext) {
|
|
// swagger:operation GET /repos/{owner}/{repo}/file_commits/{filepath} repository repoGetFileAllCommits
|
|
// ---
|
|
// summary: Get a list of all commits by filepath from a repository ***
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: owner
|
|
// in: path
|
|
// description: owner of the repo
|
|
// type: string
|
|
// required: true
|
|
// - name: repo
|
|
// in: path
|
|
// description: name of the repo
|
|
// type: string
|
|
// required: true
|
|
// - name: filepath
|
|
// in: path
|
|
// description: filepath of the file to get
|
|
// type: string
|
|
// required: true
|
|
// - name: sha
|
|
// in: query
|
|
// description: SHA or branch to start listing commits from (usually 'master')
|
|
// type: string
|
|
// - name: page
|
|
// in: query
|
|
// description: page number of results to return (1-based)
|
|
// type: integer
|
|
// - name: limit
|
|
// in: query
|
|
// description: page size of results
|
|
// type: integer
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/FileCommitList"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
// "409":
|
|
// "$ref": "#/responses/EmptyRepository"
|
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
|
ctx.JSON(http.StatusConflict, api.APIError{
|
|
Message: "Git Repository is empty",
|
|
URL: setting.API.SwaggerURL,
|
|
})
|
|
return
|
|
}
|
|
gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "Get the GitRepo", err)
|
|
return
|
|
}
|
|
defer gitRepo.Close()
|
|
|
|
listOptions := utils.GetListOptions(ctx)
|
|
if listOptions.Page <= 0 {
|
|
listOptions.Page = 1
|
|
}
|
|
|
|
if listOptions.PageSize > setting.Git.CommitsRangeSize {
|
|
listOptions.PageSize = setting.Git.CommitsRangeSize
|
|
}
|
|
|
|
sha := ctx.Query("sha")
|
|
treePath := ctx.Params(":*")
|
|
|
|
var baseCommit *git.Commit
|
|
var commitsCountTotal int64
|
|
if len(sha) == 0 {
|
|
head, err := gitRepo.GetHEADBranch()
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetHeadBranch", err)
|
|
return
|
|
|
|
}
|
|
baseCommit, err = gitRepo.GetBranchCommit(head.Name)
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "Getcommit ", err)
|
|
return
|
|
}
|
|
|
|
commitsCountTotal, err = git.CommitsCountFiles(gitRepo.Path, []string{head.Name}, []string{treePath}) // get th commmit count by file
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "CommmisCountFiles", err)
|
|
return
|
|
}
|
|
} else {
|
|
baseCommit, err = gitRepo.GetCommit(sha)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
|
|
return
|
|
}
|
|
commitsCountTotal, err = git.CommitsCountFiles(gitRepo.Path, []string{sha}, []string{treePath})
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "CommitsCountFiles", err)
|
|
return
|
|
}
|
|
|
|
}
|
|
pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(listOptions.PageSize)))
|
|
commits, err := baseCommit.CommitsByFileAndRange(treePath, listOptions.Page, listOptions.PageSize)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "CommitByFileAndRange", err)
|
|
return
|
|
}
|
|
userCache := make(map[string]*models.User)
|
|
apiCommits := make([]*api.Commit, commits.Len())
|
|
|
|
i := 0
|
|
|
|
for commitPointer := commits.Front(); commitPointer != nil; commitPointer = commitPointer.Next() {
|
|
commit := commitPointer.Value.(*git.Commit)
|
|
|
|
// Create json struct
|
|
apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "toCommit", err)
|
|
return
|
|
}
|
|
|
|
i++
|
|
}
|
|
// kept for backwards compatibility
|
|
ctx.Header().Set("X-Page", strconv.Itoa(listOptions.Page))
|
|
ctx.Header().Set("X-PerPage", strconv.Itoa(listOptions.PageSize))
|
|
ctx.Header().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10))
|
|
ctx.Header().Set("X-PageCount", strconv.Itoa(pageCount))
|
|
ctx.Header().Set("X-HasMore", strconv.FormatBool(listOptions.Page < pageCount))
|
|
|
|
ctx.SetLinkHeader(int(commitsCountTotal), listOptions.PageSize)
|
|
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", commitsCountTotal))
|
|
|
|
ctx.JSON(http.StatusOK, apiCommits)
|
|
}
|
|
|
|
// 获取 commit diff
|
|
// Diff get diffs by commit on a repository
|
|
func Diff(ctx *context.APIContext) {
|
|
// swagger:operation GET /repos/{owner}/{repo}/commits/{sha}/diff repository repoGetDiffs***
|
|
// ---
|
|
// summary: Get diffs by commit from a repository
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: owner
|
|
// in: path
|
|
// description: owner of the repo
|
|
// type: string
|
|
// required: true
|
|
// - name: repo
|
|
// in: path
|
|
// description: name of the repo
|
|
// type: string
|
|
// required: true
|
|
// - name: sha
|
|
// in: path
|
|
// description: name of the repo
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// 200:
|
|
// description: success
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
commitID := ctx.Params(":sha")
|
|
|
|
gitRepo := ctx.Repo.GitRepo
|
|
|
|
log.Info("gitRepo = ", gitRepo)
|
|
|
|
commit, err := gitRepo.GetCommit(commitID)
|
|
log.Info("commit=\n", commit)
|
|
|
|
if err != nil {
|
|
if git.IsErrNotExist(err) {
|
|
ctx.NotFound("Repo.GitRepo.GetCommit", err)
|
|
} else {
|
|
ctx.Error(http.StatusInternalServerError, "Repo.GitRepo.GetCommit", err)
|
|
}
|
|
return
|
|
}
|
|
if len(commitID) != 40 {
|
|
commitID = commit.ID.String()
|
|
}
|
|
diff, err := gitdiff.GetDiffCommit(gitRepo,
|
|
commitID, setting.Git.MaxGitDiffLines,
|
|
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
|
|
if err != nil {
|
|
ctx.NotFound("GetDiffCommitWithWhitespaceBehavior", err)
|
|
return
|
|
}
|
|
if err != nil {
|
|
ctx.NotFound("GetDiffCommit", err)
|
|
return
|
|
}
|
|
ctx.JSON(http.StatusOK, &diff)
|
|
}
|