Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite compare functions #32786

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8eb19a5
Refactor GetDiff/Path functions to let it more flexible
lunny Dec 9, 2024
48b5bab
fix conflict
lunny Dec 10, 2024
283c030
Fix bug
lunny Dec 10, 2024
f19b4b7
Refactor Parsing compare path parameters
lunny Dec 11, 2024
4ee5334
Refactor all compare routers
lunny Dec 11, 2024
07abd8d
Merge branch 'main' into lunny/refactor_getpatch
lunny Dec 11, 2024
743fa68
make the code simpler
lunny Dec 11, 2024
72f6e28
Merge branch 'main' into lunny/refactor_getpatch
lunny Dec 11, 2024
52639ff
Fix bug
lunny Dec 11, 2024
a3eb356
Add rootRepo and ownForkRepo
lunny Dec 12, 2024
a2804a5
Merge branch 'main' into lunny/refactor_getpatch
lunny Dec 12, 2024
743cebe
Some improvements
lunny Dec 12, 2024
050fad9
revert change to fork
lunny Dec 12, 2024
95bccdb
remove unnecessary parameter
lunny Dec 12, 2024
91147db
Fix test
lunny Dec 12, 2024
27b6753
Merge branch 'main' into lunny/refactor_getpatch
lunny Dec 12, 2024
92e68ae
Revert changes related to getpatch/getdiff because they are extracted…
lunny Dec 12, 2024
b7f2be5
Fix possible bug
lunny Dec 20, 2024
6b3bc6e
Merge branch 'main' into lunny/refactor_getpatch
lunny Dec 20, 2024
831fb56
Add more test
lunny Dec 23, 2024
2461ad0
Merge branch 'main' into lunny/refactor_getpatch
lunny Dec 24, 2024
4579e49
Fix lint
lunny Dec 25, 2024
d08942e
Fix lint
lunny Dec 25, 2024
3328679
Fix test
lunny Jan 3, 2025
d9bc413
Merge branch 'main' into lunny/refactor_getpatch
lunny Jan 3, 2025
361ca40
Fix fixture
lunny Jan 3, 2025
2b830ee
Fix tests
lunny Jan 3, 2025
453adc2
Fix lint
lunny Jan 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions models/fixtures/branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,62 @@
is_deleted: false
deleted_by_id: 0
deleted_unix: 0

-
id: 16
repo_id: 1
name: 'DefaultBranch'
commit_id: '90c1019714259b24fb81711d4416ac0f18667dfa'
commit_message: 'add license'
commit_time: 1709259547
pusher_id: 1
is_deleted: false

-
id: 17
repo_id: 1
name: 'develop'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'first commit'
commit_time: 978307100
pusher_id: 1

-
id: 18
repo_id: 11
name: 'develop'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489956479
pusher_id: 1

-
id: 19
repo_id: 10
name: 'DefaultBranch'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489956479
pusher_id: 1
is_deleted: false

-
id: 20
repo_id: 1
name: 'pr-to-update'
commit_id: '62fb502a7172d4453f0322a2cc85bddffa57f07a'
commit_message: 'add WoW File'
commit_time: 1579200695
pusher_id: 1

-
id: 21
repo_id: 10
name: 'develop'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489927679
pusher_id: 12
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
71 changes: 56 additions & 15 deletions routers/api/v1/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
package repo

import (
"errors"
"net/http"
"strings"

access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/common"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/convert"
)
Expand Down Expand Up @@ -52,30 +58,65 @@ func CompareDiff(ctx *context.APIContext) {
}
}

infoPath := ctx.PathParam("*")
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}
pathParam := ctx.PathParam("*")
baseRepo := ctx.Repo.Repository
ci, err := common.ParseComparePathParams(ctx, pathParam, baseRepo, ctx.Repo.GitRepo)
if err != nil {
switch {
case user_model.IsErrUserNotExist(err):
ctx.NotFound("GetUserByName")
case repo_model.IsErrRepoNotExist(err):
ctx.NotFound("GetRepositoryByOwnerAndName")
case errors.Is(err, util.ErrInvalidArgument):
ctx.NotFound("ParseComparePathParams")
default:
ctx.ServerError("GetRepositoryByOwnerAndName", err)
}
return
}
defer ci.Close()

// remove the check when we support compare with carets
if ci.CaretTimes > 0 {
ctx.NotFound("Unsupported compare")
return
}

if !ci.IsSameRepo() {
// user should have permission to read headrepo's codes
permHead, err := access_model.GetUserRepoPermission(ctx, ci.HeadRepo, ctx.Doer)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err)
return
}
if !permHead.CanRead(unit.TypeCode) {
if log.IsTrace() {
log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in headRepo has Permissions: %-+v",
ctx.Doer,
ci.HeadRepo,
permHead)
}
ctx.NotFound("Can't read headRepo UnitTypeCode")
return
}
}

compareResult, closer := parseCompareInfo(ctx, api.CreatePullRequestOption{Base: infos[0], Head: infos[1]})
if ctx.Written() {
ctx.Repo.PullRequest.SameRepo = ci.IsSameRepo()
log.Trace("Repo path: %q, base branch: %q, head branch: %q", ctx.Repo.GitRepo.Path, ci.BaseOriRef, ci.HeadOriRef)

ci.CompareInfo, err = ci.HeadGitRepo.GetCompareInfo(repo_model.RepoPath(baseRepo.Owner.Name, baseRepo.Name), ci.BaseOriRef, ci.HeadOriRef, false, false)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCompareInfo", err)
return
}
defer closer()

verification := ctx.FormString("verification") == "" || ctx.FormBool("verification")
files := ctx.FormString("files") == "" || ctx.FormBool("files")

apiCommits := make([]*api.Commit, 0, len(compareResult.compareInfo.Commits))
apiCommits := make([]*api.Commit, 0, len(ci.CompareInfo.Commits))
userCache := make(map[string]*user_model.User)
for i := 0; i < len(compareResult.compareInfo.Commits); i++ {
apiCommit, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, compareResult.compareInfo.Commits[i], userCache,
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: verification,
Expand All @@ -89,7 +130,7 @@ func CompareDiff(ctx *context.APIContext) {
}

ctx.JSON(http.StatusOK, &api.Compare{
TotalCommits: len(compareResult.compareInfo.Commits),
TotalCommits: len(ci.CompareInfo.Commits),
Commits: apiCommits,
})
}
Loading
Loading