Skip to content

Commit

Permalink
as per wxiaoguang
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Sep 27, 2021
1 parent 4d37e49 commit 0d97cab
Showing 1 changed file with 26 additions and 48 deletions.
74 changes: 26 additions & 48 deletions routers/web/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,26 +414,19 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
if rootRepo != nil &&
rootRepo.ID != ci.HeadRepo.ID &&
rootRepo.ID != baseRepo.ID {
if !fileOnly {
perm, branches, tags, err := getBranchesAndTagsForRepo(ctx.User, rootRepo)
if err != nil {
ctx.ServerError("GetBranchesForRepo", err)
return nil
}
if perm {
ctx.Data["RootRepo"] = rootRepo
canRead := rootRepo.CheckUnitUser(ctx.User, models.UnitTypeCode)
if canRead {
ctx.Data["RootRepo"] = rootRepo
if !fileOnly {
branches, tags, err := getBranchesAndTagsForRepo(ctx.User, rootRepo)
if err != nil {
ctx.ServerError("GetBranchesForRepo", err)
return nil
}

ctx.Data["RootRepoBranches"] = branches
ctx.Data["RootRepoTags"] = tags
}
} else {
perm, err := models.GetUserRepoPermission(rootRepo, ctx.User)
if err != nil {
ctx.ServerError("GetUserRepoPermission", err)
return nil
}
if perm.CanRead(models.UnitTypeCode) {
ctx.Data["RootRepo"] = rootRepo
}
}
}

Expand All @@ -446,26 +439,18 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
ownForkRepo.ID != ci.HeadRepo.ID &&
ownForkRepo.ID != baseRepo.ID &&
(rootRepo == nil || ownForkRepo.ID != rootRepo.ID) {
if !fileOnly {
perm, branches, tags, err := getBranchesAndTagsForRepo(ctx.User, ownForkRepo)
if err != nil {
ctx.ServerError("GetBranchesForRepo", err)
return nil
}
if perm {
ctx.Data["OwnForkRepo"] = ownForkRepo
canRead := ownForkRepo.CheckUnitUser(ctx.User, models.UnitTypeCode)
if canRead {
ctx.Data["OwnForkRepo"] = ownForkRepo
if !fileOnly {
branches, tags, err := getBranchesAndTagsForRepo(ctx.User, ownForkRepo)
if err != nil {
ctx.ServerError("GetBranchesForRepo", err)
return nil
}
ctx.Data["OwnForkRepoBranches"] = branches
ctx.Data["OwnForkRepoTags"] = tags
}
} else {
perm, err := models.GetUserRepoPermission(ownForkRepo, ctx.User)
if err != nil {
ctx.ServerError("GetUserRepoPermission", err)
return nil
}
if perm.CanRead(models.UnitTypeCode) {
ctx.Data["OwnForkRepo"] = ownForkRepo
}
}
}

Expand Down Expand Up @@ -631,29 +616,22 @@ func PrepareCompareDiff(
return false
}

func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (bool, []string, []string, error) {
perm, err := models.GetUserRepoPermission(repo, user)
if err != nil {
return false, nil, nil, err
}
if !perm.CanRead(models.UnitTypeCode) {
return false, nil, nil, nil
}
func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (branches, tags []string, err error) {
gitRepo, err := git.OpenRepository(repo.RepoPath())
if err != nil {
return false, nil, nil, err
return nil, nil, err
}
defer gitRepo.Close()

branches, _, err := gitRepo.GetBranches(0, 0)
branches, _, err = gitRepo.GetBranches(0, 0)
if err != nil {
return false, nil, nil, err
return nil, nil, err
}
tags, err := gitRepo.GetTags(0, 0)
tags, err = gitRepo.GetTags(0, 0)
if err != nil {
return false, nil, nil, err
return nil, nil, err
}
return true, branches, tags, nil
return branches, tags, nil
}

// CompareDiff show different from one commit to another commit
Expand Down

0 comments on commit 0d97cab

Please sign in to comment.