Skip to content

Commit d5ab55d

Browse files
committed
Change PR commits and diffs to use base repo rather than forked
Change the repository referenced when displaying commits or diffs in pull request to the base repository. The forked repository may not be readable by users who can read the base repository. See discussion for (#3356).
1 parent 6a949af commit d5ab55d

File tree

1 file changed

+35
-69
lines changed

1 file changed

+35
-69
lines changed

routers/repo/pull.go

Lines changed: 35 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -362,30 +362,23 @@ func ViewPullCommits(ctx *context.Context) {
362362
pull := issue.PullRequest
363363

364364
var commits *list.List
365+
var prInfo *git.PullRequestInfo
365366
if pull.HasMerged {
366-
prInfo := PrepareMergedViewPullInfo(ctx, issue)
367-
if ctx.Written() {
368-
return
369-
} else if prInfo == nil {
370-
ctx.NotFound("ViewPullCommits", nil)
371-
return
372-
}
373-
ctx.Data["Username"] = ctx.Repo.Owner.Name
374-
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
375-
commits = prInfo.Commits
367+
prInfo = PrepareMergedViewPullInfo(ctx, issue)
376368
} else {
377-
prInfo := PrepareViewPullInfo(ctx, issue)
378-
if ctx.Written() {
379-
return
380-
} else if prInfo == nil {
381-
ctx.NotFound("ViewPullCommits", nil)
382-
return
383-
}
384-
ctx.Data["Username"] = pull.HeadUserName
385-
ctx.Data["Reponame"] = pull.HeadRepo.Name
386-
commits = prInfo.Commits
369+
prInfo = PrepareViewPullInfo(ctx, issue)
387370
}
388371

372+
if ctx.Written() {
373+
return
374+
} else if prInfo == nil {
375+
ctx.NotFound("ViewPullCommits", nil)
376+
return
377+
}
378+
379+
ctx.Data["Username"] = ctx.Repo.Owner.Name
380+
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
381+
commits = prInfo.Commits
389382
commits = models.ValidateCommitsWithEmails(commits)
390383
commits = models.ParseCommitsWithSignature(commits)
391384
commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
@@ -420,62 +413,35 @@ func ViewPullFiles(ctx *context.Context) {
420413
)
421414

422415
var headTarget string
416+
var prInfo *git.PullRequestInfo
423417
if pull.HasMerged {
424-
prInfo := PrepareMergedViewPullInfo(ctx, issue)
425-
if ctx.Written() {
426-
return
427-
} else if prInfo == nil {
428-
ctx.NotFound("ViewPullFiles", nil)
429-
return
430-
}
431-
432-
diffRepoPath = ctx.Repo.GitRepo.Path
433-
gitRepo = ctx.Repo.GitRepo
434-
435-
headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
436-
if err != nil {
437-
ctx.ServerError("GetRefCommitID", err)
438-
return
439-
}
440-
441-
startCommitID = prInfo.MergeBase
442-
endCommitID = headCommitID
443-
444-
headTarget = path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
445-
ctx.Data["Username"] = ctx.Repo.Owner.Name
446-
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
418+
prInfo = PrepareMergedViewPullInfo(ctx, issue)
447419
} else {
448-
prInfo := PrepareViewPullInfo(ctx, issue)
449-
if ctx.Written() {
450-
return
451-
} else if prInfo == nil {
452-
ctx.NotFound("ViewPullFiles", nil)
453-
return
454-
}
420+
prInfo = PrepareViewPullInfo(ctx, issue)
421+
}
455422

456-
headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
423+
if ctx.Written() {
424+
return
425+
} else if prInfo == nil {
426+
ctx.NotFound("ViewPullFiles", nil)
427+
return
428+
}
457429

458-
headGitRepo, err := git.OpenRepository(headRepoPath)
459-
if err != nil {
460-
ctx.ServerError("OpenRepository", err)
461-
return
462-
}
430+
diffRepoPath = ctx.Repo.GitRepo.Path
431+
gitRepo = ctx.Repo.GitRepo
463432

464-
headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
465-
if err != nil {
466-
ctx.ServerError("GetBranchCommitID", err)
467-
return
468-
}
433+
headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
434+
if err != nil {
435+
ctx.ServerError("GetRefCommitID", err)
436+
return
437+
}
469438

470-
diffRepoPath = headRepoPath
471-
startCommitID = prInfo.MergeBase
472-
endCommitID = headCommitID
473-
gitRepo = headGitRepo
439+
startCommitID = prInfo.MergeBase
440+
endCommitID = headCommitID
474441

475-
headTarget = path.Join(pull.HeadUserName, pull.HeadRepo.Name)
476-
ctx.Data["Username"] = pull.HeadUserName
477-
ctx.Data["Reponame"] = pull.HeadRepo.Name
478-
}
442+
headTarget = path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
443+
ctx.Data["Username"] = ctx.Repo.Owner.Name
444+
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
479445

480446
diff, err := models.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
481447
startCommitID, endCommitID, setting.Git.MaxGitDiffLines,

0 commit comments

Comments
 (0)