|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package repo |
| 5 | + |
| 6 | +import ( |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + user_model "code.gitea.io/gitea/models/user" |
| 11 | + "code.gitea.io/gitea/modules/gitrepo" |
| 12 | + api "code.gitea.io/gitea/modules/structs" |
| 13 | + "code.gitea.io/gitea/services/context" |
| 14 | + "code.gitea.io/gitea/services/convert" |
| 15 | +) |
| 16 | + |
| 17 | +// CompareDiff compare two branches or commits |
| 18 | +func CompareDiff(ctx *context.APIContext) { |
| 19 | + // swagger:operation GET /repos/{owner}/{repo}/compare/{basehead} Get commit comparison information |
| 20 | + // --- |
| 21 | + // summary: Get commit comparison information |
| 22 | + // produces: |
| 23 | + // - application/json |
| 24 | + // parameters: |
| 25 | + // - name: owner |
| 26 | + // in: path |
| 27 | + // description: owner of the repo |
| 28 | + // type: string |
| 29 | + // required: true |
| 30 | + // - name: repo |
| 31 | + // in: path |
| 32 | + // description: name of the repo |
| 33 | + // type: string |
| 34 | + // required: true |
| 35 | + // - name: basehead |
| 36 | + // in: path |
| 37 | + // description: compare two branches or commits |
| 38 | + // type: string |
| 39 | + // required: true |
| 40 | + // responses: |
| 41 | + // "200": |
| 42 | + // "$ref": "#/responses/Compare" |
| 43 | + // "404": |
| 44 | + // "$ref": "#/responses/notFound" |
| 45 | + |
| 46 | + if ctx.Repo.GitRepo == nil { |
| 47 | + gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository) |
| 48 | + if err != nil { |
| 49 | + ctx.Error(http.StatusInternalServerError, "OpenRepository", err) |
| 50 | + return |
| 51 | + } |
| 52 | + ctx.Repo.GitRepo = gitRepo |
| 53 | + defer gitRepo.Close() |
| 54 | + } |
| 55 | + |
| 56 | + infoPath := ctx.Params("*") |
| 57 | + infos := []string{ctx.Repo.Repository.DefaultBranch, ctx.Repo.Repository.DefaultBranch} |
| 58 | + if infoPath != "" { |
| 59 | + infos = strings.SplitN(infoPath, "...", 2) |
| 60 | + if len(infos) != 2 { |
| 61 | + if infos = strings.SplitN(infoPath, "..", 2); len(infos) != 2 { |
| 62 | + infos = []string{ctx.Repo.Repository.DefaultBranch, infoPath} |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + _, _, headGitRepo, ci, _, _ := parseCompareInfo(ctx, api.CreatePullRequestOption{ |
| 68 | + Base: infos[0], |
| 69 | + Head: infos[1], |
| 70 | + }) |
| 71 | + if ctx.Written() { |
| 72 | + return |
| 73 | + } |
| 74 | + defer headGitRepo.Close() |
| 75 | + |
| 76 | + verification := ctx.FormString("verification") == "" || ctx.FormBool("verification") |
| 77 | + files := ctx.FormString("files") == "" || ctx.FormBool("files") |
| 78 | + |
| 79 | + apiCommits := make([]*api.Commit, 0, len(ci.Commits)) |
| 80 | + userCache := make(map[string]*user_model.User) |
| 81 | + for i := 0; i < len(ci.Commits); i++ { |
| 82 | + apiCommit, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, ci.Commits[i], userCache, |
| 83 | + convert.ToCommitOptions{ |
| 84 | + Stat: true, |
| 85 | + Verification: verification, |
| 86 | + Files: files, |
| 87 | + }) |
| 88 | + if err != nil { |
| 89 | + ctx.ServerError("toCommit", err) |
| 90 | + return |
| 91 | + } |
| 92 | + apiCommits = append(apiCommits, apiCommit) |
| 93 | + } |
| 94 | + |
| 95 | + ctx.JSON(http.StatusOK, &api.Compare{ |
| 96 | + TotalCommits: len(ci.Commits), |
| 97 | + Commits: apiCommits, |
| 98 | + }) |
| 99 | +} |
0 commit comments