Skip to content

Commit

Permalink
feat: implement branch/commit comparison API
Browse files Browse the repository at this point in the history
- Add a new API endpoint for comparing differences between branches or commits
- Implement a new function `CompareDiff` in the `repo` package to handle the comparison logic
- Create a new file `compare.go` in the `common` package to parse and process comparison information
- Define a new `CompareInfo` struct to hold comparison details
- Add logic to determine the base and head repositories for comparison, including handling of forks and permission checks
- Implement comparison logic to fetch and compare information between branches or commits, including support for file-only comparisons

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Apr 9, 2024
1 parent bbe5cd7 commit 7544034
Show file tree
Hide file tree
Showing 3 changed files with 397 additions and 0 deletions.
2 changes: 2 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,8 @@ func Routes() *web.Route {
m.Post("/migrate", reqToken(), bind(api.MigrateRepoOptions{}), repo.Migrate)

m.Group("/{username}/{reponame}", func() {
m.Get("/compare/*", repo.CompareDiff)

m.Combo("").Get(reqAnyRepoReader(), repo.Get).
Delete(reqToken(), reqOwner(), repo.Delete).
Patch(reqToken(), reqAdmin(), bind(api.EditRepoOption{}), repo.Edit)
Expand Down
83 changes: 83 additions & 0 deletions routers/api/v1/repo/compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package repo

import (
"net/http"

user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/gitrepo"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/common"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/convert"
)

// CompareDiff compare two branches or commits
func CompareDiff(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/compare/{diff} Get compared branches information
// ---
// summary: Get compared branches information
// 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: diff
// in: path
// description: compare two branches or commits
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/Repository"
// "404":
// "$ref": "#/responses/notFound"

if ctx.Repo.GitRepo == nil {
gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
if err != nil {
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
return
}
ctx.Repo.GitRepo = gitRepo
defer gitRepo.Close()
}

ci := common.ParseCompareInfo(ctx)
defer func() {
if ci != nil && ci.HeadGitRepo != nil {
ci.HeadGitRepo.Close()
}
}()
if ctx.Written() {
return
}

apiCommits := make([]*api.Commit, 0, len(ci.CompareInfo.Commits))
userCache := make(map[string]*user_model.User)
for i := 0; i < len(ci.CompareInfo.Commits); i++ {
apiCommit, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, ci.CompareInfo.Commits[i], userCache,
convert.ToCommitOptions{
Stat: true,
Verification: ctx.FormBool("verification"),
Files: ctx.FormBool("files"),
})
if err != nil {
ctx.ServerError("toCommit", err)
return
}
apiCommits = append(apiCommits, apiCommit)
}

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

0 comments on commit 7544034

Please sign in to comment.