Skip to content

Commit

Permalink
Change PR commits and diffs to use base repo rather than forked
Browse files Browse the repository at this point in the history
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
(go-gitea#3356).
  • Loading branch information
krrutkow committed Jan 23, 2019
1 parent 6a949af commit d5ab55d
Showing 1 changed file with 35 additions and 69 deletions.
104 changes: 35 additions & 69 deletions routers/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,30 +362,23 @@ func ViewPullCommits(ctx *context.Context) {
pull := issue.PullRequest

var commits *list.List
var prInfo *git.PullRequestInfo
if pull.HasMerged {
prInfo := PrepareMergedViewPullInfo(ctx, issue)
if ctx.Written() {
return
} else if prInfo == nil {
ctx.NotFound("ViewPullCommits", nil)
return
}
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
commits = prInfo.Commits
prInfo = PrepareMergedViewPullInfo(ctx, issue)
} else {
prInfo := PrepareViewPullInfo(ctx, issue)
if ctx.Written() {
return
} else if prInfo == nil {
ctx.NotFound("ViewPullCommits", nil)
return
}
ctx.Data["Username"] = pull.HeadUserName
ctx.Data["Reponame"] = pull.HeadRepo.Name
commits = prInfo.Commits
prInfo = PrepareViewPullInfo(ctx, issue)
}

if ctx.Written() {
return
} else if prInfo == nil {
ctx.NotFound("ViewPullCommits", nil)
return
}

ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
commits = prInfo.Commits
commits = models.ValidateCommitsWithEmails(commits)
commits = models.ParseCommitsWithSignature(commits)
commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
Expand Down Expand Up @@ -420,62 +413,35 @@ func ViewPullFiles(ctx *context.Context) {
)

var headTarget string
var prInfo *git.PullRequestInfo
if pull.HasMerged {
prInfo := PrepareMergedViewPullInfo(ctx, issue)
if ctx.Written() {
return
} else if prInfo == nil {
ctx.NotFound("ViewPullFiles", nil)
return
}

diffRepoPath = ctx.Repo.GitRepo.Path
gitRepo = ctx.Repo.GitRepo

headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
if err != nil {
ctx.ServerError("GetRefCommitID", err)
return
}

startCommitID = prInfo.MergeBase
endCommitID = headCommitID

headTarget = path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
prInfo = PrepareMergedViewPullInfo(ctx, issue)
} else {
prInfo := PrepareViewPullInfo(ctx, issue)
if ctx.Written() {
return
} else if prInfo == nil {
ctx.NotFound("ViewPullFiles", nil)
return
}
prInfo = PrepareViewPullInfo(ctx, issue)
}

headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
if ctx.Written() {
return
} else if prInfo == nil {
ctx.NotFound("ViewPullFiles", nil)
return
}

headGitRepo, err := git.OpenRepository(headRepoPath)
if err != nil {
ctx.ServerError("OpenRepository", err)
return
}
diffRepoPath = ctx.Repo.GitRepo.Path
gitRepo = ctx.Repo.GitRepo

headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
if err != nil {
ctx.ServerError("GetBranchCommitID", err)
return
}
headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
if err != nil {
ctx.ServerError("GetRefCommitID", err)
return
}

diffRepoPath = headRepoPath
startCommitID = prInfo.MergeBase
endCommitID = headCommitID
gitRepo = headGitRepo
startCommitID = prInfo.MergeBase
endCommitID = headCommitID

headTarget = path.Join(pull.HeadUserName, pull.HeadRepo.Name)
ctx.Data["Username"] = pull.HeadUserName
ctx.Data["Reponame"] = pull.HeadRepo.Name
}
headTarget = path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name

diff, err := models.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
Expand Down

0 comments on commit d5ab55d

Please sign in to comment.