Skip to content

Commit

Permalink
Use GitHub-compatible endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerty287 committed Dec 13, 2021
1 parent f91aba5 commit 4ebd8c0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 112 deletions.
2 changes: 1 addition & 1 deletion integrations/api_repo_git_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestGetFileHistory(t *testing.T) {
session := loginUser(t, user.Name)
token := getTokenForLoggedInUser(t, session)

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

var apiData []api.Commit
Expand Down
1 change: 0 additions & 1 deletion routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,6 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
m.Get("/blobs/{sha}", context.RepoRefForAPI, repo.GetBlob)
m.Get("/tags/{sha}", context.RepoRefForAPI, repo.GetAnnotatedTag)
m.Get("/notes/{sha}", repo.GetNote)
m.Get("/history/*", context.RepoRefForAPI, reqRepoReader(unit.TypeCode), repo.GetFileHistory)
}, reqRepoReader(unit.TypeCode))
m.Group("/contents", func() {
m.Get("", repo.GetContentsList)
Expand Down
166 changes: 56 additions & 110 deletions routers/api/v1/repo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,17 @@ func GetAllCommits(ctx *context.APIContext) {
// in: query
// description: SHA or branch to start listing commits from (usually 'master')
// type: string
// - name: path
// in: query
// description: filepath of a file/dir
// 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
// description: page size of results (ignored if used with 'path')
// type: integer
// responses:
// "200":
Expand Down Expand Up @@ -149,46 +153,73 @@ func GetAllCommits(ctx *context.APIContext) {
}

sha := ctx.FormString("sha")
path := ctx.FormString("path")

var (
commitsCountTotal int64
commits []*git.Commit
)

if len(path) == 0 {
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
}
}

var baseCommit *git.Commit
if len(sha) == 0 {
// no sha supplied - use default branch
head, err := gitRepo.GetHEADBranch()
// Total commit count
commitsCountTotal, err = baseCommit.CommitsCount()
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetHEADBranch", err)
ctx.Error(http.StatusInternalServerError, "GetCommitsCount", err)
return
}

baseCommit, err = gitRepo.GetBranchCommit(head.Name)
// Query commits
commits, err = baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
ctx.Error(http.StatusInternalServerError, "CommitsByRange", err)
return
}
} else {
// get commit specified by sha
baseCommit, err = gitRepo.GetCommit(sha)
if len(sha) == 0 {
sha = ctx.Repo.Repository.DefaultBranch
}

commitsCountTotal, err = gitRepo.FileCommitsCount(sha, path)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
ctx.Error(http.StatusInternalServerError, "FileCommitsCount", err)
return
} else if commitsCountTotal == 0 {
ctx.NotFound("FileCommitsCount", nil)
return
}

commits, err = gitRepo.CommitsByFileAndRange(sha, path, listOptions.PageSize)
if err != nil {
ctx.Error(http.StatusInternalServerError, "CommitsByFileAndRange", 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]*user_model.User)

apiCommits := make([]*api.Commit, len(commits))
Expand Down Expand Up @@ -264,88 +295,3 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) {
return
}
}

// GetFileHistory get a file's commit history
func GetFileHistory(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/git/history/{filepath} repository repoGetFileHistory
// ---
// summary: Get the commit history of a file or directory
// 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/directory
// type: string
// required: true
// - name: ref
// in: query
// description: "The name of the ref (branch/tag). Default the repository’s default branch"
// type: string
// required: false
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// responses:
// "200":
// "$ref": "#/responses/CommitList"
// "404":
// "$ref": "#/responses/notFound"

if ctx.Repo.Repository.IsEmpty {
ctx.NotFound()
return
}

ref := ctx.FormTrim("ref")
if len(ref) < 1 {
ref = ctx.Repo.Repository.DefaultBranch
}

page := ctx.FormInt("page")
if page <= 1 {
page = 1
}

commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ref, ctx.Repo.TreePath)
if err != nil {
ctx.Error(http.StatusInternalServerError, "FileCommitsCount", err)
return
} else if commitsCount == 0 {
ctx.NotFound("FileCommitsCount", nil)
return
}

commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(ref, ctx.Repo.TreePath, page)
if err != nil {
ctx.Error(http.StatusInternalServerError, "CommitsByFileAndRange", err)
return
}

userCache := make(map[string]*user_model.User)
apiCommits := make([]*api.Commit, len(commits))
for i, commit := range commits {
// Create json struct
apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache)
if err != nil {
ctx.Error(http.StatusInternalServerError, "toCommit", err)
return
}
}

ctx.SetLinkHeader(int(commitsCount), setting.Git.CommitsRangeSize)
ctx.SetTotalCountHeader(commitsCount)

ctx.JSON(http.StatusOK, &apiCommits)
}

0 comments on commit 4ebd8c0

Please sign in to comment.