Skip to content

Commit 21f84a6

Browse files
krrutkowtechknowlogick
authored andcommitted
Change PR commits and diffs to use base repo rather than forked (#3648)
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 d66ae50 commit 21f84a6

File tree

1 file changed

+35
-70
lines changed

1 file changed

+35
-70
lines changed

routers/repo/pull.go

+35-70
Original file line numberDiff line numberDiff line change
@@ -438,30 +438,23 @@ func ViewPullCommits(ctx *context.Context) {
438438
pull := issue.PullRequest
439439

440440
var commits *list.List
441+
var prInfo *git.CompareInfo
441442
if pull.HasMerged {
442-
prInfo := PrepareMergedViewPullInfo(ctx, issue)
443-
if ctx.Written() {
444-
return
445-
} else if prInfo == nil {
446-
ctx.NotFound("ViewPullCommits", nil)
447-
return
448-
}
449-
ctx.Data["Username"] = ctx.Repo.Owner.Name
450-
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
451-
commits = prInfo.Commits
443+
prInfo = PrepareMergedViewPullInfo(ctx, issue)
452444
} else {
453-
prInfo := PrepareViewPullInfo(ctx, issue)
454-
if ctx.Written() {
455-
return
456-
} else if prInfo == nil {
457-
ctx.NotFound("ViewPullCommits", nil)
458-
return
459-
}
460-
ctx.Data["Username"] = pull.MustHeadUserName()
461-
ctx.Data["Reponame"] = pull.HeadRepo.Name
462-
commits = prInfo.Commits
445+
prInfo = PrepareViewPullInfo(ctx, issue)
463446
}
464447

448+
if ctx.Written() {
449+
return
450+
} else if prInfo == nil {
451+
ctx.NotFound("ViewPullCommits", nil)
452+
return
453+
}
454+
455+
ctx.Data["Username"] = ctx.Repo.Owner.Name
456+
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
457+
commits = prInfo.Commits
465458
commits = models.ValidateCommitsWithEmails(commits)
466459
commits = models.ParseCommitsWithSignature(commits)
467460
commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
@@ -497,63 +490,35 @@ func ViewPullFiles(ctx *context.Context) {
497490
)
498491

499492
var headTarget string
493+
var prInfo *git.CompareInfo
500494
if pull.HasMerged {
501-
prInfo := PrepareMergedViewPullInfo(ctx, issue)
502-
if ctx.Written() {
503-
return
504-
} else if prInfo == nil {
505-
ctx.NotFound("ViewPullFiles", nil)
506-
return
507-
}
508-
509-
diffRepoPath = ctx.Repo.GitRepo.Path
510-
gitRepo = ctx.Repo.GitRepo
511-
512-
headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
513-
if err != nil {
514-
ctx.ServerError("GetRefCommitID", err)
515-
return
516-
}
517-
518-
startCommitID = prInfo.MergeBase
519-
endCommitID = headCommitID
520-
521-
headTarget = path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
522-
ctx.Data["Username"] = ctx.Repo.Owner.Name
523-
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
495+
prInfo = PrepareMergedViewPullInfo(ctx, issue)
524496
} else {
525-
prInfo := PrepareViewPullInfo(ctx, issue)
526-
if ctx.Written() {
527-
return
528-
} else if prInfo == nil {
529-
ctx.NotFound("ViewPullFiles", nil)
530-
return
531-
}
497+
prInfo = PrepareViewPullInfo(ctx, issue)
498+
}
532499

533-
headRepoPath := pull.HeadRepo.RepoPath()
500+
if ctx.Written() {
501+
return
502+
} else if prInfo == nil {
503+
ctx.NotFound("ViewPullFiles", nil)
504+
return
505+
}
534506

535-
headGitRepo, err := git.OpenRepository(headRepoPath)
536-
if err != nil {
537-
ctx.ServerError("OpenRepository", err)
538-
return
539-
}
540-
defer headGitRepo.Close()
507+
diffRepoPath = ctx.Repo.GitRepo.Path
508+
gitRepo = ctx.Repo.GitRepo
541509

542-
headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
543-
if err != nil {
544-
ctx.ServerError("GetBranchCommitID", err)
545-
return
546-
}
510+
headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
511+
if err != nil {
512+
ctx.ServerError("GetRefCommitID", err)
513+
return
514+
}
547515

548-
diffRepoPath = headRepoPath
549-
startCommitID = prInfo.MergeBase
550-
endCommitID = headCommitID
551-
gitRepo = headGitRepo
516+
startCommitID = prInfo.MergeBase
517+
endCommitID = headCommitID
552518

553-
headTarget = path.Join(pull.MustHeadUserName(), pull.HeadRepo.Name)
554-
ctx.Data["Username"] = pull.MustHeadUserName()
555-
ctx.Data["Reponame"] = pull.HeadRepo.Name
556-
}
519+
headTarget = path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
520+
ctx.Data["Username"] = ctx.Repo.Owner.Name
521+
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
557522
ctx.Data["AfterCommitID"] = endCommitID
558523

559524
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,

0 commit comments

Comments
 (0)