Skip to content

Commit d3468ed

Browse files
lunnytechknowlogick
authored andcommitted
Fix compare (go-gitea#9808)
Co-authored-by: techknowlogick <[email protected]>
1 parent 8dd5ab2 commit d3468ed

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
@@ -157,12 +157,12 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
157157
ctx.ServerError("OpenRepository", err)
158158
return nil, nil, nil, nil, "", ""
159159
}
160+
defer headGitRepo.Close()
160161
}
161162

162163
// user should have permission to read baseRepo's codes and pulls, NOT headRepo's
163164
permBase, err := models.GetUserRepoPermission(baseRepo, ctx.User)
164165
if err != nil {
165-
headGitRepo.Close()
166166
ctx.ServerError("GetUserRepoPermission", err)
167167
return nil, nil, nil, nil, "", ""
168168
}
@@ -173,42 +173,40 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
173173
baseRepo,
174174
permBase)
175175
}
176-
headGitRepo.Close()
177176
ctx.NotFound("ParseCompareInfo", nil)
178177
return nil, nil, nil, nil, "", ""
179178
}
180179

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

200199
// Check if head branch is valid.
201-
headIsCommit := ctx.Repo.GitRepo.IsCommitExist(headBranch)
200+
headIsCommit := headGitRepo.IsCommitExist(headBranch)
202201
headIsBranch := headGitRepo.IsBranchExist(headBranch)
203202
headIsTag := headGitRepo.IsTagExist(headBranch)
204203
if !headIsCommit && !headIsBranch && !headIsTag {
205204
// Check if headBranch is short sha commit hash
206-
if headCommit, _ := ctx.Repo.GitRepo.GetCommit(headBranch); headCommit != nil {
205+
if headCommit, _ := headGitRepo.GetCommit(headBranch); headCommit != nil {
207206
headBranch = headCommit.ID.String()
208207
ctx.Data["HeadBranch"] = headBranch
209208
headIsCommit = true
210209
} else {
211-
headGitRepo.Close()
212210
ctx.NotFound("IsRefExist", nil)
213211
return nil, nil, nil, nil, "", ""
214212
}
@@ -229,14 +227,12 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
229227
baseRepo,
230228
permBase)
231229
}
232-
headGitRepo.Close()
233230
ctx.NotFound("ParseCompareInfo", nil)
234231
return nil, nil, nil, nil, "", ""
235232
}
236233

237-
compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
234+
compareInfo, err := headGitRepo.GetCompareInfo(baseRepo.RepoPath(), baseBranch, headBranch)
238235
if err != nil {
239-
headGitRepo.Close()
240236
ctx.ServerError("GetCompareInfo", err)
241237
return nil, nil, nil, nil, "", ""
242238
}

0 commit comments

Comments
 (0)