fix merge from hh_file_commits

This commit is contained in:
yystopf 2021-10-08 14:26:21 +08:00
commit de0f7764c7
8 changed files with 274 additions and 6 deletions

View File

@ -293,6 +293,11 @@ func CommitsCount(repoPath, revision string) (int64, error) {
return commitsCount(repoPath, []string{revision}, []string{})
}
// CommitsCountByFile returns number of total commits of unitl given revision and file
func CommitsCountByFile(repoPath, revision, file string) (int64, error) {
return commitsCount(repoPath, []string{revision}, []string{file})
}
// CommitsCount returns number of total commits of until current revision.
func (c *Commit) CommitsCount() (int64, error) {
return CommitsCount(c.repo.Path, c.ID.String())
@ -303,6 +308,11 @@ func (c *Commit) CommitsByRange(page, pageSize int) (*list.List, error) {
return c.repo.commitsByRange(c.ID, page, pageSize)
}
// CommitsByFileAndRange returns the specific page page commits before current revision and file, every page's number default by CommitsRangeSize
func (c *Commit) CommitsByFileAndRange(file string, page, pageSize int) (*list.List, error) {
return c.repo.CommitsByFileAndRange(c.ID.String(), file, page, pageSize)
}
// CommitsBefore returns all the commits before current revision
func (c *Commit) CommitsBefore() (*list.List, error) {
return c.repo.getCommitsBefore(c.ID)

View File

@ -335,8 +335,8 @@ func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
}
// CommitsByFileAndRange return the commits according revison file and the page
func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (*list.List, error) {
stdout, err := NewCommand("log", revision, "--follow", "--skip="+strconv.Itoa((page-1)*50),
func (repo *Repository) CommitsByFileAndRange(revision, file string, page, pageSize int) (*list.List, error) {
stdout, err := NewCommand("log", revision, "--follow", "--skip="+strconv.Itoa((page-1)*pageSize),
"--max-count="+strconv.Itoa(CommitsRangeSize), prettyLogFormat, "--", file).RunInDirBytes(repo.Path)
if err != nil {
return nil, err
@ -345,8 +345,8 @@ func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (
}
// CommitsByFileAndRangeNoFollow return the commits according revison file and the page
func (repo *Repository) CommitsByFileAndRangeNoFollow(revision, file string, page int) (*list.List, error) {
stdout, err := NewCommand("log", revision, "--skip="+strconv.Itoa((page-1)*50),
func (repo *Repository) CommitsByFileAndRangeNoFollow(revision, file string, page, pageSize int) (*list.List, error) {
stdout, err := NewCommand("log", revision, "--skip="+strconv.Itoa((page-1)*pageSize),
"--max-count="+strconv.Itoa(CommitsRangeSize), prettyLogFormat, "--", file).RunInDirBytes(repo.Path)
if err != nil {
return nil, err

View File

@ -956,6 +956,9 @@ func RegisterRoutes(m *macaron.Macaron) {
})
}, reqRepoReader(models.UnitTypeCode))
m.Get("/commits_slice", repo.GetAllCommitsSliceByTime)
m.Group("/file_commits", func() {
m.Get("/*", repo.GetFileAllCommits)
})
m.Group("/git", func() {
m.Group("/commits", func() {
m.Get("/:sha", repo.GetSingleCommit)

View File

@ -84,6 +84,140 @@ func getCommit(ctx *context.APIContext, identifier string) {
ctx.JSON(http.StatusOK, json)
}
// GetFileCommits 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.ServerError("OpenRepository", err)
return
}
defer gitRepo.Close()
listOptions := utils.GetListOptions(ctx)
if listOptions.Page <= 0 {
listOptions.Page = 1
}
if listOptions.PageSize > git.CommitsRangeSize {
listOptions.PageSize = 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.ServerError("GetHEADBranch", err)
return
}
baseCommit, err = gitRepo.GetBranchCommit(head.Name)
if err != nil {
ctx.ServerError("GetCommit", err)
return
}
commitsCountTotal, err = git.CommitsCountByFile(gitRepo.Path, head.Name, treePath)
if err != nil {
ctx.ServerError("CommitsCount", err)
return
}
} else {
baseCommit, err = gitRepo.GetCommit(sha)
if err != nil {
ctx.ServerError("GetCommit", err)
return
}
commitsCountTotal, err = git.CommitsCountByFile(gitRepo.Path, sha, treePath)
if err != nil {
ctx.ServerError("CommitsCount", err)
return
}
}
pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(listOptions.PageSize)))
commits, err := baseCommit.CommitsByFileAndRange(treePath, listOptions.Page, listOptions.PageSize)
if err != nil {
ctx.ServerError("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)
apiCommits[i], err = toCommit(ctx, ctx.Repo.Repository, commit, userCache)
if err != nil {
ctx.ServerError("toCommit", err)
return
}
i++
}
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)
}
// GetAllCommits get all commits via
func GetAllCommits(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/commits repository repoGetAllCommits

View File

@ -290,6 +290,28 @@ type swaggerCommitList struct {
Body []api.Commit `json:"body"`
}
// FileCommitList
// swagger:response FileCommitList
type swaggerFileCommitList struct {
// The current page
Page int `json:"X-Page"`
// Commits per page
PerPage int `json:"X-PerPage"`
// Total commit count
Total int `json:"X-Total"`
// Total number of pages
PageCount int `json:"X-PageCount"`
// True if there is another page
HasMore bool `json:"X-HasMore"`
// in: body
Body []api.Commit `json:"body"`
}
// EmptyRepository
// swagger:response EmptyRepository
type swaggerEmptyRepository struct {

View File

@ -179,7 +179,7 @@ func FileHistory(ctx *context.Context) {
page = 1
}
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(branchName, fileName, page)
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(branchName, fileName, page, 50)
if err != nil {
ctx.ServerError("CommitsByFileAndRange", err)
return

View File

@ -277,7 +277,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
}
// get Commit Count
commitsHistory, err := wikiRepo.CommitsByFileAndRangeNoFollow("master", pageFilename, page)
commitsHistory, err := wikiRepo.CommitsByFileAndRangeNoFollow("master", pageFilename, page, 50)
if err != nil {
if wikiRepo != nil {
wikiRepo.Close()

View File

@ -3286,6 +3286,70 @@
}
}
},
"/repos/{owner}/{repo}/file_commits/{filepath}": {
"get": {
"produces": [
"application/json"
],
"tags": [
"repository"
],
"summary": "Get a list of all commits by filepath from a repository",
"operationId": "repoGetFileAllCommits",
"parameters": [
{
"type": "string",
"description": "owner of the repo",
"name": "owner",
"in": "path",
"required": true
},
{
"type": "string",
"description": "name of the repo",
"name": "repo",
"in": "path",
"required": true
},
{
"type": "string",
"description": "filepath of the file to get",
"name": "filepath",
"in": "path",
"required": true
},
{
"type": "string",
"description": "SHA or branch to start listing commits from (usually 'master')",
"name": "sha",
"in": "query"
},
{
"type": "integer",
"description": "page number of results to return (1-based)",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "page size of results",
"name": "limit",
"in": "query"
}
],
"responses": {
"200": {
"$ref": "#/responses/FileCommitList"
},
"404": {
"$ref": "#/responses/notFound"
},
"409": {
"$ref": "#/responses/EmptyRepository"
}
}
}
},
"/repos/{owner}/{repo}/find": {
"get": {
"produces": [
@ -15987,6 +16051,41 @@
"$ref": "#/definitions/APIError"
}
},
"FileCommitList": {
"description": "FileCommitList",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Commit"
}
},
"headers": {
"X-HasMore": {
"type": "boolean",
"description": "True if there is another page"
},
"X-Page": {
"type": "integer",
"format": "int64",
"description": "The current page"
},
"X-PageCount": {
"type": "integer",
"format": "int64",
"description": "Total number of pages"
},
"X-PerPage": {
"type": "integer",
"format": "int64",
"description": "Commits per page"
},
"X-Total": {
"type": "integer",
"format": "int64",
"description": "Total commit count"
}
}
},
"FileDeleteResponse": {
"description": "FileDeleteResponse",
"schema": {