]> source.dussan.org Git - gitea.git/commitdiff
Fix compare (#9808)
authorLunny Xiao <xiaolunwen@gmail.com>
Thu, 16 Jan 2020 19:59:07 +0000 (03:59 +0800)
committerLauris BH <lauris@nix.lv>
Thu, 16 Jan 2020 19:59:07 +0000 (21:59 +0200)
Co-authored-by: techknowlogick <matti@mdranta.net>
routers/repo/compare.go

index bb800f9ef70ccff3b2707ca03f20a9cff2690b83..50c439f74649a4edbd74bc78ad5a378f6bae6da8 100644 (file)
@@ -157,12 +157,12 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
                        ctx.ServerError("OpenRepository", err)
                        return nil, nil, nil, nil, "", ""
                }
+               defer headGitRepo.Close()
        }
 
        // user should have permission to read baseRepo's codes and pulls, NOT headRepo's
        permBase, err := models.GetUserRepoPermission(baseRepo, ctx.User)
        if err != nil {
-               headGitRepo.Close()
                ctx.ServerError("GetUserRepoPermission", err)
                return nil, nil, nil, nil, "", ""
        }
@@ -173,42 +173,40 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
                                baseRepo,
                                permBase)
                }
-               headGitRepo.Close()
                ctx.NotFound("ParseCompareInfo", nil)
                return nil, nil, nil, nil, "", ""
        }
 
-       // user should have permission to read headrepo's codes
-       permHead, err := models.GetUserRepoPermission(headRepo, ctx.User)
-       if err != nil {
-               headGitRepo.Close()
-               ctx.ServerError("GetUserRepoPermission", err)
-               return nil, nil, nil, nil, "", ""
-       }
-       if !permHead.CanRead(models.UnitTypeCode) {
-               if log.IsTrace() {
-                       log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in headRepo has Permissions: %-+v",
-                               ctx.User,
-                               headRepo,
-                               permHead)
+       if !isSameRepo {
+               // user should have permission to read headrepo's codes
+               permHead, err := models.GetUserRepoPermission(headRepo, ctx.User)
+               if err != nil {
+                       ctx.ServerError("GetUserRepoPermission", err)
+                       return nil, nil, nil, nil, "", ""
+               }
+               if !permHead.CanRead(models.UnitTypeCode) {
+                       if log.IsTrace() {
+                               log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in headRepo has Permissions: %-+v",
+                                       ctx.User,
+                                       headRepo,
+                                       permHead)
+                       }
+                       ctx.NotFound("ParseCompareInfo", nil)
+                       return nil, nil, nil, nil, "", ""
                }
-               headGitRepo.Close()
-               ctx.NotFound("ParseCompareInfo", nil)
-               return nil, nil, nil, nil, "", ""
        }
 
        // Check if head branch is valid.
-       headIsCommit := ctx.Repo.GitRepo.IsCommitExist(headBranch)
+       headIsCommit := headGitRepo.IsCommitExist(headBranch)
        headIsBranch := headGitRepo.IsBranchExist(headBranch)
        headIsTag := headGitRepo.IsTagExist(headBranch)
        if !headIsCommit && !headIsBranch && !headIsTag {
                // Check if headBranch is short sha commit hash
-               if headCommit, _ := ctx.Repo.GitRepo.GetCommit(headBranch); headCommit != nil {
+               if headCommit, _ := headGitRepo.GetCommit(headBranch); headCommit != nil {
                        headBranch = headCommit.ID.String()
                        ctx.Data["HeadBranch"] = headBranch
                        headIsCommit = true
                } else {
-                       headGitRepo.Close()
                        ctx.NotFound("IsRefExist", nil)
                        return nil, nil, nil, nil, "", ""
                }
@@ -229,14 +227,12 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
                                baseRepo,
                                permBase)
                }
-               headGitRepo.Close()
                ctx.NotFound("ParseCompareInfo", nil)
                return nil, nil, nil, nil, "", ""
        }
 
-       compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
+       compareInfo, err := headGitRepo.GetCompareInfo(baseRepo.RepoPath(), baseBranch, headBranch)
        if err != nil {
-               headGitRepo.Close()
                ctx.ServerError("GetCompareInfo", err)
                return nil, nil, nil, nil, "", ""
        }