mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-07-31 07:29:40 +02:00
- fix: API must use headGitRepo instead of ctx.Repo.GitRepo for comparing - fix: make API /repos/{owner}/{repo}/compare/{basehead} work with forks - add test coverage for both fixes and the underlying function `parseCompareInfo` - refactor and improve part of the helpers from `tests/integration/api_helper_for_declarative_test.go` - remove a few wrong or misleading comments Refs forgejo/forgejo#7978 ## Note on the focus of the PR It was initially created to address a regression introduced in v12. But the tests that verify it is fixed discovered a v11.0 bug. They cannot conveniently be separated because they both relate to the same area of code that was previously not covered by any test. ## Note on v11.0 backport It must be manually done by cherry-picking all commits up to and not including `fix: API must use headGitRepo instead of ctx.Repo.GitRepo for comparing` because it is v12 specific. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. ### Documentation - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] I do not want this change to show in the release notes. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8326 Reviewed-by: Otto <otto@codeberg.org> Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
102 lines
2.7 KiB
Go
102 lines
2.7 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repo
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
user_model "forgejo.org/models/user"
|
|
"forgejo.org/modules/gitrepo"
|
|
api "forgejo.org/modules/structs"
|
|
"forgejo.org/services/context"
|
|
"forgejo.org/services/convert"
|
|
)
|
|
|
|
// CompareDiff compare two branches or commits
|
|
func CompareDiff(ctx *context.APIContext) {
|
|
// swagger:operation GET /repos/{owner}/{repo}/compare/{basehead} repository repoCompareDiff
|
|
// ---
|
|
// summary: Get commit comparison 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: basehead
|
|
// in: path
|
|
// description: compare two branches or commits
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/Compare"
|
|
// "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()
|
|
}
|
|
|
|
infoPath := ctx.Params("*")
|
|
infos := []string{ctx.Repo.Repository.DefaultBranch, ctx.Repo.Repository.DefaultBranch}
|
|
if infoPath != "" {
|
|
infos = strings.SplitN(infoPath, "...", 2)
|
|
if len(infos) != 2 {
|
|
if infos = strings.SplitN(infoPath, "..", 2); len(infos) != 2 {
|
|
infos = []string{ctx.Repo.Repository.DefaultBranch, infoPath}
|
|
}
|
|
}
|
|
}
|
|
|
|
headRepository, headGitRepo, ci, _, _ := parseCompareInfo(ctx, api.CreatePullRequestOption{
|
|
Base: infos[0],
|
|
Head: infos[1],
|
|
})
|
|
if ctx.Written() {
|
|
return
|
|
}
|
|
defer headGitRepo.Close()
|
|
|
|
verification := ctx.FormString("verification") == "" || ctx.FormBool("verification")
|
|
files := ctx.FormString("files") == "" || ctx.FormBool("files")
|
|
|
|
apiCommits := make([]*api.Commit, 0, len(ci.Commits))
|
|
apiFiles := []*api.CommitAffectedFiles{}
|
|
userCache := make(map[string]*user_model.User)
|
|
for i := 0; i < len(ci.Commits); i++ {
|
|
apiCommit, err := convert.ToCommit(ctx, headRepository, headGitRepo, ci.Commits[i], userCache,
|
|
convert.ToCommitOptions{
|
|
Stat: true,
|
|
Verification: verification,
|
|
Files: files,
|
|
})
|
|
if err != nil {
|
|
ctx.ServerError("toCommit", err)
|
|
return
|
|
}
|
|
apiCommits = append(apiCommits, apiCommit)
|
|
apiFiles = append(apiFiles, apiCommit.Files...)
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, &api.Compare{
|
|
TotalCommits: len(ci.Commits),
|
|
Commits: apiCommits,
|
|
Files: apiFiles,
|
|
})
|
|
}
|