Skip to content

Commit 0d97cab

Browse files
committed
as per wxiaoguang
Signed-off-by: Andrew Thornton <art27@cantab.net>
1 parent 4d37e49 commit 0d97cab

File tree

1 file changed

+26
-48
lines changed

1 file changed

+26
-48
lines changed

routers/web/repo/compare.go

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -414,26 +414,19 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
414414
if rootRepo != nil &&
415415
rootRepo.ID != ci.HeadRepo.ID &&
416416
rootRepo.ID != baseRepo.ID {
417-
if !fileOnly {
418-
perm, branches, tags, err := getBranchesAndTagsForRepo(ctx.User, rootRepo)
419-
if err != nil {
420-
ctx.ServerError("GetBranchesForRepo", err)
421-
return nil
422-
}
423-
if perm {
424-
ctx.Data["RootRepo"] = rootRepo
417+
canRead := rootRepo.CheckUnitUser(ctx.User, models.UnitTypeCode)
418+
if canRead {
419+
ctx.Data["RootRepo"] = rootRepo
420+
if !fileOnly {
421+
branches, tags, err := getBranchesAndTagsForRepo(ctx.User, rootRepo)
422+
if err != nil {
423+
ctx.ServerError("GetBranchesForRepo", err)
424+
return nil
425+
}
426+
425427
ctx.Data["RootRepoBranches"] = branches
426428
ctx.Data["RootRepoTags"] = tags
427429
}
428-
} else {
429-
perm, err := models.GetUserRepoPermission(rootRepo, ctx.User)
430-
if err != nil {
431-
ctx.ServerError("GetUserRepoPermission", err)
432-
return nil
433-
}
434-
if perm.CanRead(models.UnitTypeCode) {
435-
ctx.Data["RootRepo"] = rootRepo
436-
}
437430
}
438431
}
439432

@@ -446,26 +439,18 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
446439
ownForkRepo.ID != ci.HeadRepo.ID &&
447440
ownForkRepo.ID != baseRepo.ID &&
448441
(rootRepo == nil || ownForkRepo.ID != rootRepo.ID) {
449-
if !fileOnly {
450-
perm, branches, tags, err := getBranchesAndTagsForRepo(ctx.User, ownForkRepo)
451-
if err != nil {
452-
ctx.ServerError("GetBranchesForRepo", err)
453-
return nil
454-
}
455-
if perm {
456-
ctx.Data["OwnForkRepo"] = ownForkRepo
442+
canRead := ownForkRepo.CheckUnitUser(ctx.User, models.UnitTypeCode)
443+
if canRead {
444+
ctx.Data["OwnForkRepo"] = ownForkRepo
445+
if !fileOnly {
446+
branches, tags, err := getBranchesAndTagsForRepo(ctx.User, ownForkRepo)
447+
if err != nil {
448+
ctx.ServerError("GetBranchesForRepo", err)
449+
return nil
450+
}
457451
ctx.Data["OwnForkRepoBranches"] = branches
458452
ctx.Data["OwnForkRepoTags"] = tags
459453
}
460-
} else {
461-
perm, err := models.GetUserRepoPermission(ownForkRepo, ctx.User)
462-
if err != nil {
463-
ctx.ServerError("GetUserRepoPermission", err)
464-
return nil
465-
}
466-
if perm.CanRead(models.UnitTypeCode) {
467-
ctx.Data["OwnForkRepo"] = ownForkRepo
468-
}
469454
}
470455
}
471456

@@ -631,29 +616,22 @@ func PrepareCompareDiff(
631616
return false
632617
}
633618

634-
func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (bool, []string, []string, error) {
635-
perm, err := models.GetUserRepoPermission(repo, user)
636-
if err != nil {
637-
return false, nil, nil, err
638-
}
639-
if !perm.CanRead(models.UnitTypeCode) {
640-
return false, nil, nil, nil
641-
}
619+
func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (branches, tags []string, err error) {
642620
gitRepo, err := git.OpenRepository(repo.RepoPath())
643621
if err != nil {
644-
return false, nil, nil, err
622+
return nil, nil, err
645623
}
646624
defer gitRepo.Close()
647625

648-
branches, _, err := gitRepo.GetBranches(0, 0)
626+
branches, _, err = gitRepo.GetBranches(0, 0)
649627
if err != nil {
650-
return false, nil, nil, err
628+
return nil, nil, err
651629
}
652-
tags, err := gitRepo.GetTags(0, 0)
630+
tags, err = gitRepo.GetTags(0, 0)
653631
if err != nil {
654-
return false, nil, nil, err
632+
return nil, nil, err
655633
}
656-
return true, branches, tags, nil
634+
return branches, tags, nil
657635
}
658636

659637
// CompareDiff show different from one commit to another commit

0 commit comments

Comments
 (0)