Skip to content

Commit a3eb356

Browse files
committed
Add rootRepo and ownForkRepo
1 parent 52639ff commit a3eb356

File tree

5 files changed

+116
-48
lines changed

5 files changed

+116
-48
lines changed

routers/api/v1/repo/compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func CompareDiff(ctx *context.APIContext) {
6161

6262
pathParam := ctx.PathParam("*")
6363
baseRepo := ctx.Repo.Repository
64-
ci, err := common.ParseComparePathParams(ctx, pathParam, baseRepo, ctx.Repo.GitRepo)
64+
ci, err := common.ParseComparePathParams(ctx, pathParam, baseRepo, ctx.Repo.GitRepo, ctx.Doer)
6565
if err != nil {
6666
switch {
6767
case user_model.IsErrUserNotExist(err):

routers/api/v1/repo/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func CreatePullRequest(ctx *context.APIContext) {
403403
)
404404

405405
// Get repo/branch information
406-
ci, err := common.ParseComparePathParams(ctx, form.Base+"..."+form.Head, repo, ctx.Repo.GitRepo)
406+
ci, err := common.ParseComparePathParams(ctx, form.Base+"..."+form.Head, repo, ctx.Repo.GitRepo, ctx.Doer)
407407
if err != nil {
408408
switch {
409409
case user_model.IsErrUserNotExist(err):

routers/common/compare.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ type CompareInfo struct {
101101
HeadUser *user_model.User
102102
HeadRepo *repo_model.Repository
103103
HeadGitRepo *git.Repository
104+
RootRepo *repo_model.Repository
105+
OwnForkRepo *repo_model.Repository
104106
CompareInfo *git.CompareInfo
105107
close func()
106108
IsBaseCommit bool
@@ -190,6 +192,20 @@ func findHeadRepoFromRootBase(ctx context.Context, baseRepo *repo_model.Reposito
190192
return nil, nil
191193
}
192194

195+
func getRootRepo(ctx context.Context, repo *repo_model.Repository) (*repo_model.Repository, error) {
196+
curRepo := repo
197+
for curRepo.IsFork {
198+
if err := curRepo.GetBaseRepo(ctx); err != nil {
199+
return nil, err
200+
}
201+
if curRepo.BaseRepo == nil {
202+
break
203+
}
204+
curRepo = curRepo.BaseRepo
205+
}
206+
return curRepo, nil
207+
}
208+
193209
// ParseComparePathParams Get compare information
194210
// A full compare url is of the form:
195211
//
@@ -215,7 +231,7 @@ func findHeadRepoFromRootBase(ctx context.Context, baseRepo *repo_model.Reposito
215231
// format: <base branch>...[<head repo>:]<head branch>
216232
// base<-head: master...head:feature
217233
// same repo: master...feature
218-
func ParseComparePathParams(ctx context.Context, pathParam string, baseRepo *repo_model.Repository, baseGitRepo *git.Repository) (*CompareInfo, error) {
234+
func ParseComparePathParams(ctx context.Context, pathParam string, baseRepo *repo_model.Repository, baseGitRepo *git.Repository, doer *user_model.User) (*CompareInfo, error) {
219235
ci := &CompareInfo{}
220236
var err error
221237

@@ -273,13 +289,37 @@ func ParseComparePathParams(ctx context.Context, pathParam string, baseRepo *rep
273289
}
274290
}
275291

292+
// find root repo
293+
if !baseRepo.IsFork {
294+
ci.RootRepo = baseRepo
295+
} else {
296+
if !ci.HeadRepo.IsFork {
297+
ci.RootRepo = ci.HeadRepo
298+
} else {
299+
ci.RootRepo, err = getRootRepo(ctx, baseRepo)
300+
if err != nil {
301+
return nil, err
302+
}
303+
}
304+
}
305+
306+
// find ownfork repo
307+
if doer != nil && ci.HeadRepo.OwnerID != doer.ID && baseRepo.OwnerID != doer.ID {
308+
ci.OwnForkRepo, err = findHeadRepo(ctx, baseRepo, doer.ID)
309+
if err != nil {
310+
return nil, err
311+
}
312+
}
313+
276314
ci.BaseFullRef, ci.IsBaseCommit, err = detectFullRef(ctx, baseRepo.ID, baseGitRepo, ci.BaseOriRef)
277315
if err != nil {
316+
ci.Close()
278317
return nil, err
279318
}
280319

281320
ci.HeadFullRef, ci.IsHeadCommit, err = detectFullRef(ctx, ci.HeadRepo.ID, ci.HeadGitRepo, ci.HeadOriRef)
282321
if err != nil {
322+
ci.Close()
283323
return nil, err
284324
}
285325
return ci, nil

routers/web/repo/compare.go

Lines changed: 71 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"net/http"
1515
"net/url"
1616
"path/filepath"
17+
"slices"
1718
"strings"
1819

1920
"code.gitea.io/gitea/models/db"
@@ -193,7 +194,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
193194
pathParam := ctx.PathParam("*")
194195
baseRepo := ctx.Repo.Repository
195196

196-
ci, err := common.ParseComparePathParams(ctx, pathParam, baseRepo, ctx.Repo.GitRepo)
197+
ci, err := common.ParseComparePathParams(ctx, pathParam, baseRepo, ctx.Repo.GitRepo, ctx.Doer)
197198
if err != nil {
198199
switch {
199200
case user_model.IsErrUserNotExist(err):
@@ -207,7 +208,6 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
207208
}
208209
return nil
209210
}
210-
defer ci.Close()
211211

212212
// remove the check when we support compare with carets
213213
if ci.CaretTimes > 0 {
@@ -245,8 +245,6 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
245245
ctx.Data["CanWriteToHeadRepo"] = permHead.CanWrite(unit.TypeCode)
246246
}
247247

248-
// TODO: prepareRepos, branches and tags for dropdowns
249-
250248
ctx.Data["PageIsComparePull"] = ci.IsPull() && ctx.Repo.CanReadIssuesOrPulls(true)
251249
ctx.Data["BaseName"] = baseRepo.OwnerName
252250
ctx.Data["BaseBranch"] = ci.BaseOriRef
@@ -258,7 +256,6 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
258256
ctx.Data["BaseIsBranch"] = ci.BaseFullRef.IsBranch()
259257
ctx.Data["BaseIsTag"] = ci.BaseFullRef.IsTag()
260258
ctx.Data["IsPull"] = true
261-
// ctx.Data["OwnForkRepo"] = ownForkRepo FIXME: This is not used
262259

263260
ctx.Data["HeadRepo"] = ci.HeadRepo
264261
ctx.Data["BaseCompareRepo"] = ctx.Repo.Repository
@@ -399,34 +396,91 @@ func PrepareCompareDiff(
399396
return false
400397
}
401398

402-
func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *repo_model.Repository) (branches, tags []string, err error) {
403-
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
404-
if err != nil {
405-
return nil, nil, err
406-
}
407-
defer gitRepo.Close()
408-
409-
branches, err = git_model.FindBranchNames(ctx, git_model.FindBranchOptions{
399+
func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *repo_model.Repository) ([]string, []string, error) {
400+
branches, err := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{
410401
RepoID: repo.ID,
411402
ListOptions: db.ListOptionsAll,
412403
IsDeletedBranch: optional.Some(false),
413404
})
414405
if err != nil {
415406
return nil, nil, err
416407
}
417-
tags, err = gitRepo.GetTags(0, 0)
408+
// always put default branch on the top if it exists
409+
if slices.Contains(branches, repo.DefaultBranch) {
410+
branches = util.SliceRemoveAll(branches, repo.DefaultBranch)
411+
branches = append([]string{repo.DefaultBranch}, branches...)
412+
}
413+
414+
tags, err := repo_model.GetTagNamesByRepoID(ctx, repo.ID)
418415
if err != nil {
419416
return nil, nil, err
420417
}
418+
421419
return branches, tags, nil
422420
}
423421

422+
func prepareCompareRepoBranchesTagsDropdowns(ctx *context.Context, ci *common.CompareInfo) {
423+
baseRepo := ctx.Repo.Repository
424+
// For compare repo branches
425+
baseBranches, baseTags, err := getBranchesAndTagsForRepo(ctx, baseRepo)
426+
if err != nil {
427+
ctx.ServerError("getBranchesAndTagsForRepo", err)
428+
return
429+
}
430+
431+
ctx.Data["Branches"] = baseBranches
432+
ctx.Data["Tags"] = baseTags
433+
434+
if ci.IsSameRepo() {
435+
ctx.Data["HeadBranches"] = baseBranches
436+
ctx.Data["HeadTags"] = baseTags
437+
} else {
438+
headBranches, headTags, err := getBranchesAndTagsForRepo(ctx, ci.HeadRepo)
439+
if err != nil {
440+
ctx.ServerError("getBranchesAndTagsForRepo", err)
441+
return
442+
}
443+
ctx.Data["HeadBranches"] = headBranches
444+
ctx.Data["HeadTags"] = headTags
445+
}
446+
447+
if ci.RootRepo != nil &&
448+
ci.RootRepo.ID != ci.HeadRepo.ID &&
449+
ci.RootRepo.ID != baseRepo.ID {
450+
canRead := access_model.CheckRepoUnitUser(ctx, ci.RootRepo, ctx.Doer, unit.TypeCode)
451+
if canRead {
452+
ctx.Data["RootRepo"] = ci.RootRepo
453+
branches, tags, err := getBranchesAndTagsForRepo(ctx, ci.RootRepo)
454+
if err != nil {
455+
ctx.ServerError("GetBranchesForRepo", err)
456+
return
457+
}
458+
ctx.Data["RootRepoBranches"] = branches
459+
ctx.Data["RootRepoTags"] = tags
460+
}
461+
}
462+
463+
if ci.OwnForkRepo != nil &&
464+
ci.OwnForkRepo.ID != ci.HeadRepo.ID &&
465+
ci.OwnForkRepo.ID != baseRepo.ID &&
466+
(ci.RootRepo == nil || ci.OwnForkRepo.ID != ci.RootRepo.ID) {
467+
ctx.Data["OwnForkRepo"] = ci.OwnForkRepo
468+
branches, tags, err := getBranchesAndTagsForRepo(ctx, ci.OwnForkRepo)
469+
if err != nil {
470+
ctx.ServerError("GetBranchesForRepo", err)
471+
return
472+
}
473+
ctx.Data["OwnForkRepoBranches"] = branches
474+
ctx.Data["OwnForkRepoTags"] = tags
475+
}
476+
}
477+
424478
// CompareDiff show different from one commit to another commit
425479
func CompareDiff(ctx *context.Context) {
426480
ci := ParseCompareInfo(ctx)
427481
defer func() {
428-
if ci != nil && ci.HeadGitRepo != nil {
429-
ci.HeadGitRepo.Close()
482+
if ci != nil {
483+
ci.Close()
430484
}
431485
}()
432486
if ctx.Written() {
@@ -448,43 +502,17 @@ func CompareDiff(ctx *context.Context) {
448502
return
449503
}
450504

451-
baseTags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
452-
if err != nil {
453-
ctx.ServerError("GetTagNamesByRepoID", err)
454-
return
455-
}
456-
ctx.Data["Tags"] = baseTags
457-
458505
fileOnly := ctx.FormBool("file-only")
459506
if fileOnly {
460507
ctx.HTML(http.StatusOK, tplDiffBox)
461508
return
462509
}
463510

464-
headBranches, err := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{
465-
RepoID: ci.HeadRepo.ID,
466-
ListOptions: db.ListOptionsAll,
467-
IsDeletedBranch: optional.Some(false),
468-
})
469-
if err != nil {
470-
ctx.ServerError("GetBranches", err)
471-
return
472-
}
473-
ctx.Data["HeadBranches"] = headBranches
474-
475-
// For compare repo branches
476-
PrepareBranchList(ctx)
511+
prepareCompareRepoBranchesTagsDropdowns(ctx, ci)
477512
if ctx.Written() {
478513
return
479514
}
480515

481-
headTags, err := repo_model.GetTagNamesByRepoID(ctx, ci.HeadRepo.ID)
482-
if err != nil {
483-
ctx.ServerError("GetTagNamesByRepoID", err)
484-
return
485-
}
486-
ctx.Data["HeadTags"] = headTags
487-
488516
if ctx.Data["PageIsComparePull"] == true {
489517
pr, err := issues_model.GetUnmergedPullRequest(ctx, ci.HeadRepo.ID, ctx.Repo.Repository.ID, ci.HeadOriRef, ci.BaseOriRef, issues_model.PullRequestFlowGithub)
490518
if err != nil {

routers/web/repo/pull.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,8 +1266,8 @@ func CompareAndPullRequestPost(ctx *context.Context) {
12661266

12671267
ci := ParseCompareInfo(ctx)
12681268
defer func() {
1269-
if ci != nil && ci.HeadGitRepo != nil {
1270-
ci.HeadGitRepo.Close()
1269+
if ci != nil {
1270+
ci.Close()
12711271
}
12721272
}()
12731273
if ctx.Written() {

0 commit comments

Comments
 (0)