-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement branch/commit comparison API
- 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
Showing
3 changed files
with
397 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.