Skip to content

Commit ed6a2f2

Browse files
Fix compare (#9808) (#9815)
Co-authored-by: techknowlogick <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent a0435fc commit ed6a2f2

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

routers/repo/compare.go

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
152152
ctx.ServerError("OpenRepository", err)
153153
return nil, nil, nil, nil, "", ""
154154
}
155+
defer headGitRepo.Close()
155156
}
156157

157158
// user should have permission to read baseRepo's codes and pulls, NOT headRepo's
158159
permBase, err := models.GetUserRepoPermission(baseRepo, ctx.User)
159160
if err != nil {
160-
headGitRepo.Close()
161161
ctx.ServerError("GetUserRepoPermission", err)
162162
return nil, nil, nil, nil, "", ""
163163
}
@@ -168,42 +168,40 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
168168
baseRepo,
169169
permBase)
170170
}
171-
headGitRepo.Close()
172171
ctx.NotFound("ParseCompareInfo", nil)
173172
return nil, nil, nil, nil, "", ""
174173
}
175174

176-
// user should have permission to read headrepo's codes
177-
permHead, err := models.GetUserRepoPermission(headRepo, ctx.User)
178-
if err != nil {
179-
headGitRepo.Close()
180-
ctx.ServerError("GetUserRepoPermission", err)
181-
return nil, nil, nil, nil, "", ""
182-
}
183-
if !permHead.CanRead(models.UnitTypeCode) {
184-
if log.IsTrace() {
185-
log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in headRepo has Permissions: %-+v",
186-
ctx.User,
187-
headRepo,
188-
permHead)
175+
if !isSameRepo {
176+
// user should have permission to read headrepo's codes
177+
permHead, err := models.GetUserRepoPermission(headRepo, ctx.User)
178+
if err != nil {
179+
ctx.ServerError("GetUserRepoPermission", err)
180+
return nil, nil, nil, nil, "", ""
181+
}
182+
if !permHead.CanRead(models.UnitTypeCode) {
183+
if log.IsTrace() {
184+
log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in headRepo has Permissions: %-+v",
185+
ctx.User,
186+
headRepo,
187+
permHead)
188+
}
189+
ctx.NotFound("ParseCompareInfo", nil)
190+
return nil, nil, nil, nil, "", ""
189191
}
190-
headGitRepo.Close()
191-
ctx.NotFound("ParseCompareInfo", nil)
192-
return nil, nil, nil, nil, "", ""
193192
}
194193

195194
// Check if head branch is valid.
196-
headIsCommit := ctx.Repo.GitRepo.IsCommitExist(headBranch)
195+
headIsCommit := headGitRepo.IsCommitExist(headBranch)
197196
headIsBranch := headGitRepo.IsBranchExist(headBranch)
198197
headIsTag := headGitRepo.IsTagExist(headBranch)
199198
if !headIsCommit && !headIsBranch && !headIsTag {
200199
// Check if headBranch is short sha commit hash
201-
if headCommit, _ := ctx.Repo.GitRepo.GetCommit(headBranch); headCommit != nil {
200+
if headCommit, _ := headGitRepo.GetCommit(headBranch); headCommit != nil {
202201
headBranch = headCommit.ID.String()
203202
ctx.Data["HeadBranch"] = headBranch
204203
headIsCommit = true
205204
} else {
206-
headGitRepo.Close()
207205
ctx.NotFound("IsRefExist", nil)
208206
return nil, nil, nil, nil, "", ""
209207
}
@@ -224,14 +222,12 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
224222
baseRepo,
225223
permBase)
226224
}
227-
headGitRepo.Close()
228225
ctx.NotFound("ParseCompareInfo", nil)
229226
return nil, nil, nil, nil, "", ""
230227
}
231228

232-
compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
229+
compareInfo, err := headGitRepo.GetCompareInfo(baseRepo.RepoPath(), baseBranch, headBranch)
233230
if err != nil {
234-
headGitRepo.Close()
235231
ctx.ServerError("GetCompareInfo", err)
236232
return nil, nil, nil, nil, "", ""
237233
}

0 commit comments

Comments
 (0)